-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Dispatch events before & after each handler #52425
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Handler\HandlerDescriptor; | ||
|
||
abstract class AbstractHandlerEvent | ||
{ | ||
public function __construct( | ||
public readonly Envelope $envelope, | ||
public readonly HandlerDescriptor $handlerDescriptor, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Handler\HandlerDescriptor; | ||
|
||
/** | ||
* Event dispatched after a handler fails. | ||
*/ | ||
final class HandlerFailureEvent extends AbstractHandlerEvent | ||
{ | ||
public function __construct( | ||
Envelope $envelope, | ||
HandlerDescriptor $handlerDescriptor, | ||
public readonly \Throwable $exception, | ||
) { | ||
parent::__construct($envelope, $handlerDescriptor); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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; | ||
|
||
/** | ||
* Event dispatched before a handler is called. | ||
*/ | ||
final class HandlerStartingEvent extends AbstractHandlerEvent | ||
Comment on lines
+15
to
+17
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. The name of the class does not make it clear, that it is dispatched before. Maybe BeforeHandlerStartEvent? 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. I actually thought about this, but wanted to keep things organized by prefix, like |
||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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; | ||
|
||
/** | ||
* Event dispatched after a handler succeeds. | ||
*/ | ||
final class HandlerSuccessEvent extends AbstractHandlerEvent | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,8 +11,12 @@ | |||||||||
|
||||||||||
namespace Symfony\Component\Messenger\Middleware; | ||||||||||
|
||||||||||
use Psr\EventDispatcher\EventDispatcherInterface; | ||||||||||
use Psr\Log\LoggerAwareTrait; | ||||||||||
use Symfony\Component\Messenger\Envelope; | ||||||||||
use Symfony\Component\Messenger\Event\HandlerFailureEvent; | ||||||||||
use Symfony\Component\Messenger\Event\HandlerStartingEvent; | ||||||||||
use Symfony\Component\Messenger\Event\HandlerSuccessEvent; | ||||||||||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||||||||||
use Symfony\Component\Messenger\Exception\LogicException; | ||||||||||
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; | ||||||||||
|
@@ -35,6 +39,7 @@ class HandleMessageMiddleware implements MiddlewareInterface | |||||||||
public function __construct( | ||||||||||
private HandlersLocatorInterface $handlersLocator, | ||||||||||
private bool $allowNoHandlers = false, | ||||||||||
private ?EventDispatcherInterface $eventDispatcher = null, | ||||||||||
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.
Suggested change
I like it more, but lets see what the others say 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. Actually |
||||||||||
) { | ||||||||||
} | ||||||||||
|
||||||||||
|
@@ -58,6 +63,10 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope | |||||||||
continue; | ||||||||||
} | ||||||||||
|
||||||||||
$this->eventDispatcher?->dispatch(new HandlerStartingEvent($envelope, $handlerDescriptor)); | ||||||||||
|
||||||||||
$e = null; | ||||||||||
|
||||||||||
try { | ||||||||||
$handler = $handlerDescriptor->getHandler(); | ||||||||||
$batchHandler = $handlerDescriptor->getBatchHandler(); | ||||||||||
|
@@ -97,6 +106,14 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope | |||||||||
} catch (\Throwable $e) { | ||||||||||
$exceptions[$handlerDescriptor->getName()] = $e; | ||||||||||
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.
Suggested change
Same for the success case and remove the 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. I don't mind dispatching the failure event in the That's why I kept things outside the 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. It would be great if a HandlerFailureEvent subscriber could silence an exception. Right now, it's only possible to log them. But by always passing the exception to the HandlerFailureEvent and dispatching that, a subscriber could say "ignore this". A use case could be to annotate From Messenger perspective all will be fine again. Related to 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 could add a 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. @ro0NL @OskarStark What do you think? 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. Also fine, but then we should let a subscriber mutate the handler descriptor. See my previous comment. |
||||||||||
} | ||||||||||
|
||||||||||
if (null !== $this->eventDispatcher) { | ||||||||||
$event = (null !== $e) | ||||||||||
? new HandlerFailureEvent($envelope, $handlerDescriptor, $e) | ||||||||||
: new HandlerSuccessEvent($envelope, $handlerDescriptor); | ||||||||||
|
||||||||||
$this->eventDispatcher->dispatch($event); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/** @var FlushBatchHandlersStamp $flushStamp */ | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean no abstract? What about
AbstractWorkerMessageEvent
?