Skip to content
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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- main
schedule:
- cron: "0 0 * * *"
- cron: "5 0 * * *"

jobs:
php-cs-fixer:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/infection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- main
schedule:
- cron: "0 0 * * *"
- cron: "5 0 * * *"

jobs:
infection:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- main
schedule:
- cron: "0 0 * * *"
- cron: "5 0 * * *"

jobs:
phpunit:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- main
schedule:
- cron: "0 0 * * *"
- cron: "5 0 * * *"

jobs:
phpstan:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- main
schedule:
- cron: "0 0 * * *"
- cron: "5 0 * * *"

jobs:
security:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- main
schedule:
- cron: "0 0 * * *"
- cron: "5 0 * * *"

jobs:
phpstan:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ summary.log
.php-cs-fixer.cache

# Tests
tests/**/assets/
.phpunit.result.cache
tests/.assets/**/*.json

Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"SchedulerBundle\\Event\\": "src/Event/",
"SchedulerBundle\\EventListener\\": "src/EventListener/",
"SchedulerBundle\\Exception\\": "src/Exception/",
"SchedulerBundle\\Export\\": "src/Export/",
"SchedulerBundle\\Expression\\": "src/Expression/",
"SchedulerBundle\\Fiber\\": "src/Fiber/",
"SchedulerBundle\\Messenger\\": "src/Messenger/",
Expand Down Expand Up @@ -74,6 +75,7 @@
"Tests\\SchedulerBundle\\DependencyInjection\\Assets\\": "tests/DependencyInjection/Assets/",
"Tests\\SchedulerBundle\\Event\\": "tests/Event/",
"Tests\\SchedulerBundle\\EventListener\\": "tests/EventListener/",
"Tests\\SchedulerBundle\\Export\\": "tests/Export/",
"Tests\\SchedulerBundle\\Expression\\": "tests/Expression/",
"Tests\\SchedulerBundle\\Messenger\\": "tests/Messenger/",
"Tests\\SchedulerBundle\\Middleware\\": "tests/Middleware/",
Expand Down
86 changes: 86 additions & 0 deletions src/Command/ExportCommand.php
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;
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ExporterRegistryInterface $exporterRegistry,
SchedulerInterface $scheduler
) {
$this->exporterRegistry = $exporterRegistry;
$this->scheduler = $scheduler;
private ExporterRegistryInterface $exporterRegistry,
private SchedulerInterface $scheduler
) {


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;
}
}
42 changes: 42 additions & 0 deletions src/DependencyInjection/SchedulerBundleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use SchedulerBundle\Command\DebugProbeCommand;
use SchedulerBundle\Command\ExecuteExternalProbeCommand;
use SchedulerBundle\Command\ExecuteTaskCommand;
use SchedulerBundle\Command\ExportCommand;
use SchedulerBundle\Command\ListFailedTasksCommand;
use SchedulerBundle\Command\ListTasksCommand;
use SchedulerBundle\Command\RebootSchedulerCommand;
Expand All @@ -31,6 +32,10 @@
use SchedulerBundle\EventListener\TaskLoggerSubscriber;
use SchedulerBundle\EventListener\TaskSubscriber;
use SchedulerBundle\EventListener\WorkerLifecycleSubscriber;
use SchedulerBundle\Export\CronTabExporter;
use SchedulerBundle\Export\ExporterInterface;
use SchedulerBundle\Export\ExporterRegistry;
use SchedulerBundle\Export\ExporterRegistryInterface;
use SchedulerBundle\Expression\BuilderInterface;
use SchedulerBundle\Expression\ComputedExpressionBuilder;
use SchedulerBundle\Expression\CronExpressionBuilder;
Expand Down Expand Up @@ -185,6 +190,7 @@ final class SchedulerBundleExtension extends Extension
private const EXECUTION_POLICY_TAG = 'scheduler.execution_policy';
private const WORKER_TAG = 'scheduler.worker';
private const SCHEDULER_MIDDLEWARE_TAG = 'scheduler.middleware';
private const TASK_EXPORTER_TAG = 'scheduler.task_exporter';

public function load(array $configs, ContainerBuilder $container): void
{
Expand All @@ -208,6 +214,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->registerExpressionFactoryAndPolicies($container);
$this->registerBuilders($container);
$this->registerRunners($container);
$this->registerExportTools($container);
$this->registerNormalizer($container);
$this->registerMessengerTools($container);
$this->registerSubscribers($container);
Expand Down Expand Up @@ -262,6 +269,7 @@ private function registerAutoConfigure(ContainerBuilder $container): void
$container->registerForAutoconfiguration(TaskBagInterface::class)->addTag('scheduler.task_bag');
$container->registerForAutoconfiguration(SchedulerAwareInterface::class)->addTag('scheduler.entry_point');
$container->registerForAutoconfiguration(ExecutionPolicyInterface::class)->addTag(self::EXECUTION_POLICY_TAG);
$container->registerForAutoconfiguration(ExporterInterface::class)->addTag(self::TASK_EXPORTER_TAG);
}

private function registerConfigurationFactories(ContainerBuilder $container): void
Expand Down Expand Up @@ -599,6 +607,17 @@ private function registerCommands(ContainerBuilder $container): void
])
;

$container->register(ExportCommand::class, ExportCommand::class)
->setArguments([
new Reference(ExporterRegistryInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE),
new Reference(SchedulerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE),
])
->addTag('console.command')
->addTag('container.preload', [
'class' => ExportCommand::class,
])
;

$container->register(ListFailedTasksCommand::class, ListFailedTasksCommand::class)
->setArguments([
new Reference(WorkerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE),
Expand Down Expand Up @@ -961,6 +980,29 @@ private function registerRunners(ContainerBuilder $container): void
;
}

public function registerExportTools(ContainerBuilder $container): void
{
$container->register(ExporterRegistry::class, ExporterRegistry::class)
->setArguments([
new TaggedIteratorArgument(self::TASK_EXPORTER_TAG),
])
->addTag('container.preload', [
'class' => ExporterRegistry::class,
])
;
$container->setAlias(ExporterRegistryInterface::class, ExporterRegistry::class);

$container->register(CronTabExporter::class, CronTabExporter::class)
->setArguments([
$container->getParameter('kernel.project_dir'),
])
->addTag(self::TASK_EXPORTER_TAG)
->addTag('container.preload', [
'class' => CronTabExporter::class,
])
;
}

private function registerNormalizer(ContainerBuilder $container): void
{
$container->register(TaskNormalizer::class, TaskNormalizer::class)
Expand Down
23 changes: 23 additions & 0 deletions src/Export/AbstractExporter.php
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private string $projectDir;


public function __construct(string $projectDir)
{
$this->projectDir = $projectDir;
}
Comment on lines +14 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function __construct(string $projectDir)
{
$this->projectDir = $projectDir;
}
public function __construct(private string $projectDir)
{
}


protected function getProjectDir(): string
{
return $this->projectDir;
}
}
45 changes: 45 additions & 0 deletions src/Export/CronTabExporter.php
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;
}
}
20 changes: 20 additions & 0 deletions src/Export/ExporterInterface.php
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;
}
Loading