Saga
Process Manager Saga PHP
Saga is responsible for coordination of long running processes. It can store information about what happened and make a decision what to do in the right time.

Saga

In Ecotone, Saga is POPO Aggregate,as Aggregate comes with a lot of functionality, that is used in typical Saga implementation. This provides a lot of flexibility, as Aggregate can combine behaviour of Saga and vice versa, when needed.
It's really up to you, if you want to distinct Sagas from Aggregates or combine them, Ecotone does not try to impose the solution.

Storing Saga's State

Depending on the Repository implementation, you may store Saga as event stream or store whole current state.

Handling Saga

1
#[Aggregate]
2
class OrderFulfillment
3
{
4
#[AggregateIdentifier]
5
private string $orderId;
6
7
private bool $isFinished;
8
9
private function __construct(string $orderId)
10
{
11
$this->orderId = $orderId;
12
}
13
14
#[EventHandler]
15
public static function start(OrderWasPlacedEvent $event) : self
16
{
17
return new self($event->getOrderId());
18
}
19
20
#[EventHandler]
21
public function whenPaymentWasDone(PaymentWasFinishedEvent $event, CommandBus $commandBus) : self
22
{
23
if ($this->isFinished) {
24
return;
25
}
26
27
$this->isFinished = true;
28
$commandBus->send(new ShipOrderCommand($this->orderId));
29
}
30
}
Copied!
Aggregate - Saga can stated-stored Aggregate or Event Sourced Aggregate EventHandler - We mark method to be called, when specific event happens.
  • start - is factory methodand should construct new instance OrderFulfillment.Depending on need you may construct differently as Event Sourced Aggregate.
  • paymentWasDone - Is called when PaymentWasFinishedEvent event is published. We have injected CommandBus into the method in order to finish process by sending ShipOrderCommand. We could also publish event instead.

Event Correlation

As Saga is identified by identifier, just like an Aggregate the events need to be correlated with specific instance. When we do have event like PaymentWasFinishedEvent we need to tell Ecotone which instance of OrderFulfillment it should be retrieve from Repository and call method on.
This is done automatically, when property name in Event is the same as property marked as @AggregateIdentifier in aggregate.
1
class PaymentWasFinishedEvent
2
{
3
private string $orderId;
4
}
Copied!
If the property name is different we need to give Ecotone a hint, how to correlate identifiers.
1
class SomeEvent
2
{
3
#[TargetAggregateIdentifier("orderId")]
4
private string $purchaseId;
5
}
Copied!
In other scenario, when there is no property to correlate, we can make use of Before or Presend Interceptors to enrich event's metadata with required identifier. Suppose the orderId identifier is available in metadata under key orderNumber, then we can tell Endpoint to use the mapping.
1
#[EventHandler(identifierMetadataMapping: ["orderId" => "orderNumber"])]
2
public function failPayment(PaymentWasFailedEvent $event, CommandBus $commandBus) : self
3
{
4
// do something with $event
5
}
Copied!

Unordered Events

In the previous example we have assumed, that the first event we will receive is OrderWasPlacedEvent and the second which finishes the Saga is PaymentWasFinishedEvent. It it's always risky to make such assumptions, especially, when events comes from different systems. What we could do instead, is to expect them to come in different order and handle it gracefully.
1
#[Aggregate]
2
class OrderFulfillment
3
{
4
#[AggregateIdentifier]
5
private string $orderId;
6
7
private bool $isFinished;
8
9
private function __construct(string $orderId)
10
{
11
$this->orderId = $orderId;
12
}
13
14
#[EventHandler("whenOrderWasPlaced")]
15
public static function startByPlacedOrder(OrderWasPlacedEvent $event) : self
16
{
17
return new self($event->getOrderId());
18
}
19
20
#[EventHandler("whenPaymentWasDone")]
21
public static function startByPaymentFinished(PaymentWasFinished $event) : self
22
{
23
return new self($event->getOrderId());
24
}
25
26
#[EventHandler("whenOrderWasPlaced")]
27
public function whenOrderWasPlaced(OrderWasPlacedEvent $event, CommandBus $commandBus) : self
28
{
29
if ($this->isFinished) {
30
return;
31
}
32
33
$this->isFinished = true;
34
$commandBus->send(new ShipOrderCommand($this->orderId));
35
}
36
37
#[EventHandler("whenPaymentWasDone")]
38
public function whenPaymentWasDone(PaymentWasFinishedEvent $event, CommandBus $commandBus) : self
39
{
40
if ($this->isFinished) {
41
return;
42
}
43
44
$this->isFinished = true;
45
$commandBus->send(new ShipOrderCommand($this->orderId));
46
}
47
}
Copied!
1
#[EventHandler("whenOrderWasPlaced")]
2
public static function startByPlacedOrder(OrderWasPlacedEvent $event) : self
3
4
#[EventHandler("whenOrderWasPlaced")]
5
public function whenOrderWasPlaced(OrderWasPlacedEvent $event, CommandBus $commandBus) : self
Copied!
If you look closely, you will see that those the factory method startByPlacedOrder and action method whenOrderWasPlaced are handling the same event. If that's the case, Ecotone will verify, before calling factory method, if the aggregate exists and if so, will reroute the event to the action method. This solution will prevent us from depending on the order of events, without introducing routing functionality into our business code.

Ignoring Events

There may be situations, when we will want to handle events, only if Saga already started.
1
#[Aggregate]
2
class OrderFulfillment
3
{
4
#[AggregateIdentifier]
5
private string $customerId;
6
7
private function __construct(string $customerId)
8
{
9
$this->customerId = $customerId;
10
}
11
12
#[EventHandler]
13
public static function start(ReceivedGreatCustomerBadge $event) : void
14
{
15
return new self($event->getCustomerId());
16
}
17
18
#[EventHandler(dropMessageOnNotFound: true)]
19
public function whenNewOrderWasPlaced(OrderWasPlaced $event, CommandBus $commandBus) : void
20
{
21
$commandBus->send(new PromotionCode($this->customerId));
22
}
23
}
Copied!
We want to send promotion code to the customer, if he received great customer badge, but if not we do nothing.
1
EventHandler(dropMessageOnNotFound=true)
Copied!
If this saga instance will be not found, then this event will be dropped and will not call whenNewOrderWasPlaced method.
Options we used in here, can also be applied to Command Handlers
Last modified 8mo ago