Skip to content

Navigation Menu

Sign in
Appearance settings

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

[Messenger] Add middleware to stop all workers on exception #35481

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
<argument type="service" id="debug.stopwatch" />
</service>

<service id="messenger.middleware.stop_worker_on_exceptions" class="Symfony\Component\Messenger\Middleware\StopWorkerOnExceptionMiddleware" abstract="true" public="false">
<argument type="collection" />
<call method="setRestartSignalCachePool">
<argument type="service" id="cache.messenger.restart_workers_signal" />
</call>
</service>

<!-- Discovery -->
<service id="messenger.receiver_locator">
<tag name="container.service_locator" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

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

namespace Symfony\Component\Messenger\Middleware;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
use Symfony\Component\Messenger\Exception\HandlerFailedException;

/**
* Stop all workers when an exceptions is thrown.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class StopWorkerOnExceptionMiddleware
{
private $exceptions;
private $restartSignalCachePool;

/**
*
* @param array $exceptions of fully qualified class names of exceptions
*/
public function __construct(array $exceptions)
{
$this->exceptions = array_values($exceptions);
}

/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
try {
return $stack->next()->handle($envelope, $stack);
} catch (HandlerFailedException $e) {
if (count($this->exceptions) === 0) {
throw $e;
}

if (count($this->exceptions) === 1 && $this->exceptions[0] === '*') {
$this->stopWorkers();
throw $e;
}

foreach ($e->getNestedExceptions() as $exception) {
if (in_array(get_class($exception), $this->exceptions)) {
$this->stopWorkers();
break;
}
}

throw $e;
}
}

private function stopWorkers(): void
{
$cacheItem = $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);
$cacheItem->set(microtime(true));
$this->restartSignalCachePool->save($cacheItem);
}

public function setRestartSignalCachePool(CacheItemPoolInterface $restartSignalCachePool): void
{
if ($this->restartSignalCachePool !== null) {
throw new \RuntimeException('Cannot update restartSignalCachePool dependency');
}

$this->restartSignalCachePool = $restartSignalCachePool;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.