Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.3.0
-----

* Added `WorkerStoppedEvent` dispatched when a worker is stopped.
* Added optional `MessageCountAwareInterface` that receivers can implement
to give information about how many messages are waiting to be processed.
* [BC BREAK] The `Envelope::__construct()` signature changed:
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/Messenger/Event/WorkerStoppedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Event;

/**
* Dispatched when a worker has been stopped.
*
* @author Robin Chalas <[email protected]>
*
* @experimental in 4.3
*/
class WorkerStoppedEvent
{
}
11 changes: 7 additions & 4 deletions src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Retry\RetryStrategyInterface;
Expand Down Expand Up @@ -187,11 +188,12 @@ public function testWorkerDispatchesEventsOnSuccess()

$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();

$eventDispatcher->expects($this->exactly(2))
$eventDispatcher->expects($this->exactly(3))
->method('dispatch')
->withConsecutive(
[$this->isInstanceOf(WorkerMessageReceivedEvent::class)],
[$this->isInstanceOf(WorkerMessageHandledEvent::class)]
[$this->isInstanceOf(WorkerMessageHandledEvent::class)],
[$this->isInstanceOf(WorkerStoppedEvent::class)]
);

$worker = new Worker([$receiver], $bus, [], $eventDispatcher);
Expand All @@ -214,11 +216,12 @@ public function testWorkerDispatchesEventsOnError()

$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();

$eventDispatcher->expects($this->exactly(2))
$eventDispatcher->expects($this->exactly(3))
->method('dispatch')
->withConsecutive(
[$this->isInstanceOf(WorkerMessageReceivedEvent::class)],
[$this->isInstanceOf(WorkerMessageFailedEvent::class)]
[$this->isInstanceOf(WorkerMessageFailedEvent::class)],
[$this->isInstanceOf(WorkerStoppedEvent::class)]
);

$worker = new Worker([$receiver], $bus, [], $eventDispatcher);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
Expand Down Expand Up @@ -109,6 +110,8 @@ public function run(array $options = [], callable $onHandledCallback = null): vo
usleep($options['sleep']);
}
}

$this->dispatchEvent(new WorkerStoppedEvent());
}

private function handleMessage(Envelope $envelope, ReceiverInterface $receiver, string $receiverName, ?RetryStrategyInterface $retryStrategy)
Expand Down