From b5584d3eecea8b5dff525561727e81bf890c2b89 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 24 Apr 2024 14:28:02 +0200 Subject: [PATCH 1/4] Add flush param to ProcessMonitor::start + deal with null process for the end function --- src/Monitoring/ProcessMonitor.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Monitoring/ProcessMonitor.php b/src/Monitoring/ProcessMonitor.php index de9adef..04e9ea6 100644 --- a/src/Monitoring/ProcessMonitor.php +++ b/src/Monitoring/ProcessMonitor.php @@ -15,16 +15,27 @@ public function __construct(private readonly EntityManagerInterface $entityManag { } - public function start(ProcessInterface $process): ProcessInterface + /** + * If your process is long or asynchrone you might want to flush it on start, if so pass true to the $flush param + */ + public function start(ProcessInterface $process, bool $flush = false): ProcessInterface { $process->setStartedAt(new \DateTime()); $process->setStatus(ProcessStatusEnum::ONGOING); + if ($flush) { + $this->entityManager->persist($process); + $this->entityManager->flush(); + } return $process; } - public function end(ProcessInterface $process, bool $isSuccess = true, bool $flush = true): void + public function end(?ProcessInterface $process, bool $isSuccess = true, bool $flush = true): void { + if ($process == null) { + return; + } + $endedAt = new \DateTime(); $process->setEndedAt($endedAt); $process->setDuration((int) $endedAt->format('Uv') - (int) $process->getStartedAt()->format('Uv')); From 13c535f6b785a15ff88309cb37dc6bc9c96d0293 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 24 Apr 2024 16:06:39 +0200 Subject: [PATCH 2/4] Add ISO8601Formatter Date and Datetime const --- src/Formatter/ISO8601Formatter.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/Formatter/ISO8601Formatter.php diff --git a/src/Formatter/ISO8601Formatter.php b/src/Formatter/ISO8601Formatter.php new file mode 100644 index 0000000..ee2ac27 --- /dev/null +++ b/src/Formatter/ISO8601Formatter.php @@ -0,0 +1,13 @@ + + */ +class ISO8601Formatter +{ + public const DATE = 'DD/MM/YYYY'; + public const DATETIME = 'DD/MM/YYYY à HH:mm'; + public const DATETIME_WITH_SECONDS = 'DD/MM/YYYY à HH:mm:ss'; +} From e1ab3c6e2a465eea7e5055d9f25f7f7a0060fca9 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 24 Apr 2024 16:15:10 +0200 Subject: [PATCH 3/4] ProcessMonitor : Add functions to manage process log and print them out to the console --- src/Monitoring/ProcessMonitor.php | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/Monitoring/ProcessMonitor.php b/src/Monitoring/ProcessMonitor.php index 04e9ea6..131c34d 100644 --- a/src/Monitoring/ProcessMonitor.php +++ b/src/Monitoring/ProcessMonitor.php @@ -5,12 +5,16 @@ use Doctrine\ORM\EntityManagerInterface; use Smart\CoreBundle\Entity\ProcessInterface; use Smart\CoreBundle\Enum\ProcessStatusEnum; +use Symfony\Component\Console\Style\SymfonyStyle; /** * @author Mathieu Ducrot */ class ProcessMonitor { + protected ?ProcessInterface $process = null; + protected ?SymfonyStyle $consoleIo = null; + public function __construct(private readonly EntityManagerInterface $entityManager) { } @@ -26,6 +30,7 @@ public function start(ProcessInterface $process, bool $flush = false): ProcessIn $this->entityManager->persist($process); $this->entityManager->flush(); } + $this->process = $process; return $process; } @@ -50,4 +55,51 @@ public function end(?ProcessInterface $process, bool $isSuccess = true, bool $fl $this->entityManager->flush(); } } + + public function log(string $message): void + { + $this->process?->addLog($message); + $this->consoleIo?->writeln($message); + } + + public function logSection(string $message): void + { + $this->process?->addLog('--- ' . $message); + $this->consoleIo?->section($message); + } + + public function logWarning(string $message): void + { + $this->process?->addLog('/!\\ ' . $message); + $this->consoleIo?->warning($message); + } + + public function logSuccess(string $message): void + { + $this->process?->setSummary($message); + $this->consoleIo?->success($message); + } + + public function logException(\Exception $e): void + { + $message = $e->getMessage(); + $this->process?->setSummary($message); + $this->process?->addExceptionTraceData($e); + $this->consoleIo?->error($message); + } + + public function processAddData(string $key, mixed $value): void + { + $this->process?->addData($key, $value); + } + + public function getProcess(): ?ProcessInterface + { + return $this->process; + } + + public function setConsoleIo(?SymfonyStyle $consoleIo): void + { + $this->consoleIo = $consoleIo; + } } From 766f3b4852a4b419af2a62857334d722900148bf Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Thu, 25 Apr 2024 17:20:46 +0200 Subject: [PATCH 4/4] Comment ProcessMonitor addExceptionTraceData for now as it can cause memory issue on Clever --- CHANGELOG.md | 9 +++++++++ src/Monitoring/ProcessMonitor.php | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1b2800..f3ea251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ CHANGELOG for 1.x =================== +## v1.3.0 - (2024-04-29) +### Added +- `ISO8601Formatter` for common date formatting +- `ProcessMonitor` Add functions to manage process log and print them out to the console + +### Changed +- `ProcessMonitor::start` Add boolean flush param to better manage when the process should be flushed +- `ProcessMonitor::end` Handle passing process param as null + ## v1.2.4 - (2024-04-22) **The process duration value is now calculated in milliseconds instead of seconds.** diff --git a/src/Monitoring/ProcessMonitor.php b/src/Monitoring/ProcessMonitor.php index 131c34d..347cac3 100644 --- a/src/Monitoring/ProcessMonitor.php +++ b/src/Monitoring/ProcessMonitor.php @@ -84,7 +84,7 @@ public function logException(\Exception $e): void { $message = $e->getMessage(); $this->process?->setSummary($message); - $this->process?->addExceptionTraceData($e); + // $this->process?->addExceptionTraceData($e); cause memomry limit issue on clever, try to catch the exception with sentry instead $this->consoleIo?->error($message); }