Search code examples
masstransitsagaautomatonymous

UseInMemoryOutbox in Masstransit State Machine, Preserve Order of Publish Messages


I have a MT state machine which is configured with UseInMemoryOutbox() so any messages that are part of the event handling will be published only after the final step in the pipeline is successfully done, which in my case is, state persistence to Couchbase.

        Initially(When(A).
                    ThenAsync(async context =>
                    {   
                        context.Instance.IsAutoApprove = context.Data.IsAutoApprove;
                        IList<Task> publishTasks = context.Data.SomeList.Select(item =>
                        {
                            MessageA message = new MessageA
                            {
                                Content = "contentA"
                            };
                            return context.Publish(message);
                        }).ToList();

                        await Task.WhenAll(publishTasks);
                    }).TransitionTo(B));

        WhenEnter(B, f => f.If(c => c.Instance.IsAutoApprove,
            callback =>
            callback.TransitionTo(C)
            ));

        WhenEnter(C, c => c.Publish<SagaState, MessageC>(context =>
                     new MessageC
                     {
                         Content = "contentC"
                     }));

I've noticed that the publish messages are published eventually, in an inconsistent order.

For example - assume there are two items within the "SomeList" in the first event, so the messages are published sometimes in this order:

MessageC
MessageA
MessageA

and sometimes

MessageA
MessageA
MessageC

and so on. The order of the logical flow is not preserved.

Can you please assist me with this behavior?

I'm using

MassTransit 3.5.2

MassTransit.Automatonymous 3.5.2

MassTransit.RabbitMQ 3.5.2

Automatonymous 3.5.11

GreenPipes 1.0.9

Thanks,


Solution

  • This was something that was done for performance reasons, but doesn't really make sense, so I've submitted an issue and closed it.

    https://github.com/MassTransit/MassTransit/issues/945

    With v4, actions will be executed in order.