-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CronJob/FrameworkExtraBundle] Support queue cron post command execution
- Loading branch information
Showing
14 changed files
with
266 additions
and
27 deletions.
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
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
63 changes: 63 additions & 0 deletions
63
packages/cron-job/EventListener/PostExecutionQueueCronJobListener.php
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,63 @@ | ||
<?php | ||
|
||
namespace Draw\Component\CronJob\EventListener; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Draw\Component\CronJob\CronJobProcessor; | ||
use Draw\Component\CronJob\Entity\CronJob; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Event\ConsoleTerminateEvent; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
|
||
/** | ||
* This command listener allow to queue a cron job by name after the execution if it's a success. | ||
* | ||
* Example: | ||
* console/bin acme:purge-database --draw-draw-post-execution-queue-cron-job | ||
*/ | ||
class PostExecutionQueueCronJobListener | ||
{ | ||
final public const OPTION_POST_EXECUTION_QUEUE_CRON_JOB = 'draw-post-execution-queue-cron-job'; | ||
|
||
public function __construct( | ||
private CronJobProcessor $cronJobProcessor, | ||
private ManagerRegistry $managerRegistry, | ||
private ?LoggerInterface $logger = new NullLogger(), | ||
) { | ||
} | ||
|
||
#[AsEventListener(priority: -1000)] | ||
public function triggerCronJob(ConsoleTerminateEvent $event): void | ||
{ | ||
if (Command::SUCCESS !== $event->getExitCode()) { | ||
return; | ||
} | ||
|
||
$input = $event->getInput(); | ||
|
||
if (!$input->hasOption(static::OPTION_POST_EXECUTION_QUEUE_CRON_JOB)) { | ||
return; | ||
} | ||
|
||
$cronJobRepository = $this->managerRegistry->getRepository(CronJob::class); | ||
|
||
$cronJobNames = $input->getOption(static::OPTION_POST_EXECUTION_QUEUE_CRON_JOB); | ||
|
||
foreach ($cronJobNames as $cronJobName) { | ||
$cronJob = $cronJobRepository | ||
->findOneBy(['name' => $cronJobName]); | ||
|
||
if (null === $cronJob) { | ||
$this->logger->error(sprintf('Cron job "%s" could not be found.', $cronJobName)); | ||
|
||
continue; | ||
} | ||
|
||
$this->logger->info(sprintf('Queueing cron job "%s"...', $cronJob->getName())); | ||
|
||
$this->cronJobProcessor->queue($cronJob, true); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...framework-extra-bundle/DependencyInjection/Compiler/AddPostCronJobExecutionOptionPass.php
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,37 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler; | ||
|
||
use Draw\Component\CronJob\EventListener\PostExecutionQueueCronJobListener; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
|
||
class AddPostCronJobExecutionOptionPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
try { | ||
$container->findDefinition(PostExecutionQueueCronJobListener::class); | ||
} catch (ServiceNotFoundException) { | ||
return; | ||
} | ||
|
||
foreach (array_keys($container->findTaggedServiceIds('console.command')) as $serviceId) { | ||
$definition = $container->getDefinition($serviceId); | ||
if (!$definition->isAbstract()) { | ||
$definition | ||
->addMethodCall( | ||
'addOption', | ||
[ | ||
PostExecutionQueueCronJobListener::OPTION_POST_EXECUTION_QUEUE_CRON_JOB, | ||
null, | ||
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, | ||
'Queue does cron job by name after execution of the command.', | ||
] | ||
); | ||
} | ||
} | ||
} | ||
} |
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
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
84 changes: 84 additions & 0 deletions
84
tests/CronJob/EventListener/PostExecutionQueueCronJobListenerTest.php
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,84 @@ | ||
<?php | ||
|
||
namespace App\Tests\CronJob\EventListener; | ||
|
||
use App\Command\NullCommand; | ||
use Draw\Bundle\TesterBundle\Messenger\TransportTester; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowiredCompletionAwareInterface; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireTransportTester; | ||
use Draw\Component\Core\FilterExpression\Expression\Expression; | ||
use Draw\Component\CronJob\Message\ExecuteCronJobMessage; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Output\NullOutput; | ||
|
||
class PostExecutionQueueCronJobListenerTest extends KernelTestCase implements AutowiredCompletionAwareInterface | ||
{ | ||
#[AutowireService] | ||
private NullCommand $command; | ||
|
||
#[AutowireTransportTester('async_high_priority')] | ||
private TransportTester $transportTester; | ||
|
||
private Application $application; | ||
|
||
public function postAutowire(): void | ||
{ | ||
$this->application = new Application(static::$kernel); | ||
$this->application->add($this->command); | ||
$this->application->setAutoExit(false); | ||
} | ||
|
||
public function testTriggerCronJob(): void | ||
{ | ||
$result = $this->application | ||
->run( | ||
new ArrayInput([ | ||
'app:null', | ||
'--draw-post-execution-queue-cron-job' => ['test', 'test'], | ||
]), | ||
new NullOutput() | ||
); | ||
|
||
static::assertSame( | ||
Command::SUCCESS, | ||
$result | ||
); | ||
|
||
$this->transportTester | ||
->assertMessageMatch( | ||
ExecuteCronJobMessage::class, | ||
Expression::andWhereEqual([ | ||
'execution.cronJob.name' => 'test', | ||
]), | ||
2 | ||
); | ||
} | ||
|
||
public function testTriggerCronJobError(): void | ||
{ | ||
$result = $this->application | ||
->run( | ||
new ArrayInput([ | ||
'app:null', | ||
'--exit-code' => Command::FAILURE, | ||
'--draw-post-execution-queue-cron-job' => ['test', 'test'], | ||
]), | ||
new NullOutput() | ||
); | ||
|
||
static::assertSame( | ||
Command::FAILURE, | ||
$result | ||
); | ||
|
||
$this->transportTester | ||
->assertMessageMatch( | ||
ExecuteCronJobMessage::class, | ||
count: 0 | ||
); | ||
} | ||
} |
Oops, something went wrong.