Skip to content

Commit

Permalink
feat: Added new cron testing commands to test if your cron jobs are…
Browse files Browse the repository at this point in the history
… executing
  • Loading branch information
ambroisemaupate committed Jan 29, 2024
1 parent 5316137 commit b486bec
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 8 deletions.
43 changes: 43 additions & 0 deletions lib/RoadizCoreBundle/src/Console/GetCronLastExecDateCommand.php
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;
}
}
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;
}
}
16 changes: 8 additions & 8 deletions lib/RoadizCoreBundle/src/Entity/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function getName(): string
*
* @return $this
*/
public function setName(?string $name)
public function setName(?string $name): self
{
$this->name = trim(\mb_strtolower($name ?? ''));
$this->name = (new UnicodeString($this->name))
Expand Down Expand Up @@ -178,7 +178,7 @@ public function getRawValue(): ?string
* @throws \Exception
*/
#[SymfonySerializer\Ignore]
public function getValue()
public function getValue(): string|bool|\DateTime|int|null
{
if ($this->isEncrypted()) {
$value = $this->clearValue;
Expand All @@ -203,11 +203,11 @@ public function getValue()
}

/**
* @param null|mixed $value
* @param mixed $value
*
* @return $this
*/
public function setValue($value)
public function setValue(mixed $value): self
{
if (null === $value) {
$this->value = null;
Expand Down Expand Up @@ -256,7 +256,7 @@ public function getType(): int
*
* @return $this
*/
public function setType(int $type)
public function setType(int $type): self
{
$this->type = $type;

Expand Down Expand Up @@ -290,7 +290,7 @@ public function isVisible(): bool
*
* @return $this
*/
public function setVisible(bool $visible)
public function setVisible(bool $visible): self
{
$this->visible = $visible;

Expand All @@ -310,7 +310,7 @@ public function getSettingGroup(): ?SettingGroup
*
* @return $this
*/
public function setSettingGroup(?SettingGroup $settingGroup)
public function setSettingGroup(?SettingGroup $settingGroup): self
{
$this->settingGroup = $settingGroup;

Expand All @@ -330,7 +330,7 @@ public function getDefaultValues(): ?string
*
* @return Setting
*/
public function setDefaultValues(?string $defaultValues)
public function setDefaultValues(?string $defaultValues): self
{
$this->defaultValues = $defaultValues;

Expand Down

0 comments on commit b486bec

Please sign in to comment.