Skip to content

Completed saga returned from static Start is persisted regardless of completed state #2073

@agross

Description

@agross

Describe the bug

Returning a saga instance that was MarkCompleted from a static Start method does not cause persistence to skip.

To Reproduce

namespace Testing;

public record StaticMessage;

public class StartIsStaticMethod : Saga
{
  public string? Id { get; set; }

  public static StartIsStaticMethod Start(StaticMessage message)
  {
    var saga = new StartIsStaticMethod();
    saga.MarkCompleted();

    return saga;
  }
}

This generates:

// <auto-generated/>
#pragma warning disable
using Microsoft.Extensions.Logging;

namespace Internal.Generated.WolverineHandlers
{
    // START: StaticMessageHandler426984011
    [global::System.CodeDom.Compiler.GeneratedCode("JasperFx", "1.0.0")]
    public sealed class StaticMessageHandler426984011 : Wolverine.Runtime.Handlers.MessageHandler
    {
        public override async System.Threading.Tasks.Task HandleAsync(Wolverine.Runtime.MessageContext context, System.Threading.CancellationToken cancellation)
        {
            // The actual message body
            var staticMessage = (Testing.StaticMessage)context.Envelope.Message;

            await using var sagaStorage = await Wolverine.Persistence.Sagas.SagaSupport<string, Testing.StartIsStaticMethod>.EnrollAndFetchSagaStorage(context);
            var sagaStorage_Slim = sagaStorage;
            
            // The actual message execution
            var outgoing1 = Testing.StartIsStaticMethod.Start(staticMessage);

            // This should check for "outgoing".IsCompleted.
            await sagaStorage_Slim.InsertAsync(outgoing1, cancellation);
            await sagaStorage.SaveChangesAsync(cancellation);
        }

    }
}

Expected behavior

Same behavior as with a non-static Start method, where IsCompleted is checked before calling the persistence.

namespace Testing;

public record InstanceMessage;

public class StartIsInstanceMethod : Saga
{
  public string? Id { get; set; }

  public void Start(InstanceMessage message)
  {
    MarkCompleted();
  }
}

generates:

await using var sagaStorage = await Wolverine.Persistence.Sagas.SagaSupport<string, Testing.StartIsInstanceMethod>.EnrollAndFetchSagaStorage(context);
var sagaStorage_Slim = sagaStorage;
var startIsInstanceMethod = new Testing.StartIsInstanceMethod();

// The actual message execution
startIsInstanceMethod.Start(instanceMessage);

if (!startIsInstanceMethod.IsCompleted())
{
    await sagaStorage_Slim.InsertAsync(startIsInstanceMethod, cancellation);
}

// No additional Unit of Work necessary
await sagaStorage.SaveChangesAsync(cancellation);

Wolverine 5.12.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions