From 217ac6236bf590a3c174ca43be5d08330a496954 Mon Sep 17 00:00:00 2001 From: Daniel Badura Date: Wed, 5 Feb 2025 11:55:21 +0100 Subject: [PATCH 1/3] Add new option subscribers --- src/DefaultWorker.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/DefaultWorker.php b/src/DefaultWorker.php index 8d792b6..4ea3b1c 100644 --- a/src/DefaultWorker.php +++ b/src/DefaultWorker.php @@ -16,6 +16,7 @@ use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use function max; use function microtime; @@ -83,8 +84,8 @@ public function stop(): void } /** - * @param Closure(Closure):void $job - * @param array{runLimit?: (positive-int|null), memoryLimit?: (string|null), timeLimit?: (positive-int|null)} $options + * @param Closure(Closure):void $job + * @param array{runLimit?: (positive-int|null), memoryLimit?: (string|null), timeLimit?: (positive-int|null), subscribers?: list} $options */ public static function create( Closure $job, @@ -112,6 +113,12 @@ public static function create( ); } + if (isset($options['subscribers'])) { + foreach ($options['subscribers'] as $subscriber) { + $eventDispatcher->addSubscriber($subscriber); + } + } + return new self( $job, $eventDispatcher, From 4803b4cde72fccd28b3a431a0ddb67f49e3ebc68 Mon Sep 17 00:00:00 2001 From: Daniel Badura Date: Wed, 5 Feb 2025 12:26:39 +0100 Subject: [PATCH 2/3] Intead of providing subscribers, inject the dispatcher itself --- src/DefaultWorker.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/DefaultWorker.php b/src/DefaultWorker.php index 4ea3b1c..70cfe3d 100644 --- a/src/DefaultWorker.php +++ b/src/DefaultWorker.php @@ -16,7 +16,6 @@ use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; use function max; use function microtime; @@ -84,15 +83,19 @@ public function stop(): void } /** - * @param Closure(Closure):void $job - * @param array{runLimit?: (positive-int|null), memoryLimit?: (string|null), timeLimit?: (positive-int|null), subscribers?: list} $options + * @param Closure(Closure):void $job + * @param array{runLimit?: (positive-int|null), memoryLimit?: (string|null), timeLimit?: (positive-int|null)} $options */ public static function create( Closure $job, array $options = [], LoggerInterface $logger = new NullLogger(), + EventDispatcherInterface|null $eventDispatcher, ): self { - $eventDispatcher = new EventDispatcher(); + if ($eventDispatcher === null) { + $eventDispatcher = new EventDispatcher(); + } + $eventDispatcher->addSubscriber(new StopWorkerOnSigtermSignalListener($logger)); if (isset($options['runLimit'])) { @@ -113,12 +116,6 @@ public static function create( ); } - if (isset($options['subscribers'])) { - foreach ($options['subscribers'] as $subscriber) { - $eventDispatcher->addSubscriber($subscriber); - } - } - return new self( $job, $eventDispatcher, From 7ae57f4497a68a07c666ac4f5b98bf27288a33c8 Mon Sep 17 00:00:00 2001 From: Daniel Badura Date: Wed, 5 Feb 2025 15:23:40 +0100 Subject: [PATCH 3/3] Add test --- src/DefaultWorker.php | 2 +- tests/Unit/DefaultWorkerTest.php | 50 +++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/DefaultWorker.php b/src/DefaultWorker.php index 70cfe3d..b2788ae 100644 --- a/src/DefaultWorker.php +++ b/src/DefaultWorker.php @@ -90,7 +90,7 @@ public static function create( Closure $job, array $options = [], LoggerInterface $logger = new NullLogger(), - EventDispatcherInterface|null $eventDispatcher, + EventDispatcherInterface|null $eventDispatcher = null, ): self { if ($eventDispatcher === null) { $eventDispatcher = new EventDispatcher(); diff --git a/tests/Unit/DefaultWorkerTest.php b/tests/Unit/DefaultWorkerTest.php index d3a3f88..4c81b3c 100644 --- a/tests/Unit/DefaultWorkerTest.php +++ b/tests/Unit/DefaultWorkerTest.php @@ -12,6 +12,7 @@ use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Log\LoggerInterface; +use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; final class DefaultWorkerTest extends TestCase @@ -20,9 +21,9 @@ final class DefaultWorkerTest extends TestCase public function testRunWorker(): void { - $evenDispatcher = $this->prophesize(EventDispatcherInterface::class); - $evenDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); - $evenDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1)->will( + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1)->will( /** @param array{WorkerRunningEvent} $args */ static function (array $args) { $args[0]->worker->stop(); @@ -30,7 +31,7 @@ static function (array $args) { return $args[0]; }, ); - $evenDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); $logger = $this->prophesize(LoggerInterface::class); $logger->debug('Worker starting')->shouldBeCalledTimes(1); @@ -40,16 +41,16 @@ static function (array $args) { $logger->debug('Worker stopped')->shouldBeCalledTimes(1); $logger->debug('Worker terminated')->shouldBeCalledTimes(1); - $worker = new DefaultWorker(static fn () => null, $evenDispatcher->reveal(), $logger->reveal()); + $worker = new DefaultWorker(static fn () => null, $eventDispatcher->reveal(), $logger->reveal()); $worker->run(200); } public function testJobStopWorker(): void { - $evenDispatcher = $this->prophesize(EventDispatcherInterface::class); - $evenDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); - $evenDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1); - $evenDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(Argument::type(WorkerStartedEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerRunningEvent::class))->shouldBeCalledTimes(1); + $eventDispatcher->dispatch(Argument::type(WorkerStoppedEvent::class))->shouldBeCalledTimes(1); $logger = $this->prophesize(LoggerInterface::class); $logger->debug('Worker starting')->shouldBeCalledTimes(1); @@ -63,10 +64,39 @@ public function testJobStopWorker(): void static function ($stop): void { $stop(); }, - $evenDispatcher->reveal(), + $eventDispatcher->reveal(), $logger->reveal(), ); $worker->run(0); } + + public function testCustomEventDispatcher(): void + { + $listener = new class { + public int $called = 0; + + public function __invoke(WorkerStartedEvent $event): void + { + $this->called++; + } + }; + + $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addListener(WorkerStartedEvent::class, $listener); + + $logger = $this->prophesize(LoggerInterface::class); + $worker = DefaultWorker::create( + static function ($stop): void { + $stop(); + }, + [], + $logger->reveal(), + $eventDispatcher, + ); + + $worker->run(0); + + self::assertEquals(1, $listener->called); + } }