Ajai
Ajai

Reputation: 63

NServiceBus TimeOut Manager

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

Upvotes: 4

Views: 4404

Answers (1)

Andreas &#214;hlund
Andreas &#214;hlund

Reputation: 5273

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!

Upvotes: 4

Related Questions