-
Notifications
You must be signed in to change notification settings - Fork 0
Skip the sleep when the job returns true #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ | |
|
|
||
| private readonly ClockInterface $clock; | ||
|
|
||
| /** @param Closure(Closure):void $job */ | ||
| /** @param Closure(Closure):(bool|void) $job return true if the job did work to skip the sleep */ | ||
| public function __construct( | ||
| private readonly Closure $job, | ||
| private readonly EventDispatcherInterface $eventDispatcher, | ||
|
|
@@ -39,7 +39,7 @@ | |
| } | ||
|
|
||
| /** @param positive-int|0 $sleepTimer in milliseconds */ | ||
| public function run(int $sleepTimer = 1000): void | ||
|
Check warning on line 42 in src/DefaultWorker.php
|
||
| { | ||
| $this->logger?->debug('Worker starting'); | ||
|
|
||
|
|
@@ -53,7 +53,7 @@ | |
|
|
||
| $startTime = $this->milliseconds(); | ||
|
|
||
| ($this->job)($this->stop(...)); | ||
| $didWork = ($this->job)($this->stop(...)) === true; | ||
|
|
||
| $endTime = $this->milliseconds(); | ||
| $ranTime = $endTime - $startTime; | ||
|
|
@@ -63,17 +63,21 @@ | |
| $this->eventDispatcher->dispatch(new WorkerRunningEvent($this)); | ||
|
|
||
| if ($this->shouldStop) { | ||
| break; | ||
|
Check warning on line 66 in src/DefaultWorker.php
|
||
| } | ||
|
|
||
| if ($didWork) { | ||
| continue; | ||
| } | ||
|
|
||
| $sleepFor = max($sleepTimer - $ranTime, 0); | ||
|
Check warning on line 73 in src/DefaultWorker.php
|
||
|
|
||
| if ($sleepFor <= 0) { | ||
| continue; | ||
| } | ||
|
|
||
| $this->logger?->debug('Worker sleep for {sleepTimer}ms', ['sleepTimer' => $sleepFor]); | ||
| usleep($sleepFor * 1000); | ||
|
Check warning on line 80 in src/DefaultWorker.php
|
||
| } | ||
| } catch (Throwable $exception) { | ||
| throw $exception; | ||
|
|
@@ -95,7 +99,7 @@ | |
| } | ||
|
|
||
| /** | ||
| * @param Closure(Closure):void $job | ||
| * @param Closure(Closure):(bool|void) $job | ||
| * @param array{runLimit?: (positive-int|null), memoryLimit?: (string|null), timeLimit?: (positive-int|null)} $options | ||
| */ | ||
| public static function create( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isnt it more
true|void? :DUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only
truechanges the behaviour, that's right. I'd still keepboolthough, for two reasons.falsereads as an explicit "nothing to do, sleep", which makes the intent clearer than mixingreturn;andreturn true;:And it lets a job pass a bool result straight through, which is common for consumer APIs:
With
true|void, PHPStan rejects the first closure becausefalseis not allowed, so the result has to be converted by hand. That needs an extra branch just to dropfalse, and the closure can't have a native return type anymore:true|voidisn't valid PHP, andtrue|nullwould throw a TypeError on the path without areturn, unless you add an explicitreturn null.