diff --git a/packages/application/Cron/Command/CronDumpToFileCommand.php b/packages/application/Cron/Command/CronDumpToFileCommand.php deleted file mode 100644 index f8ae0ff75..000000000 --- a/packages/application/Cron/Command/CronDumpToFileCommand.php +++ /dev/null @@ -1,40 +0,0 @@ -setName('draw:cron:dump-to-file') - ->setDescription('Dump the cron job configuration to a file compatible with crontab.') - ->addArgument('filePath', InputArgument::REQUIRED, 'The file path where to dump.') - ->addOption('override', null, InputOption::VALUE_NONE, 'If the file is present we override it.'); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $filePath = $input->getArgument('filePath'); - - if (is_file($filePath) && !$input->getOption('override')) { - throw new \RuntimeException(\sprintf('The file [%s] already exists. Remove the file or use option --override.', $filePath)); - } - - file_put_contents($filePath, $this->cronManager->dumpJobs()); - - return 0; - } -} diff --git a/packages/application/Cron/CronManager.php b/packages/application/Cron/CronManager.php deleted file mode 100644 index 9c2b7b46d..000000000 --- a/packages/application/Cron/CronManager.php +++ /dev/null @@ -1,46 +0,0 @@ -jobs[] = $job; - } - - /** - * Dump all the jobs to a crontab compatible string. - */ - public function dumpJobs(): string - { - $result = []; - foreach ($this->jobs as $job) { - if (!$job->getEnabled()) { - continue; - } - $jobData = $job->toArray(); - $mapping = []; - foreach ($jobData as $key => $value) { - $mapping['{'.$key.'}'] = $value; - } - - $cronLine = str_replace( - array_keys($mapping), - array_values($mapping), - self::JOB_STRING_PATTERN - ); - - $result[] = $cronLine; - } - - return trim(implode(\PHP_EOL, $result)).\PHP_EOL; - } -} diff --git a/packages/application/Cron/Job.php b/packages/application/Cron/Job.php deleted file mode 100644 index 97b3ab403..000000000 --- a/packages/application/Cron/Job.php +++ /dev/null @@ -1,112 +0,0 @@ -/dev/null 2>&1'; - - public function __construct( - /** - * The name of the job for reference. - */ - private string $name, - /** - * The command to execute. - */ - private string $command, - /** - * The cron execution expression configuration. - */ - private string $expression = '* * * * *', - /** - * If the job is enabled or not. - */ - private bool $enabled = true, - /** - * The description of the job. - */ - private ?string $description = null, - ) { - } - - public function getName(): string - { - return $this->name; - } - - public function setName(string $name): self - { - $this->name = $name; - - return $this; - } - - public function getDescription(): ?string - { - return $this->description; - } - - public function setDescription(string $description): self - { - $this->description = $description; - - return $this; - } - - public function getExpression(): string - { - return $this->expression; - } - - public function setExpression(string $expression): self - { - $this->expression = $expression; - - return $this; - } - - public function getCommand(): string - { - return $this->command; - } - - public function setCommand(string $command): self - { - $this->command = $command; - - return $this; - } - - public function getEnabled(): bool - { - return $this->enabled; - } - - public function setEnabled(bool $enabled): self - { - $this->enabled = $enabled; - - return $this; - } - - public function getOutput(): string - { - return $this->output; - } - - public function setOutput(string $output): self - { - $this->output = $output; - - return $this; - } - - public function toArray(): array - { - return get_object_vars($this); - } -} diff --git a/packages/application/DependencyInjection/CronIntegration.php b/packages/application/DependencyInjection/CronIntegration.php deleted file mode 100644 index 211369b7c..000000000 --- a/packages/application/DependencyInjection/CronIntegration.php +++ /dev/null @@ -1,106 +0,0 @@ -registerClasses( - $loader, - $namespace = 'Draw\\Component\\Application\\Cron\\', - $directory = \dirname((new \ReflectionClass(CronManager::class))->getFileName()), - [ - $directory.'/Job.php', - ] - ); - - $cronManagerDefinition = $container->getDefinition(CronManager::class); - foreach ($config['jobs'] as $jobData) { - $jobDefinition = new Definition( - Job::class, - [ - $jobData['name'], - $jobData['command'], - $jobData['expression'], - $jobData['enabled'], - $jobData['description'], - ] - ); - - $jobDefinition->addMethodCall('setOutput', [$jobData['output']]); - - $cronManagerDefinition->addMethodCall('addJob', [$jobDefinition]); - } - - $this->renameDefinitions( - $container, - $namespace, - 'draw.cron.' - ); - } - - public function addConfiguration(ArrayNodeDefinition $node): void - { - $node - ->children() - ->arrayNode('jobs') - ->defaultValue([]) - ->beforeNormalization() - ->always(function ($config) { - foreach ($config as $name => $configuration) { - if (!isset($configuration['name'])) { - $config[$name]['name'] = $name; - } - } - - return $config; - }) - ->end() - ->useAttributeAsKey('name', false) - ->arrayPrototype() - ->children() - ->scalarNode('name') - ->validate() - ->ifTrue(fn ($value) => \is_int($value)) - ->thenInvalid('You must specify a name for the job. Can be via the attribute or the key.') - ->end() - ->isRequired() - ->end() - ->scalarNode('description') - ->defaultNull() - ->end() - ->scalarNode('expression') - ->isRequired() - ->end() - ->scalarNode('output') - ->defaultValue('>/dev/null 2>&1') - ->end() - ->scalarNode('command') - ->isRequired() - ->end() - ->booleanNode('enabled') - ->defaultValue(true) - ->end() - ->end() - ->end() - ->end() - ->end(); - } -} diff --git a/packages/application/Tests/Command/CronDumpToFileCommandTest.php b/packages/application/Tests/Command/CronDumpToFileCommandTest.php deleted file mode 100644 index 42d85990f..000000000 --- a/packages/application/Tests/Command/CronDumpToFileCommandTest.php +++ /dev/null @@ -1,95 +0,0 @@ -command = new CronDumpToFileCommand( - $this->cronManager = $this->createMock(CronManager::class) - ); - } - - public function getCommandName(): string - { - return 'draw:cron:dump-to-file'; - } - - public static function provideTestArgument(): iterable - { - yield ['filePath', InputArgument::REQUIRED]; - } - - public static function provideTestOption(): iterable - { - yield ['override', null, InputOption::VALUE_NONE]; - } - - public function testExecuteNewFile(): void - { - $this->cronManager - ->expects(static::once()) - ->method('dumpJobs') - ->willReturn('Output'); - - $filePath = sys_get_temp_dir().'/'.uniqid().'.txt'; - register_shutdown_function('unlink', $filePath); - $this - ->execute(['filePath' => $filePath]) - ->test(CommandDataTester::create()); - - static::assertSame('Output', file_get_contents($filePath)); - } - - public function testExecuteNewFileOverride(): void - { - $this->cronManager - ->expects(static::once()) - ->method('dumpJobs') - ->willReturn('Output'); - - $filePath = sys_get_temp_dir().'/'.uniqid().'.txt'; - file_put_contents($filePath, 'Before'); - register_shutdown_function('unlink', $filePath); - $this - ->execute(['filePath' => $filePath, '--override' => '1']) - ->test(CommandDataTester::create()); - - static::assertSame('Output', file_get_contents($filePath)); - } - - public function testExecuteNewFileNoOverrideException(): void - { - $this->cronManager - ->expects(static::never()) - ->method('dumpJobs'); - - $filePath = sys_get_temp_dir().'/'.uniqid().'.txt'; - touch($filePath); - register_shutdown_function('unlink', $filePath); - - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage(\sprintf( - 'The file [%s] already exists. Remove the file or use option --override.', - $filePath - )); - - $this->execute(['filePath' => $filePath]); - } -} diff --git a/packages/application/Tests/Cron/CronManagerTest.php b/packages/application/Tests/Cron/CronManagerTest.php deleted file mode 100644 index 984258554..000000000 --- a/packages/application/Tests/Cron/CronManagerTest.php +++ /dev/null @@ -1,59 +0,0 @@ -service = new CronManager(); - } - - public function testDumpJobs(): void - { - $job = new Job('Job name', 'echo "test"'); - $this->service->addJob($job); - $cronTab = "#Description: \n* * * * * echo \"test\" >/dev/null 2>&1\n"; - - static::assertSame( - $cronTab, - $this->service->dumpJobs() - ); - } - - public function testDumpJobsTwoJobs(): void - { - $job = new Job('Job name', 'echo "test"'); - $this->service->addJob($job); - - $job = new Job('Job 2', 'echo "test"', '*/5 * * * *', true, 'Job every 5 minutes'); - $this->service->addJob($job); - $cronTab = "#Description: \n* * * * * echo \"test\" >/dev/null 2>&1\n\n"; - $cronTab .= "#Description: Job every 5 minutes\n*/5 * * * * echo \"test\" >/dev/null 2>&1\n"; - - static::assertSame( - $cronTab, - $this->service->dumpJobs() - ); - } - - public function testDumpJobsDisabled(): void - { - $job = new Job('Job 2', 'echo "test"'); - $job->setEnabled(false); - $this->service->addJob($job); - - static::assertSame( - \PHP_EOL, - $this->service->dumpJobs() - ); - } -} diff --git a/packages/application/Tests/Cron/JobTest.php b/packages/application/Tests/Cron/JobTest.php deleted file mode 100644 index 2d3b44ee7..000000000 --- a/packages/application/Tests/Cron/JobTest.php +++ /dev/null @@ -1,127 +0,0 @@ -entity = new Job(self::DEFAULT_NAME, self::DEFAULT_COMMAND); - } - - public function testNameMutator(): void - { - static::assertSame(self::DEFAULT_NAME, $this->entity->getName()); - - static::assertSame( - $this->entity, - $this->entity->setName($value = uniqid()) - ); - - static::assertSame( - $value, - $this->entity->getName() - ); - } - - public function testCommandMutator(): void - { - static::assertSame(self::DEFAULT_COMMAND, $this->entity->getCommand()); - - static::assertSame( - $this->entity, - $this->entity->setCommand($value = uniqid()) - ); - - static::assertSame( - $value, - $this->entity->getCommand() - ); - } - - public function testDescriptionMutator(): void - { - static::assertNull($this->entity->getDescription()); - - static::assertSame( - $this->entity, - $this->entity->setDescription($value = uniqid()) - ); - - static::assertSame( - $value, - $this->entity->getDescription() - ); - } - - public function testEnabledMutator(): void - { - static::assertTrue( - $this->entity->getEnabled() - ); - - static::assertSame( - $this->entity, - $this->entity->setEnabled(false) - ); - - static::assertFalse( - $this->entity->getEnabled() - ); - } - - public function testExpressionMutator(): void - { - static::assertSame('* * * * *', $this->entity->getExpression()); - - static::assertSame( - $this->entity, - $this->entity->setExpression($value = uniqid()) - ); - - static::assertSame( - $value, - $this->entity->getExpression() - ); - } - - public function testOutputMutator(): void - { - static::assertSame('>/dev/null 2>&1', $this->entity->getOutput()); - - static::assertSame( - $this->entity, - $this->entity->setOutput($value = uniqid()) - ); - - static::assertSame( - $value, - $this->entity->getOutput() - ); - } - - public function testToArray(): void - { - static::assertEquals( - [ - 'name' => 'name', - 'description' => '', - 'expression' => '* * * * *', - 'command' => 'ls', - 'enabled' => true, - 'output' => '>/dev/null 2>&1', - ], - $this->entity->toArray() - ); - } -} diff --git a/packages/application/Tests/DependencyInjection/CronIntegrationTest.php b/packages/application/Tests/DependencyInjection/CronIntegrationTest.php deleted file mode 100644 index a7967d285..000000000 --- a/packages/application/Tests/DependencyInjection/CronIntegrationTest.php +++ /dev/null @@ -1,110 +0,0 @@ - [], - ]; - } - - public static function provideTestLoad(): iterable - { - yield [ - [ - [ - 'jobs' => [ - 'acme_cron' => [ - 'description' => 'Execute acme:command every 5 minutes', - 'command' => 'acme:command', - 'expression' => '*/5 * * * *', - 'enabled' => false, - ], - ], - ], - ], - [ - new ServiceConfiguration( - 'draw.cron.command.cron_dump_to_file_command', - [ - CronDumpToFileCommand::class, - ] - ), - new ServiceConfiguration( - 'draw.cron.cron_manager', - [ - CronManager::class, - ], - function (Definition $definition): void { - $methodCalls = $definition->getMethodCalls(); - - static::assertCount(1, $methodCalls); - - static::assertSame( - 'addJob', - $methodCalls[0][0] - ); - - static::assertCount( - 1, - $methodCalls[0][1] - ); - - $jobDefinition = $methodCalls[0][1][0]; - - static::assertInstanceOf(Definition::class, $jobDefinition); - - static::assertSame( - [ - 'acme_cron', - 'acme:command', - '*/5 * * * *', - false, - 'Execute acme:command every 5 minutes', - ], - $jobDefinition->getArguments() - ); - - static::assertSame( - [ - [ - 'setOutput', - [ - '>/dev/null 2>&1', - ], - ], - ], - $jobDefinition->getMethodCalls() - ); - } - ), - ], - ]; - } -} diff --git a/packages/framework-extra-bundle/DependencyInjection/DrawFrameworkExtraExtension.php b/packages/framework-extra-bundle/DependencyInjection/DrawFrameworkExtraExtension.php index cdbfd755e..fff620b86 100644 --- a/packages/framework-extra-bundle/DependencyInjection/DrawFrameworkExtraExtension.php +++ b/packages/framework-extra-bundle/DependencyInjection/DrawFrameworkExtraExtension.php @@ -3,7 +3,6 @@ namespace Draw\Bundle\FrameworkExtraBundle\DependencyInjection; use Draw\Component\Application\DependencyInjection\ConfigurationIntegration; -use Draw\Component\Application\DependencyInjection\CronIntegration; use Draw\Component\Application\DependencyInjection\FeatureIntegration; use Draw\Component\Application\DependencyInjection\SystemMonitoringIntegration; use Draw\Component\Application\DependencyInjection\VersioningIntegration; @@ -52,7 +51,6 @@ private function provideExtensionClasses(): array AwsToolKitIntegration::class, ConfigurationIntegration::class, ConsoleIntegration::class, - CronIntegration::class, CronJobIntegration::class, DoctrineExtraIntegration::class, EntityMigratorIntegration::class, diff --git a/packages/framework-extra-bundle/doc/APPLICATION.md b/packages/framework-extra-bundle/doc/APPLICATION.md index 5fe1c6e71..1330ddd6c 100644 --- a/packages/framework-extra-bundle/doc/APPLICATION.md +++ b/packages/framework-extra-bundle/doc/APPLICATION.md @@ -1,57 +1,3 @@ # Application -This is for the integration of [draw/application](https://github.com/mpoiriert/application) - -## Cron - -You can configure cron base on the environment configuration or an **enabled** setting. - -This is mainly useful if you want to configure the cron in the project and during your deployment flow you call the -command to dump the cron file with the proper environment configure. - -**It's not intent to run the cron, it's just to allow to centralize configuration.** - -### Configuration - -Here is a sample of the configuration: - -```YAML -parameters: - cron.console.execution: "www-data php %kernel.project_dir%/bin/console" - cron.context.enabled: true - -draw_framework_extra: - cron: - jobs: - acme_cron: - description: "Execute acme:command every 5 minutes" - command: "%cron.console.execution% acme:command" - expression: "*/5 * * * *" - output: ">/dev/null 2>&1" #This is the default value - enabled: "%cron.context.enabled%" -``` - -The command ```draw:cron:dump-to-file``` will dump something like this - -``` -#Description: Execute acme:command every 5 minutes -* * * * * www-data php /var/www/acme/bin/console acme:command >/dev/null 2>&1 -``` - -### Command - -The command to dump the file is ```draw:cron:dump-to-file```. - -If you want to dump it the first time or in a new file path you can simply do this: - -``` -bin/console draw:cron:dump-to-file /path/to/the/file -``` - -It will throw an exception if the file already exists. If you want to override it, simply use the --override option. - -``` -bin/console draw:cron:dump-to-file /path/to/the/file --override -``` - -Normally you should integrate this in your deployment pipeline. \ No newline at end of file +This is for the integration of [draw/application](https://github.com/mpoiriert/application) \ No newline at end of file