Giter VIP home page Giter VIP logo

Comments (4)

brucezlata avatar brucezlata commented on July 20, 2024

Use saga pattern
https://workflow-core.readthedocs.io/en/latest/sagas/

from workflow-core.

MOwaisfarooq avatar MOwaisfarooq commented on July 20, 2024

@brucezlata Can you please let me know how I can implement saga transaction. I am using EventPublish to forward the workflow step and in each step i have perform some database transaction through service injection. if error raises on any step, it will suspend the workflow and rollback my service transaction for current step only.

from workflow-core.

brucezlata avatar brucezlata commented on July 20, 2024

@MOwaisfarooq you can check the above link, for example, throw exception once there is any error in any of your steps (Tasks), then, compensate entire saga transaction (UndoEverything).

builder
    .StartWith(context => Console.WriteLine("Begin"))
        .Saga(saga => saga
            .StartWith<Task1>()
            .Then<Task2>()
            .Then<Task3>()
    )
        .CompensateWith<**UndoEverything**>()
    .Then(context => Console.WriteLine("End"));

from workflow-core.

MOwaisfarooq avatar MOwaisfarooq commented on July 20, 2024

If an error occurs in step B of the workflow, I suspend the workflow to publish this event later from the controller API. The _workflowStepDetailsService.UpdateTransactionForStepB has made a transaction in my table stepTable. I want to revert only the step B service transaction that updated the table. I don't want to undo everything, so that next time I can continue from step B after the error. For example, if I made an insertion in step A using _workflowStepDetailsService.CreateTransactionforStepA in my stepTable, I want to preserve that transaction and only undo the changes made by step B.

builder .StartWith(context => Console.WriteLine("Begin")) .UseDefaultErrorBehavior(WorkflowErrorHandling.Suspend) .Saga(saga => saga .StartWith<stepA>() .Then<stepB>() .Then<stepC>() ) .CompensateWith<**UndoEverything**>() .Then(context => Console.WriteLine("End"));
``

public class stepA: StepBodyAsync
{


    private readonly IWorkflowStepDetailsService _workflowStepDetailsService;

    public stepA(IWorkflowStepDetailsService workflowStepDetailsService)
    {
        _workflowStepDetailsService = workflowStepDetailsService;
    }

    public override async Task<ExecutionResult> RunAsync(IStepExecutionContext context)
    {

        if (!context.ExecutionPointer.EventPublished)
        {
            return ExecutionResult.WaitForEvent("StartWorkflowEvent", context.Workflow.Id, DateTime.Now);
        }

        var workflowsteprequest = (WorkFlowStepDetails)context.ExecutionPointer.EventData;
        await _workflowStepDetailsService.CreateTransactionforStepA(workflowsteprequest);

        return ExecutionResult.Next();
    }
}

``
public class stepB : StepBodyAsync
{
private readonly IWorkflowStepDetailsService _workflowStepDetailsService;

    public stepB: (IWorkflowStepDetailsService workflowStepDetailsService)
    {
        _workflowStepDetailsService = workflowStepDetailsService;
    }

    public override async Task<ExecutionResult> RunAsync(IStepExecutionContext context)
    {

        if (!context.ExecutionPointer.EventPublished)
        {
            return ExecutionResult.WaitForEvent("StartWorkflowEvent", context.Workflow.Id, DateTime.Now);
        }

        var workflowsteprequest = (WorkFlowStepDetails)context.ExecutionPointer.EventData;

        await _workflowStepDetailsService.UpdateTransactionForStepB(workflowsteprequest);

        return ExecutionResult.Next();
    }

}
``

from workflow-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.