-
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.
Merge pull request #26 from smartbooster/process_monitor_start_flush
Add ProcessMonitor log function + ISO8601Formatter
- Loading branch information
Showing
3 changed files
with
87 additions
and
2 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
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,13 @@ | ||
<?php | ||
|
||
namespace Smart\CoreBundle\Formatter; | ||
|
||
/** | ||
* @author Mathieu Ducrot <[email protected]> | ||
*/ | ||
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'; | ||
} |
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 |
---|---|---|
|
@@ -5,26 +5,42 @@ | |
use Doctrine\ORM\EntityManagerInterface; | ||
use Smart\CoreBundle\Entity\ProcessInterface; | ||
use Smart\CoreBundle\Enum\ProcessStatusEnum; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* @author Mathieu Ducrot <[email protected]> | ||
*/ | ||
class ProcessMonitor | ||
{ | ||
protected ?ProcessInterface $process = null; | ||
protected ?SymfonyStyle $consoleIo = null; | ||
|
||
public function __construct(private readonly EntityManagerInterface $entityManager) | ||
{ | ||
} | ||
|
||
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(); | ||
} | ||
$this->process = $process; | ||
|
||
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')); | ||
|
@@ -39,4 +55,51 @@ public function end(ProcessInterface $process, bool $isSuccess = true, bool $flu | |
$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); cause memomry limit issue on clever, try to catch the exception with sentry instead | ||
$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; | ||
} | ||
} |