From 9c76200aeb26132729317c705afea55cbf4689d6 Mon Sep 17 00:00:00 2001 From: David Badura Date: Fri, 2 Oct 2026 17:21:16 +0200 Subject: [PATCH] Reset stop flag after the worker has finished Calling run() a second time on a stopped worker did nothing except dispatch the started and stopped events. The flag is reset at the end of run() rather than at the start, so stop() before run() still prevents the job from running. --- src/DefaultWorker.php | 2 ++ tests/Unit/DefaultWorkerTest.php | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/DefaultWorker.php b/src/DefaultWorker.php index aa19e18..cfde05d 100644 --- a/src/DefaultWorker.php +++ b/src/DefaultWorker.php @@ -73,6 +73,8 @@ public function run(int $sleepTimer = 1000): void usleep($sleepFor * 1000); } + $this->shouldStop = false; + $this->logger?->debug('Worker stopped'); $this->eventDispatcher->dispatch(new WorkerStoppedEvent($this)); diff --git a/tests/Unit/DefaultWorkerTest.php b/tests/Unit/DefaultWorkerTest.php index 1249e1e..f063a64 100644 --- a/tests/Unit/DefaultWorkerTest.php +++ b/tests/Unit/DefaultWorkerTest.php @@ -275,4 +275,39 @@ static function ($stop) use (&$calls): void { self::assertSame(1, $calls); } + + public function testRunWorkerTwice(): void + { + $calls = 0; + + $worker = new DefaultWorker( + static function (callable $stop) use (&$calls): void { + $calls++; + $stop(); + }, + new EventDispatcher(), + ); + + $worker->run(0); + $worker->run(0); + + self::assertSame(2, $calls); + } + + public function testStopBeforeRun(): void + { + $calls = 0; + + $worker = new DefaultWorker( + static function () use (&$calls): void { + $calls++; + }, + new EventDispatcher(), + ); + + $worker->stop(); + $worker->run(0); + + self::assertSame(0, $calls); + } }