Hi,
Is it possible to remove instances of sagas after a timeout period? I have three applications working together. If for any reason one is down then the saga instances of another will not be completed and therefore not removed. However I know a saga instance that is working properly will never have to exist more than 20 minutes. Therefore if a saga instance is that old it should be removed. Here is my code.
I have a Saga on a Web Application that uses the In Memory Saga Repository
MassTransit.Bus.Initialize(sbc =>
{
sbc.UseMsmq();
...
sbc.Subscribe(subs =>
{
subs.Saga<ExportWebClientSaga>(new InMemorySagaRepository<ExportWebClientSaga>());
});
});
In my Saga I have configured RemoveWhen to remove the saga if the State is completed
static ExportWebClientSaga()
{
Define(() =>
{
Initially(
When(Response)
.Then((saga, message) => saga.HandleResponse(message))
);
During(NextResponse,
When(Response)
.Then((saga, message) => saga.HandleResponse(message))
);
RemoveWhen(x => x.CurrentState == Completed);
});
}
In HandleResponse I programatically set the CurrentState to Completed
public virtual void HandleResponse(SymphonyExportResponseMessage message)
{
...
if (message.IsFinalResponse)
ChangeCurrentState(Completed);
else if (CurrentState != NextResponse)
ChangeCurrentState(NextResponse);
}
I was thinking I could do the following for the RemoveWhen expression
x => x.CurrentState == Completed || DateTime.Now > x.StartTime.AddMinutes(20)
However I think that expression will only run when there is a saga event. Is that right? Is there another way of doing this?
Thank you,
Regards Ben