Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[Messenger] MakeHandleMessageMiddleware::callHandler protected#50980

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ruudk wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromruudk:make-call-handler-protected

Conversation

ruudk
Copy link
Contributor

QA
Branch?6.4
Bug fix?no
New feature?yes
Deprecations?no
Tickets
LicenseMIT
Doc PR

I made the following changes:

  • makecallHandler protected
  • pass the HandlerDescriptor (can be used to read options from the handler)
  • pass the Envelope (can be used to read more stamps)

This allows people to decorate theHandleMessageMiddleware and intercept the moment the handler is called.

I currently have the following use cases for this in my application:

  1. Every handler can decide if they want to wrap their handling in a transaction. This is done by
    a custom#[WrapInTransaction] attribute. When the attribute is there, we wrap it in a database
    transaction. I know theDoctrineTransactionMiddleware exists in the Symfony Doctrine bridge,
    but that runs as middleware and cannot be toggled individually. It would be possible to modify it
    so that it only runs when the handler has it enabled using an option, but that only works for
    buses with only 1 handler. For an event bus, with multiple handlers, that won't be a solution.

  2. Every handler can decide if they want to ignore certain exceptions that are thrown. This is done
    by a custom#[IgnoreException] attribute. For example the handler uses
    #[IgnoreException(EventNotFoundException::class)]. Whenever that exception is thrown, the
    exception is caught and ignored. The middleware thinks it's executed without issues.

I made the following changes:- make `callHandler` protected- pass the HandlerDescriptor (can be used to read options from the handler)- pass the Envelope (can be used to read more stamps)This allows people to decorate the `HandleMessageMiddleware` and intercept the moment the handleris called.I currently have the following use cases for this in my application:1) Every handler can decide if they want to wrap their handling in a transaction. This is done by   a custom `#[WrapInTransaction]` attribute. When the attribute is there, we wrap it in a database   transaction. I know the `DoctrineTransactionMiddleware` exists in the Symfony Doctrine bridge,   but that runs as middleware and cannot be toggled individually. It would be possible to modify it   so that it only runs when the handler has it enabled using an option, but that only works for   buses with only 1 handler. For an event bus, with multiple handlers, that won't be a solution.2) Every handler can decide if they want to ignore certain exceptions that are thrown. This is done   by a custom `#[IgnoreException]` attribute. For example the handler uses   `#[IgnoreException(EventNotFoundException::class)]`. Whenever that exception is thrown, the   exception is caught and ignored. The middleware thinks it's executed without issues.
@carsonbotcarsonbot added this to the6.4 milestoneJul 14, 2023
ruudk added a commit to ruudk/symfony that referenced this pull requestJul 17, 2023
The `HandlerDescriptor` class is final and therefore cannot be extended. To make this a bit moreflexible we introduce the `HandlerDescriptorInterface` that is used everywhere.It also introduces a `.messenger.handler_descriptor_class` parameter that's used in theMessengerPass. With this parameter it's possible to use a custom implementation of the`HandlersLocator`.This is an alternative approach tosymfony#50980.
@ruudk
Copy link
ContributorAuthor

ruudk commentedOct 13, 2023
edited
Loading

Sure, this can be solved by a custom middleware. But that means that I will be replacing a big part of Messenger, namely the way it handles messages:HandleMessageMiddleware.php.

That requires me to keep my "fork" middleware, sync with every release.

What I want is an extension point, where it passes a message to more than 1 handlers.

My use case: I want to add extra functionality for event subscribers. I want to know which subscriber failed, and which didn't. Or I want to wrap every subscriber inside its own database transaction.

Currently, not possible as we can only wrap ALL handlers.

@ruudk
Copy link
ContributorAuthor

A try/catch inside the subscribers will work, but that requires putting this in place in 1000's of subscribers. Or it requires me to create an abstract subscriber that all subscribers need to extend from.

I want to handle this in a higher level. Abstracted away from what developers will see in their day to day.

@ruudk
Copy link
ContributorAuthor

@ro0NL Interesting. I checked it but I don't see how that would work. Or how that could help my use case.

I really think there should be an extension point for the execution of the handler.

Or, in an ideal world, there is a dedicated middleware stacks per handler. That would allow wrapping handlers isolated, instead of the current situation where multiple handlerscan be called.

@nicolas-grekasnicolas-grekas modified the milestones:6.4,7.1Nov 15, 2023
@xabbuhxabbuh modified the milestones:7.1,7.2May 15, 2024
@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
@jerowork
Copy link
Contributor

Bringing back to the attention 😎 .

If we would make thecallHandler protected, it allows to extend on theHandleMessageMiddleware, which avoids maintaining own versions of the full middleware. It makes the Messenger more flexible for extension :).

At the same time, the risk/impact of such a change (private to protected) is limited, fairly safe to do so.

Is this still an option to agree on and merge? 🙏

ruudk reacted with thumbs up emoji

@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

6 participants
@ruudk@jerowork@fabpot@nicolas-grekas@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp