From b486bec23bfeef277e05c2f07d618dcfff7b5bc2 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Mon, 29 Jan 2024 09:29:38 +0100 Subject: [PATCH] feat: Added new `cron` testing commands to test if your cron jobs are executing --- .../Console/GetCronLastExecDateCommand.php | 43 +++++++++++++++++ .../RegisterCronLastExecDateCommand.php | 46 +++++++++++++++++++ lib/RoadizCoreBundle/src/Entity/Setting.php | 16 +++---- 3 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 lib/RoadizCoreBundle/src/Console/GetCronLastExecDateCommand.php create mode 100644 lib/RoadizCoreBundle/src/Console/RegisterCronLastExecDateCommand.php diff --git a/lib/RoadizCoreBundle/src/Console/GetCronLastExecDateCommand.php b/lib/RoadizCoreBundle/src/Console/GetCronLastExecDateCommand.php new file mode 100644 index 00000000..e090c16d --- /dev/null +++ b/lib/RoadizCoreBundle/src/Console/GetCronLastExecDateCommand.php @@ -0,0 +1,43 @@ +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; + } +} diff --git a/lib/RoadizCoreBundle/src/Console/RegisterCronLastExecDateCommand.php b/lib/RoadizCoreBundle/src/Console/RegisterCronLastExecDateCommand.php new file mode 100644 index 00000000..20bed441 --- /dev/null +++ b/lib/RoadizCoreBundle/src/Console/RegisterCronLastExecDateCommand.php @@ -0,0 +1,46 @@ +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; + } +} diff --git a/lib/RoadizCoreBundle/src/Entity/Setting.php b/lib/RoadizCoreBundle/src/Entity/Setting.php index ead82037..c5caf3b4 100644 --- a/lib/RoadizCoreBundle/src/Entity/Setting.php +++ b/lib/RoadizCoreBundle/src/Entity/Setting.php @@ -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)) @@ -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; @@ -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; @@ -256,7 +256,7 @@ public function getType(): int * * @return $this */ - public function setType(int $type) + public function setType(int $type): self { $this->type = $type; @@ -290,7 +290,7 @@ public function isVisible(): bool * * @return $this */ - public function setVisible(bool $visible) + public function setVisible(bool $visible): self { $this->visible = $visible; @@ -310,7 +310,7 @@ public function getSettingGroup(): ?SettingGroup * * @return $this */ - public function setSettingGroup(?SettingGroup $settingGroup) + public function setSettingGroup(?SettingGroup $settingGroup): self { $this->settingGroup = $settingGroup; @@ -330,7 +330,7 @@ public function getDefaultValues(): ?string * * @return Setting */ - public function setDefaultValues(?string $defaultValues) + public function setDefaultValues(?string $defaultValues): self { $this->defaultValues = $defaultValues;