Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae59fa0

Browse filesBrowse files
committed
[Messenger] Added StopWorkerException
1 parent 14a3730 commit ae59fa0
Copy full SHA for ae59fa0

File tree

5 files changed

+103
-0
lines changed
Filter options

5 files changed

+103
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Messenger\EventListener\DispatchPcntlSignalListener;
2020
use Symfony\Component\Messenger\EventListener\SendFailedMessageForRetryListener;
2121
use Symfony\Component\Messenger\EventListener\SendFailedMessageToFailureTransportListener;
22+
use Symfony\Component\Messenger\EventListener\StopWorkerOnCustomStopExceptionListener;
2223
use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
2324
use Symfony\Component\Messenger\EventListener\StopWorkerOnSigtermSignalListener;
2425
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
@@ -186,6 +187,9 @@
186187
->set('messenger.listener.stop_worker_on_sigterm_signal_listener', StopWorkerOnSigtermSignalListener::class)
187188
->tag('kernel.event_subscriber')
188189

190+
->set('messenger.listener.stop_worker_on_stop_exception_listener', StopWorkerOnCustomStopExceptionListener::class)
191+
->tag('kernel.event_subscriber')
192+
189193
->set('messenger.routable_message_bus', RoutableMessageBus::class)
190194
->args([
191195
abstract_arg('message bus locator'),

‎src/Symfony/Component/Messenger/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* `InMemoryTransport` can perform message serialization through dsn `in-memory://?serialize=true`.
8+
* Add `StopWorkerExceptionInterface` and its implementation `StopWorkerException` to stop the worker.
89

910
5.2.0
1011
-----
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
16+
use Symfony\Component\Messenger\Event\WorkerRunningEvent;
17+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
18+
use Symfony\Component\Messenger\Exception\StopWorkerExceptionInterface;
19+
20+
/**
21+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
22+
*/
23+
class StopWorkerOnCustomStopExceptionListener implements EventSubscriberInterface
24+
{
25+
private $stop = false;
26+
27+
public function onMessageFailed(WorkerMessageFailedEvent $event): void
28+
{
29+
$th = $event->getThrowable();
30+
if ($th instanceof StopWorkerExceptionInterface) {
31+
$this->stop = true;
32+
}
33+
if ($th instanceof HandlerFailedException) {
34+
foreach ($th->getNestedExceptions() as $e) {
35+
if ($e instanceof StopWorkerExceptionInterface) {
36+
$this->stop = true;
37+
}
38+
}
39+
}
40+
}
41+
42+
public function onWorkerRunning(WorkerRunningEvent $event): void
43+
{
44+
if ($this->stop) {
45+
$event->getWorker()->stop();
46+
}
47+
}
48+
49+
public static function getSubscribedEvents(): array
50+
{
51+
return [
52+
WorkerMessageFailedEvent::class => 'onMessageFailed',
53+
WorkerRunningEvent::class => 'onWorkerRunning',
54+
];
55+
}
56+
}
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Exception;
13+
14+
/**
15+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
16+
*/
17+
class StopWorkerException extends RuntimeException implements StopWorkerExceptionInterface
18+
{
19+
public function __construct(string $message = 'Worker should stop.', ?\Throwable $previous = null)
20+
{
21+
parent::__construct($message, 0, $previous);
22+
}
23+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Exception;
13+
14+
/**
15+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
16+
*/
17+
interface StopWorkerExceptionInterface extends \Throwable
18+
{
19+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.