Skip to content

Commit

Permalink
fix usage with crontab (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
N0S0I authored Sep 20, 2024
1 parent b2621d0 commit 29223a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tasks.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Tasks

on: push
on:
push:
pull_request:

jobs:
lint-php:
Expand Down
19 changes: 15 additions & 4 deletions Classes/EventListener/ConsoleCommandEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,33 @@

namespace Kanti\ServerTiming\EventListener;

use Exception;
use Kanti\ServerTiming\Dto\ScriptResult;
use Kanti\ServerTiming\SqlLogging\SqlLoggerCore11;
use Kanti\ServerTiming\Utility\TimingUtility;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Kanti\ServerTiming\Dto\StopWatch;

final class ConsoleCommandEventListener
{
/** @var StopWatch[] */
private array $stopWatches = [];

public function start(ConsoleCommandEvent $event): void
{
TimingUtility::start('console.command', (string)$event->getCommand()?->getName());
$this->stopWatches[] = TimingUtility::stopWatch('console.command', (string)$event->getCommand()?->getName());
}

public function stop(ConsoleTerminateEvent $event): void
{
TimingUtility::end('console.command');
TimingUtility::getInstance()->shutdown(ScriptResult::fromCli($event->getExitCode()));
$stopWatch = array_pop($this->stopWatches);
if ($stopWatch === null) {
throw new Exception('No stopWatch found, did you start the command already?');
}

$stopWatch->stop();
if (!$this->stopWatches) {
TimingUtility::getInstance()->shutdown(ScriptResult::fromCli($event->getExitCode()));
}
}
}

0 comments on commit 29223a4

Please sign in to comment.