-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Support for handling messages after current bus is finished #28849
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Messenger/Exception/DelayedMessageHandlingException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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\Exception; | ||
|
||
/** | ||
* When handling queued messages from {@link DispatchAfterCurrentBusMiddleware}, | ||
* some handlers caused an exception. This exception contains all those handler exceptions. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class DelayedMessageHandlingException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
private $exceptions; | ||
|
||
public function __construct(array $exceptions) | ||
{ | ||
$exceptionMessages = implode(", \n", array_map( | ||
function (\Throwable $e) { | ||
return \get_class($e).': '.$e->getMessage(); | ||
}, | ||
$exceptions | ||
)); | ||
|
||
if (1 === \count($exceptions)) { | ||
$message = sprintf("A delayed message handler threw an exception: \n\n%s", $exceptionMessages); | ||
} else { | ||
$message = sprintf("Some delayed message handlers threw an exception: \n\n%s", $exceptionMessages); | ||
} | ||
|
||
$this->exceptions = $exceptions; | ||
|
||
parent::__construct($message, 0, $exceptions[0]); | ||
} | ||
|
||
public function getExceptions(): array | ||
{ | ||
return $this->exceptions; | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
src/Symfony/Component/Messenger/Middleware/DispatchAfterCurrentBusMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?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 Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\DelayedMessageHandlingException; | ||
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp; | ||
|
||
/** | ||
* Allow to configure messages to be handled after the current bus is finished. | ||
* | ||
* I.e, messages dispatched from a handler with a DispatchAfterCurrentBus stamp | ||
* will actually be handled once the current message being dispatched is fully | ||
* handled. | ||
* | ||
* For instance, using this middleware before the DoctrineTransactionMiddleware | ||
* means sub-dispatched messages with a DispatchAfterCurrentBus stamp would be | ||
* handled after the Doctrine transaction has been committed. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class DispatchAfterCurrentBusMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var QueuedEnvelope[] A queue of messages and next middleware | ||
*/ | ||
private $queue = []; | ||
|
||
/** | ||
* @var bool this property is used to signal if we are inside a the first/root call to | ||
* MessageBusInterface::dispatch() or if dispatch has been called inside a message handler | ||
*/ | ||
private $isRootDispatchCallRunning = false; | ||
|
||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
if (null !== $envelope->last(DispatchAfterCurrentBusStamp::class)) { | ||
if (!$this->isRootDispatchCallRunning) { | ||
throw new \LogicException(sprintf('You can only use a "%s" stamp in the context of a message handler.', DispatchAfterCurrentBusStamp::class)); | ||
} | ||
$this->queue[] = new QueuedEnvelope($envelope, $stack); | ||
|
||
return $envelope; | ||
} | ||
|
||
if ($this->isRootDispatchCallRunning) { | ||
/* | ||
* A call to MessageBusInterface::dispatch() was made from inside the main bus handling, | ||
* but the message does not have the stamp. So, process it like normal. | ||
*/ | ||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
|
||
// First time we get here, mark as inside a "root dispatch" call: | ||
$this->isRootDispatchCallRunning = true; | ||
try { | ||
// Execute the whole middleware stack & message handling for main dispatch: | ||
$returnedEnvelope = $stack->next()->handle($envelope, $stack); | ||
} catch (\Throwable $exception) { | ||
/* | ||
* Whenever an exception occurs while handling a message that has | ||
* queued other messages, we drop the queued ones. | ||
* This is intentional since the queued commands were likely dependent | ||
* on the preceding command. | ||
*/ | ||
$this->queue = []; | ||
$this->isRootDispatchCallRunning = false; | ||
|
||
throw $exception; | ||
} | ||
|
||
// "Root dispatch" call is finished, dispatch stored messages. | ||
$exceptions = []; | ||
while (null !== $queueItem = array_shift($this->queue)) { | ||
try { | ||
// Execute the stored messages | ||
$queueItem->getStack()->next()->handle($queueItem->getEnvelope(), $queueItem->getStack()); | ||
} catch (\Exception $exception) { | ||
// Gather all exceptions | ||
$exceptions[] = $exception; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need to document that when you use this stamp, a totally different exception class will be thrown if your handler throws an exception. Just important to know if you're error handling. |
||
} | ||
} | ||
|
||
$this->isRootDispatchCallRunning = false; | ||
if (\count($exceptions) > 0) { | ||
throw new DelayedMessageHandlingException($exceptions); | ||
} | ||
|
||
return $returnedEnvelope; | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class QueuedEnvelope | ||
{ | ||
/** @var Envelope */ | ||
private $envelope; | ||
|
||
/** @var StackInterface */ | ||
private $stack; | ||
|
||
public function __construct(Envelope $envelope, StackInterface $stack) | ||
{ | ||
$this->envelope = $envelope; | ||
$this->stack = $stack; | ||
} | ||
|
||
public function getEnvelope(): Envelope | ||
{ | ||
return $this->envelope; | ||
} | ||
|
||
public function getStack(): StackInterface | ||
{ | ||
return $this->stack; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Messenger/Stamp/DispatchAfterCurrentBusStamp.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\Messenger\Stamp; | ||
|
||
/** | ||
* Marker item to tell this message should be handled in after the current bus has finished. | ||
* | ||
* @see \Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class DispatchAfterCurrentBusStamp implements StampInterface | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.