From 935618518a6d44324a2135ed8d0b90468d471711 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Fri, 31 Jan 2020 22:02:23 +0100 Subject: [PATCH] Made it possible to set the next interval between jobs --- src/Client.php | 32 +++++++++++++++++++++++--------- src/Context/Context.php | 22 ++++++++++++++++++++++ src/Context/ContextInterface.php | 10 ++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index 8569e9a..00bc468 100644 --- a/src/Client.php +++ b/src/Client.php @@ -117,14 +117,18 @@ public function run(): int do { try { - $this->processNextJob(); + $nextInterval = $this->processNextJob(); } catch (Throwable $e) { $exitCode = $this->handleException($e); break; } + if ($nextInterval === null) { + $nextInterval = $this->getOptions()->getInterval(); + } + // Allow the server to breath... - usleep($this->getOptions()->getInterval()); + usleep($nextInterval); } while ($this->shouldKeepRunning($startTime, memory_get_usage(true))); return $exitCode; @@ -145,23 +149,25 @@ private function shouldKeepRunning(int $startTime, int $memoryUsage): bool return !$runningTimeExpired && !$memoryLimitReached; } - private function processNextJob(): void + private function processNextJob(): ?int { $job = $this->storage->retrieveJob(); assert($job instanceof StoredJobInterface || $job === null); if (!$job) { - return; + return null; } // @todo Replace this with PSR-14 so we can move the logging into an event listener. - $this->processJob($job); + return $this->processJob($job); } - private function processJob(StoredJobInterface $storedJob): void + private function processJob(StoredJobInterface $storedJob): ?int { + $nextInterval = null; + try { - $this->executeJob($storedJob); + $nextInterval = $this->executeJob($storedJob); } catch (RecoverableException $exception) { $this->handleException($exception); @@ -177,13 +183,15 @@ private function processJob(StoredJobInterface $storedJob): void $this->handleException($exception); } + + return $nextInterval; } /** * @throws UnknownWorkerException Thrown when the worker type is unknown. * @throws ContainerExceptionInterface Thrown when the worker cannot be retrieved from the service container. */ - private function executeJob(StoredJobInterface $storedJob): void + private function executeJob(StoredJobInterface $storedJob): int { $job = $storedJob->createJobRepresentation(); assert($job instanceof JobInterface); @@ -201,7 +209,11 @@ private function executeJob(StoredJobInterface $storedJob): void $worker = $this->workers->get($job->getWorkerName()); assert($worker instanceof WorkerInterface); - $worker->run(new Context($this->storage, $this->logger, $storedJob)); + + $context = new Context($this->storage, $this->logger, $storedJob); + $context->setNextInterval($this->getOptions()->getInterval()); + + $worker->run($context); $this->storage->deleteJob($storedJob); @@ -209,6 +221,8 @@ private function executeJob(StoredJobInterface $storedJob): void '[#%d] Finished and successfully deleted job', $storedJob->getId() )); + + return $context->getNextInterval(); } private function rescheduleJob( diff --git a/src/Context/Context.php b/src/Context/Context.php index 2dfb9d2..1131a35 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -56,6 +56,11 @@ final class Context implements ContextInterface */ private $stats; + /** + * @var int + */ + private $nextInterval; + /** * Initializes a new instance of this class. */ @@ -66,6 +71,23 @@ public function __construct(StorageInterface $storage, LoggerInterface $logger, $this->storedJob = $storedJob; $this->params = $storedJob->createJobRepresentation()->getWorkerParams(); $this->stats = $storedJob->getStats(); + $this->nextInterval = 1; + } + + /** + * Gets the time in microseconds to wait before the next job can be executed. + */ + public function getNextInterval(): int + { + return $this->nextInterval; + } + + /** + * Sets the time in microseconds to wait before the next job can be executed. + */ + public function setNextInterval(int $nextInterval): void + { + $this->nextInterval = $nextInterval; } /** diff --git a/src/Context/ContextInterface.php b/src/Context/ContextInterface.php index 64e296c..8df79f2 100644 --- a/src/Context/ContextInterface.php +++ b/src/Context/ContextInterface.php @@ -18,6 +18,16 @@ */ interface ContextInterface // phpcs:ignore { + /** + * Gets the time in microseconds to wait before the next job can be executed. + */ + public function getNextInterval(): int; + + /** + * Sets the time in microseconds to wait before the next job can be executed. + */ + public function setNextInterval(int $nextInterval): void; + /** * Gets the logger which can be used to write to the output stream. */