4

I was developing a sample application to test the timeout management in saga using NserviceBus.

I am tryin to achieve the following

When a saga started set it's timeout to 1 minute Before the timeout happens if an update came to the nessage updates the timeout to 5 minutes My code is like below

  public class OrderSaga : Saga<OrderSagaData>,
        IAmStartedByMessages<SampleMessage>,
        IHandleMessages<UpdateMessage>
    {
        public override void ConfigureHowToFindSaga()
        {
            ConfigureMapping<UpdateMessage>(s => s.PurchaseOrderNumber, m => m.Update);
        }

        public void Handle(SampleMessage message)
        {
            this.Data.PurchaseOrderNumber = message.Name;
            RequestTimeout(DateTime.Now.AddMinutes(1), message.Name);
        }


        private void Complete()
        {
            MarkAsComplete();
        }

        public override void Timeout(object state)
        {
            Complete();
        }


        #region IMessageHandler<UpdateMessage> Members

        public void Handle(UpdateMessage message)
        {
            this.Data.PurchaseOrderNumber = message.NewValue;     
            RequestTimeout(DateTime.Now.AddMinutes(5), message.Update);
        }

        #endregion
    }
}

But here the problem is the timeout is not getting updated to 5 minutes.The timeout still works for 1 minute.

Could you please let me know what is doing wrong here?

Thanks in advance, Ajai

Ajai
  • 63
  • 1
  • 4

1 Answers1

4

Saga timeouts can't be updated. They will fire no matter what you do. In your case you will receive both timeouts and given that you call Complete in your timeout handler your saga will end after one minute. You need to add some logic in that takes this into account.

Something like this might do it:

if(!updateReceived or state == ThisTimeoutWasRequestedByMyUpdateHandler)
   Complete();

Hope this helps!

Andreas Öhlund
  • 5,263
  • 20
  • 24
  • Hi Andreas,Thanks for the quick reply.I gave MarkComplete there beacause i am expectiung the saga to be completed after 5 minutes.The solution also you provided will create some issues if i update the value more than two times since it can go upto n level of updation.So it will be good to keep the number of updation count in saga db somethinkg like that? – Ajai Jan 19 '11 at 08:39
  • When is your saga supposed to end? After 5 minutes? After you receive the update? After n Updates? – Andreas Öhlund Jan 19 '11 at 10:15
  • Hi Andreas, is this still the case for NSB 3.3? The ability to update a timeout, i.e. to extend or temporarily suspend it based on handled events, would be extremely useful. – killthrush Feb 06 '13 at 12:07
  • still the same. We can't solve this since what should happen if the timeout fires just as your update request comes through? you need to handle this regardless – Andreas Öhlund Feb 06 '13 at 16:38