From a3de9020c8d0ae349a00ca4fe0c1497ae89b990f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 31 Mar 2019 19:00:04 +0200 Subject: [PATCH] [Messenger] fix review --- .../Component/Messenger/Command/StopWorkersCommand.php | 2 +- .../Messenger/Worker/StopWhenRestartSignalIsReceived.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Messenger/Command/StopWorkersCommand.php b/src/Symfony/Component/Messenger/Command/StopWorkersCommand.php index afb2ce0dd654b..0ea9582897a09 100644 --- a/src/Symfony/Component/Messenger/Command/StopWorkersCommand.php +++ b/src/Symfony/Component/Messenger/Command/StopWorkersCommand.php @@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); $cacheItem = $this->restartSignalCachePool->getItem(StopWhenRestartSignalIsReceived::RESTART_REQUESTED_TIMESTAMP_KEY); - $cacheItem->set(time()); + $cacheItem->set(microtime(true)); $this->restartSignalCachePool->save($cacheItem); $io->success('Signal successfully sent to stop any running workers.'); diff --git a/src/Symfony/Component/Messenger/Worker/StopWhenRestartSignalIsReceived.php b/src/Symfony/Component/Messenger/Worker/StopWhenRestartSignalIsReceived.php index 63f6ea04d67bb..29b52c87da91b 100644 --- a/src/Symfony/Component/Messenger/Worker/StopWhenRestartSignalIsReceived.php +++ b/src/Symfony/Component/Messenger/Worker/StopWhenRestartSignalIsReceived.php @@ -38,14 +38,14 @@ public function __construct(WorkerInterface $decoratedWorker, CacheItemPoolInter public function run(array $options = [], callable $onHandledCallback = null): void { - $workerStartedTimestamp = time(); + $workerStartedAt = microtime(true); - $this->decoratedWorker->run($options, function (?Envelope $envelope) use ($onHandledCallback, $workerStartedTimestamp) { + $this->decoratedWorker->run($options, function (?Envelope $envelope) use ($onHandledCallback, $workerStartedAt) { if (null !== $onHandledCallback) { $onHandledCallback($envelope); } - if ($this->shouldRestart($workerStartedTimestamp)) { + if ($this->shouldRestart($workerStartedAt)) { $this->stop(); if (null !== $this->logger) { $this->logger->info('Worker stopped because a restart was requested.'); @@ -59,9 +59,10 @@ public function stop(): void $this->decoratedWorker->stop(); } - private function shouldRestart(int $workerStartedAt) + private function shouldRestart(float $workerStartedAt) { $cacheItem = $this->cachePool->getItem(self::RESTART_REQUESTED_TIMESTAMP_KEY); + if (!$cacheItem->isHit()) { // no restart has ever been scheduled return false;