RandomUs1r
RandomUs1r

Reputation: 4190

Sending multiple messages in the same saga

Here's my scenario:

A modal fires that sends a message to nservicebus, this modal can fire x times, BUT I only need to send the latest message. I can do this using multiple sagas (1 per message) however for cleanliness I want to do it in 1 saga.

Here's my Bus.Send

busService.Send(new PendingMentorEmailCommand()
{
    PendingMentorEmailCommandId = mentorshipData.CandidateMentorMenteeMatchID,
    MentorshipData = mentorshipData,
    JobBoardCode = Config.JobBoardCode
});

Command Handler:

public void Handle(PendingMentorEmailCommand message)
{
    Data.PendingMentorEmailCommandId = message.PendingMentorEmailCommandId;
    Data.MentorshipData = message.MentorshipData;
    Data.JobBoardCode = message.JobBoardCode;

    RequestTimeout<PendingMentorEmailTimeout>(TimeSpan.FromSeconds(PendingMentorEmailTimeoutValue));

}

Timeout:

public void Timeout(PendingMentorEmailTimeout state)
{
    Bus.Send(new PendingMentorEmailMessage
    {
        PendingMentorEmailCommandId = Data.PendingMentorEmailCommandId,
        MentorshipData = Data.MentorshipData,
        JobBoardCode = Data.JobBoardCode
    });
}

Message handler:

public void Handle(PendingMentorEmailMessage message)
{
    ResendPendingNotification(message);
}

Inside my Resend method, I need to send an email based on a check...

// is there another (newer) message in the queue?
if (currentMentorShipData.DateMentorContacted == message.MentorshipData.DateMentorContacted) 

CurrentMentorShipData is a database pull to get the values at the time of the message.

So I run message one at 10:22 and expect it to fire at 10:25 when I do nothing, however when I send a second message at 10:24, I only want 1 message to fire at 10:27 (the updated one), and nothing to fire at 10:25 because my if condition should fail at 10:25. I'm thinking what's happening is the Saga Data object is getting overridden by the 2nd message causing both messages to fire with DateMentorContacted = 10:24 on the message on the 1st and 2nd message, so my question is how can I persist each message's data individually?

Let me know if I can explain anything else, I'm new to nServiceBus & have tried to provide as much detail as possible.

Upvotes: 0

Views: 262

Answers (1)

Udi Dahan
Udi Dahan

Reputation: 12057

Hearing this statement "I only need to send the latest message", I assume that that would be true for a given application-specific ID (maybe CandidateMentorMenteeMatchID in your case).

I would use that ID as a correlation ID in your saga so that you end up with one saga instance per ID.

Next, I'd have the saga itself filter out the unnecessary message sending.

This can be done by having a kind of sequence number that you store on the saga and pass back in the timeout data. Then, in your timeout handler, you can compare the sequence number currently on the saga against that which came in the timeout data, which would tell you if another message had been received during the timeout. Only if the sequence numbers match would you send the message which would ultimately cause the email to be sent.

Upvotes: 1

Related Questions