New in Symfony 8.2 (in development). Confirming an order means several steps in order: charge the card, then ship. And if one fails, what has been done must be undone: that's a saga. Until now, Messenger left the ordering and the compensation to the code that sends the messages.
Chaining messages
ChainStamp::envelope() takes messages (or envelopes, to add stamps to them) and returns an envelope to send:
$bus->dispatch(ChainStamp::envelope( new PayerCommande($id), new ExpedierCommande($id), ));
Each step is only sent once the previous one has been handled by all its handlers. The remaining steps travel inside the stamp, so a step retried after a failure resumes the chain where it was. If a step fails for good, the chain stops.
Compensating a failure
DispatchOnFailureStamp sends a message when the stamped message fails for good: after all the attempts of a worker, or as soon as the exception is thrown with a synchronous dispatch.
$bus->dispatch( ChainStamp::envelope(new PayerCommande($id), new ExpedierCommande($id)) ->with(new DispatchOnFailureStamp(new AnnulerCommande($id))) );
The stamp passes from step to step with the chain: the compensation message goes out only once, whichever step breaks. Its handler can receive, after the message, a FailedMessageStamp (the message that failed) and an ErrorDetailsStamp (the error).
Your task
CommandeController sends the payment, then shipping, one after the other. The requests in the “Requests” tab try three orders: A100 goes through, A200 has a declined card, A300 an item out of stock. For those two, the page crashes, and nothing is cancelled.
- Chain the payment and shipping, and have the order cancelled as soon as a step fails.
- Return a 409, with the order's log, when it has been cancelled.