2

I have a Saga which should wait for a specific Database-Value to be changed. How do I achieve this?

Example:

public partial class OrderSaga : Saga<OrderSagaData>, IHandleMessages<FinishOrder> {
   public void Handle(FinishOrder message)
   {
      Order order=new Order(message.OrderId);
      if (order.Approved) {
         SendMail(order);
      }
   }
}

When the bool "Approved" of that Order is true I want to send a mail. But this can take hours or even days. How do I tell the Saga to check again in a few hours? I am new to Sagas and NServiceBus so the answer might be trivial, but I just did not find it.

Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • Do you have the ability to make an event be published when an order becomes approved? If so, then subscribe to that event from your saga. – Udi Dahan Feb 17 '15 at 06:10

1 Answers1

1

You can defer any incoming message if you're still waiting for another condition to be met. This will apply a time-out to the message and put it back on the bus for future processing.

Depending on whether you're using NServiceBus 4 or 5, the method signature for a deferral will change but let's assume you're on V4, you can then do something along the lines of this:

if (!order.Approved)
{
    Bus.Defer(TimeSpan.FromHours(1), message);
    //This will defer message handling by an hour
}
else
{
    SendMail(order);
}
Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
  • 1
    No problems, NServiceBus is a bit daunting to use at first, and stupidly powerful as well. A lot of caveats were encountered and overcome in the last couple of months, I'll tell ya :) – Yannick Meeus Feb 16 '15 at 10:38