-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added new
cron
testing commands to test if your cron jobs are…
… executing
- Loading branch information
1 parent
5316137
commit b486bec
Showing
3 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
lib/RoadizCoreBundle/src/Console/GetCronLastExecDateCommand.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,43 @@ | ||
<?php | ||
|
||
namespace RZ\Roadiz\CoreBundle\Console; | ||
|
||
use RZ\Roadiz\CoreBundle\Entity\Setting; | ||
use RZ\Roadiz\CoreBundle\Repository\SettingRepository; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand( | ||
name: 'cron:get-last-exec-date', | ||
description: 'Fetch last execution date of cron job into database.', | ||
)] | ||
final class GetCronLastExecDateCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly SettingRepository $settingRepository, | ||
string $name = null | ||
) { | ||
parent::__construct($name); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$setting = $this->settingRepository->findOneByName('cron_last_exec_date'); | ||
if (!($setting instanceof Setting)) { | ||
$io->warning('Last execution date of cron job has not been persisted yet.'); | ||
return Command::FAILURE; | ||
} | ||
|
||
$io->success(sprintf( | ||
'Last execution date of cron job is %s.', | ||
$setting->getRawValue() | ||
)); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
lib/RoadizCoreBundle/src/Console/RegisterCronLastExecDateCommand.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,46 @@ | ||
<?php | ||
|
||
namespace RZ\Roadiz\CoreBundle\Console; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use RZ\Roadiz\CoreBundle\Entity\Setting; | ||
use RZ\Roadiz\CoreBundle\Repository\SettingRepository; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand( | ||
name: 'cron:set-last-exec-date', | ||
description: 'Persist last execution date of cron job into database.', | ||
)] | ||
final class RegisterCronLastExecDateCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly SettingRepository $settingRepository, | ||
private readonly ManagerRegistry $managerRegistry, | ||
string $name = null | ||
) { | ||
parent::__construct($name); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$manager = $this->managerRegistry->getManager(); | ||
$parameter = $this->settingRepository->findOneByName('cron_last_exec_date'); | ||
if (null === $parameter) { | ||
$parameter = new Setting(); | ||
$parameter->setName('cron_last_exec_date'); | ||
$manager->persist($parameter); | ||
} | ||
|
||
$parameter->setValue(new \DateTimeImmutable()); | ||
$manager->flush(); | ||
$io->success('Last execution date of cron job has been persisted.'); | ||
|
||
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