Skip to content

Commit

Permalink
Merge pull request #26 from smartbooster/process_monitor_start_flush
Browse files Browse the repository at this point in the history
Add ProcessMonitor log function + ISO8601Formatter
  • Loading branch information
mathieu-ducrot authored Apr 29, 2024
2 parents f65f060 + 766f3b4 commit 098ed9f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.**
Expand Down
13 changes: 13 additions & 0 deletions src/Formatter/ISO8601Formatter.php
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';
}
67 changes: 65 additions & 2 deletions src/Monitoring/ProcessMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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;
}
}

0 comments on commit 098ed9f

Please sign in to comment.