-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(command): scheduler:export #198
Open
Guikingone
wants to merge
5
commits into
main
Choose a base branch
from
issue/133
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: | ||
- main | ||
schedule: | ||
- cron: "0 0 * * *" | ||
- cron: "5 0 * * *" | ||
|
||
jobs: | ||
php-cs-fixer: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: | ||
- main | ||
schedule: | ||
- cron: "0 0 * * *" | ||
- cron: "5 0 * * *" | ||
|
||
jobs: | ||
infection: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: | ||
- main | ||
schedule: | ||
- cron: "0 0 * * *" | ||
- cron: "5 0 * * *" | ||
|
||
jobs: | ||
phpunit: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: | ||
- main | ||
schedule: | ||
- cron: "0 0 * * *" | ||
- cron: "5 0 * * *" | ||
|
||
jobs: | ||
phpstan: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: | ||
- main | ||
schedule: | ||
- cron: "0 0 * * *" | ||
- cron: "5 0 * * *" | ||
|
||
jobs: | ||
security: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: | ||
- main | ||
schedule: | ||
- cron: "0 0 * * *" | ||
- cron: "5 0 * * *" | ||
|
||
jobs: | ||
phpstan: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,86 @@ | ||||||||||||||||||
<?php | ||||||||||||||||||
|
||||||||||||||||||
declare(strict_types=1); | ||||||||||||||||||
|
||||||||||||||||||
namespace SchedulerBundle\Command; | ||||||||||||||||||
|
||||||||||||||||||
use SchedulerBundle\Export\ExporterRegistryInterface; | ||||||||||||||||||
use SchedulerBundle\SchedulerInterface; | ||||||||||||||||||
use SchedulerBundle\Task\TaskInterface; | ||||||||||||||||||
use Symfony\Component\Console\Command\Command; | ||||||||||||||||||
use Symfony\Component\Console\Input\InputArgument; | ||||||||||||||||||
use Symfony\Component\Console\Input\InputInterface; | ||||||||||||||||||
use Symfony\Component\Console\Input\InputOption; | ||||||||||||||||||
use Symfony\Component\Console\Output\OutputInterface; | ||||||||||||||||||
use Symfony\Component\Console\Style\SymfonyStyle; | ||||||||||||||||||
use Throwable; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* @author Guillaume Loulier <contact@guillaumeloulier.fr> | ||||||||||||||||||
*/ | ||||||||||||||||||
final class ExportCommand extends Command | ||||||||||||||||||
{ | ||||||||||||||||||
private ExporterRegistryInterface $exporterRegistry; | ||||||||||||||||||
private SchedulerInterface $scheduler; | ||||||||||||||||||
|
||||||||||||||||||
protected static $defaultName = 'scheduler:export'; | ||||||||||||||||||
|
||||||||||||||||||
public function __construct( | ||||||||||||||||||
ExporterRegistryInterface $exporterRegistry, | ||||||||||||||||||
SchedulerInterface $scheduler | ||||||||||||||||||
) { | ||||||||||||||||||
$this->exporterRegistry = $exporterRegistry; | ||||||||||||||||||
$this->scheduler = $scheduler; | ||||||||||||||||||
Comment on lines
+29
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
parent::__construct(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* {@inheritdoc} | ||||||||||||||||||
*/ | ||||||||||||||||||
protected function configure(): void | ||||||||||||||||||
{ | ||||||||||||||||||
$this | ||||||||||||||||||
->setDescription('Export tasks to a specific format') | ||||||||||||||||||
->setDefinition([ | ||||||||||||||||||
new InputArgument('format', InputArgument::REQUIRED, 'The format used to export tasks', 'crontab'), | ||||||||||||||||||
new InputOption('filename', null, InputOption::VALUE_OPTIONAL, 'The name of the filename used to export tasks', '/etc/cron.d'), | ||||||||||||||||||
]) | ||||||||||||||||||
; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* {@inheritdoc} | ||||||||||||||||||
*/ | ||||||||||||||||||
protected function execute(InputInterface $input, OutputInterface $output): int | ||||||||||||||||||
{ | ||||||||||||||||||
$style = new SymfonyStyle($input, $output); | ||||||||||||||||||
|
||||||||||||||||||
try { | ||||||||||||||||||
$tasks = $this->scheduler->getTasks(); | ||||||||||||||||||
if (0 === $tasks->count()) { | ||||||||||||||||||
$style->warning('No tasks found'); | ||||||||||||||||||
|
||||||||||||||||||
return Command::FAILURE; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
$filename = $input->getOption('filename'); | ||||||||||||||||||
$exporter = $this->exporterRegistry->find($input->getArgument('format')); | ||||||||||||||||||
|
||||||||||||||||||
$tasks->walk(function (TaskInterface $task) use ($exporter, $filename): void { | ||||||||||||||||||
$exporter->export($filename, $task); | ||||||||||||||||||
}); | ||||||||||||||||||
} catch (Throwable $throwable) { | ||||||||||||||||||
$style->error([ | ||||||||||||||||||
'An error occurred when exporting tasks:', | ||||||||||||||||||
$throwable->getMessage(), | ||||||||||||||||||
]); | ||||||||||||||||||
|
||||||||||||||||||
return Command::FAILURE; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
$style->success('The export has succeed'); | ||||||||||||||||||
|
||||||||||||||||||
return Command::SUCCESS; | ||||||||||||||||||
} | ||||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,23 @@ | ||||||||||||||||
<?php | ||||||||||||||||
|
||||||||||||||||
declare(strict_types=1); | ||||||||||||||||
|
||||||||||||||||
namespace SchedulerBundle\Export; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* @author Guillaume Loulier <contact@guillaumeloulier.fr> | ||||||||||||||||
*/ | ||||||||||||||||
abstract class AbstractExporter implements ExporterInterface | ||||||||||||||||
{ | ||||||||||||||||
private string $projectDir; | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
public function __construct(string $projectDir) | ||||||||||||||||
{ | ||||||||||||||||
$this->projectDir = $projectDir; | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+14
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
protected function getProjectDir(): string | ||||||||||||||||
{ | ||||||||||||||||
return $this->projectDir; | ||||||||||||||||
} | ||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SchedulerBundle\Export; | ||
|
||
use SchedulerBundle\Task\TaskInterface; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use function sprintf; | ||
|
||
/** | ||
* @author Guillaume Loulier <contact@guillaumeloulier.fr> | ||
*/ | ||
final class CronTabExporter extends AbstractExporter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function export(string $filename, TaskInterface $task): void | ||
{ | ||
$finalFilename = sprintf('%s/%s', $this->getProjectDir(), $task->getName()); | ||
|
||
$fs = new Filesystem(); | ||
|
||
if ($fs->exists($finalFilename)) { | ||
return; | ||
} | ||
|
||
$fs->touch($finalFilename); | ||
$fs->dumpFile($finalFilename, sprintf( | ||
'%s cd %s && php bin/console scheduler:execute --name %s', | ||
$task->getExpression(), | ||
$this->getProjectDir(), | ||
$task->getName(), | ||
)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function support(string $format): bool | ||
{ | ||
return 'crontab' === $format; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SchedulerBundle\Export; | ||
|
||
use SchedulerBundle\Task\TaskInterface; | ||
|
||
/** | ||
* @author Guillaume Loulier <contact@guillaumeloulier.fr> | ||
*/ | ||
interface ExporterInterface | ||
{ | ||
public function export(string $filename, TaskInterface $task): void; | ||
|
||
/** | ||
* Determine if the exporter support the current @param string $format. | ||
*/ | ||
public function support(string $format): bool; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.