-
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.
Resolved code review feedback, added commands, completed message hand…
…ler.
- Loading branch information
1 parent
663a50c
commit 18d6734
Showing
12 changed files
with
337 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Draw\Component\CronJob\Command; | ||
|
||
use Draw\Component\CronJob\CronJobProcessor; | ||
use Draw\Component\CronJob\Entity\CronJob; | ||
use Symfony\Bridge\Doctrine\ManagerRegistry; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand(name: 'draw:cron-job:queue-by-name')] | ||
class QueueCronJobByNameCommand extends Command | ||
{ | ||
public function __construct( | ||
private ManagerRegistry $managerRegistry, | ||
private CronJobProcessor $cronJobProcessor, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Queues cron job by name') | ||
->addArgument('name', InputArgument::REQUIRED, 'Cron job name'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$cronJob = $this->managerRegistry | ||
->getRepository(CronJob::class) | ||
->findOneBy(['name' => $input->getArgument('name')]); | ||
|
||
if (null === $cronJob) { | ||
$io->error('Cron job could not be found.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$io->section('Queueing cron job...'); | ||
|
||
$this->cronJobProcessor->queue($cronJob, true); | ||
|
||
$io->section('Cron job successfully queued.'); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Draw\Component\CronJob\Command; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Draw\Component\CronJob\CronJobProcessor; | ||
use Draw\Component\CronJob\Entity\CronJob; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand(name: 'draw:cron-job:queue-due')] | ||
class QueueDueCronJobsCommand extends Command | ||
{ | ||
public function __construct( | ||
private ManagerRegistry $managerRegistry, | ||
private CronJobProcessor $cronJobProcessor, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Queues due cron jobs'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->section('Queueing cron jobs...'); | ||
|
||
$cronJobs = array_map( | ||
static fn(CronJob $cronJob): bool => $cronJob->isDue(), | ||
$this->managerRegistry | ||
->getRepository(CronJob::class) | ||
->findAll() | ||
); | ||
|
||
$progress = $io->createProgressBar(\count($cronJobs)); | ||
$progress->setFormat(ProgressBar::FORMAT_DEBUG); | ||
|
||
foreach ($cronJobs as $cronJob) { | ||
$this->cronJobProcessor->queue($cronJob); | ||
|
||
$progress->advance(); | ||
} | ||
|
||
$progress->finish(); | ||
|
||
$io->newLine(2); | ||
$io->success('Cron jobs successfully queued...'); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Draw\Component\CronJob; | ||
|
||
use Draw\Component\CronJob\Entity\CronJob; | ||
use Draw\Component\CronJob\Entity\CronJobExecution; | ||
|
||
class CronJobExecutionFactory | ||
{ | ||
public function create(CronJob $cronJob, bool $force = false): CronJobExecution | ||
{ | ||
return (new CronJobExecution()) | ||
->setCronJob($cronJob) | ||
->setRequestedAt(new \DateTimeImmutable()) | ||
->setForce($force); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...s/framework-extra-bundle/Tests/DependencyInjection/Integration/CronJobIntegrationTest.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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Draw\Bundle\FrameworkExtraBundle\Tests\DependencyInjection\Integration; | ||
|
||
use Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration\CronJobIntegration; | ||
use Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration\IntegrationInterface; | ||
use Draw\Component\CronJob\CronJobProcessor; | ||
use Draw\Component\CronJob\MessageHandler\ExecuteCronJobMessageHandler; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
#[CoversClass(CronJobIntegration::class)] | ||
class CronJobIntegrationTest extends IntegrationTestCase | ||
{ | ||
public function createIntegration(): IntegrationInterface | ||
{ | ||
return new CronJobIntegration(); | ||
} | ||
|
||
public function getConfigurationSectionName(): string | ||
{ | ||
return 'cron_job'; | ||
} | ||
|
||
public function getDefaultConfiguration(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public static function provideTestLoad(): iterable | ||
{ | ||
yield [ | ||
[], | ||
[ | ||
new ServiceConfiguration( | ||
'draw.cron_job.cron_job_processor', | ||
[ | ||
CronJobProcessor::class, | ||
] | ||
), | ||
new ServiceConfiguration( | ||
'draw.cron_job.message_handler.execute_cron_job_message_handler', | ||
[ | ||
ExecuteCronJobMessageHandler::class, | ||
] | ||
) | ||
], | ||
[], | ||
]; | ||
} | ||
} |
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/sonata-integration-bundle/CronJob/Admin/CronJobAdmin.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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Draw\Bundle\SonataIntegrationBundle\CronJob\Admin; | ||
|
||
use Sonata\AdminBundle\Admin\AbstractAdmin; | ||
use Sonata\AdminBundle\Datagrid\DatagridMapper; | ||
use Sonata\AdminBundle\Datagrid\ListMapper; | ||
use Sonata\AdminBundle\Form\FormMapper; | ||
use Sonata\AdminBundle\Show\ShowMapper; | ||
|
||
class CronJobAdmin extends AbstractAdmin | ||
{ | ||
protected function configureDatagridFilters(DatagridMapper $filter): void | ||
{ | ||
$filter | ||
->add('id') | ||
->add('name') | ||
->add('command') | ||
->add('active'); | ||
|
||
} | ||
|
||
protected function configureListFields(ListMapper $list): void | ||
{ | ||
$list | ||
->addIdentifier('name') | ||
->add('command') | ||
->add('schedule') | ||
->add('active', null, ['editable' => true]) | ||
->add('timeToLive') | ||
->add('priority'); | ||
} | ||
|
||
protected function configureFormFields(FormMapper $form): void | ||
{ | ||
$form | ||
->add('name') | ||
->add( | ||
'command', | ||
null, | ||
[ | ||
'help' => 'Enter the full command to run excluding stdOut and stdErr directive (... 2>&1 | logger -t ...)<p>Parameters bag is available. Use like %kernel.project_dir%</p>', | ||
] | ||
) | ||
->add('schedule') | ||
->add('active') | ||
->add('timeToLive') | ||
->add('priority'); | ||
} | ||
|
||
protected function configureShowFields(ShowMapper $show): void | ||
{ | ||
$show | ||
->add('name') | ||
->add('command') | ||
->add('schedule') | ||
->add('active', null, ['editable' => true]) | ||
->add('timeToLive') | ||
->add('priority'); | ||
} | ||
} |
Oops, something went wrong.