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] Add support for "recording" events from entities#28850

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

Closed

Conversation

@Nyholm
Copy link
Member

@NyholmNyholm commentedOct 13, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets
LicenseMIT
Doc PR

This is dependent of#28849.

services:messenger.doctrine.entity_message_collector:class:Symfony\Bridge\Doctrine\Messenger\EventSubscriber\EntityMessageCollectorpublic:falsearguments:['@messenger.bus.event']tags:          -{ name: 'doctrine.event_subscriber', connection: 'default' }
useSymfony\Bridge\Doctrine\EntityMessage\EntityMessageCollectionInterface;useSymfony\Bridge\Doctrine\EntityMessage\MessageRecorderTrait;class Userimplements EntityMessageCollectionInterface{use MessageRecorderTrait;// ...publicfunctionsetEmail(string$email)    {$oldEmail =$this->email =$email;$this->email =$email;$this->record(newEmailChanged($this->id,$oldEmail,$email);    }

onEXHovia, jvasseur, maxhelias, maks-rafalko, andreybolonin, mathroc, and kbond reacted with thumbs up emojijvasseur reacted with hooray emojimaxhelias, smoench, xfifix, and andreybolonin reacted with heart emojiKoc and andreybolonin reacted with rocket emoji
@NyholmNyholm changed the title[Messenger ]Add support for "recording" events from entities[Messenger] Add support for "recording" events from entitiesOct 13, 2018
@nicolas-grekasnicolas-grekas added this to thenext milestoneOct 14, 2018
@Koc
Copy link
Contributor

Koc commentedOct 15, 2018

I'm afraid that this listener should be stateful and events should be dispatched on post flush eventhttps://github.com/doctrine/doctrine2/blob/2.7/lib/Doctrine/ORM/UnitOfWork.php#L430 (because Doctrine's transaction may fall)

jvasseur and jakzal reacted with thumbs up emoji

@NyholmNyholmforce-pushed themessenger-transaction-doctrine branch from9441919 to305438eCompareFebruary 22, 2019 14:01
@NyholmNyholmforce-pushed themessenger-transaction-doctrine branch 3 times, most recently from274e3dd to5d75022CompareMarch 19, 2019 05:45
@Nyholm
Copy link
MemberAuthor

This PR is rebased and squashed

@fabpot
Copy link
Member

/cc@sroze

Copy link
Contributor

@srozesroze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I agree with@Koc, thedispatch of these events needs to happen atpostFlush otherwise you might dispatch events from rollbacked entities.

But it's a fantastic feature ❤️

@Nyholm
Copy link
MemberAuthor

Thank you. I've updated the PR


if ($entity instanceof EntityMessageCollectionInterface) {
foreach ($entity->getRecordedMessages() as $message) {
$this->messageBus->dispatch($message, [new DispatchAfterCurrentBusStamp()]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What if the message bus throws an exception?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

That will bubble up. But the Doctrine transaction will still be committed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

So there's a risk some messages will be dispatched while some won't. Not sure if that's an issue yet, just pointing it out.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, If we dispatch 5 messages and number 2 fails, then the 3 other will never be handled. But that is an issue with the message bus and has nothing to do with this PR.

Koc and sroze reacted with thumbs up emoji
@Nyholm
Copy link
MemberAuthor

Maybe@weaverryan could have a look?

Copy link
Contributor

@srozesroze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks good to me, sorry for the delay 👍.

I imagine you'll PR onDoctrineBundle to enable this listener from a configuration flag?

@sroze
Copy link
Contributor

@Nyholm can you squad and push again? We should have Travis & AppVeyer rebuilding this :)

@NyholmNyholmforce-pushed themessenger-transaction-doctrine branch from87444c6 to692448fCompareSeptember 29, 2019 15:39
@Nyholm
Copy link
MemberAuthor

Comments are squashed. I will create a PR to doctrine bundle right away

@maxhelias
Copy link
Contributor

Nice feature! This should not be in 4.4 ?

@vudaltsov
Copy link
Contributor

vudaltsov commentedSep 30, 2019
edited
Loading

What if we avoid leaking events and eliminate the getter on the entity?

publicfunctiondispatchEvents(callable$dispatcher):void{$dispatcher($this->events);$this->events = [];}
jakzal, alexvolik, and Koc reacted with thumbs up emoji

@Nyholm
Copy link
MemberAuthor

Hm. Callables makes the stack tree hard to follow.

@vudaltsov How would you configure an implementation like this to work with DI?

@vudaltsov
Copy link
Contributor

vudaltsov commentedSep 30, 2019
edited
Loading

@Nyholm , this would be almost the same code you use:

privatefunctioncollectEventsFromEntity(LifecycleEventArgs$message):void{$entity =$message->getEntity();if (!$entityinstanceof EntityMessageCollectionInterface) {return;    }$entity->dispatchEvents(function (iterable$events):void {foreach ($eventsas$event) {$this->messageBus->dispatch($event, [newDispatchAfterCurrentBusStamp()]);        }    });}

As you see, we only need one command method in this case, not query+command.

@vudaltsov
Copy link
Contributor

vudaltsov commentedSep 30, 2019
edited
Loading

Also, what if we implement this as a middleware, so that we don't depend on Doctrine here? It could do the same thing as the subscriber after thedoctrine_transaction middleware. It will operate on any message object, not on a Doctrine entity.

$this->collectEventsFromEntity($event);
}

private function collectEventsFromEntity(LifecycleEventArgs $message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

if the method is calledcollectEventsFromEntity, then it should acceptobject $entity as argument?

];
}

public function postFlush(LifecycleEventArgs $event)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This code will fail.
ThepostFlush event is not a lifecycle event. It passes an instance ofDoctrine\ORM\Event\PostFlushEventArgs which is not a subclass ofLifecycleEventArgs. Seehttps://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#postflush .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

So this event cannot be used here at all, because at this point you do not know which entities were flushed.

@Nyholm
Copy link
MemberAuthor

Im closing this in favor of#34310

@NyholmNyholm closed thisMay 3, 2020
@nicolas-grekasnicolas-grekas modified the milestones:next,5.1May 4, 2020
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

6 more reviewers

@jakzaljakzaljakzal left review comments

@TobionTobionTobion left review comments

@ro0NLro0NLro0NL left review comments

@ogizanagiogizanagiogizanagi left review comments

@vudaltsovvudaltsovvudaltsov requested changes

@srozesrozesroze approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

5.1

Development

Successfully merging this pull request may close these issues.

12 participants

@Nyholm@Koc@fabpot@sroze@maxhelias@vudaltsov@jakzal@Tobion@ro0NL@ogizanagi@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp