Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you jump to any step? #1283

Open
Hiprox opened this issue Aug 9, 2024 · 0 comments
Open

How do you jump to any step? #1283

Hiprox opened this issue Aug 9, 2024 · 0 comments

Comments

@Hiprox
Copy link

Hiprox commented Aug 9, 2024

The problem is that there are steps where you can't get an object from the database because it may not already exist and this logic is repeated in different steps.

The solutions I see are:

  • Make typed exceptions so that you can distinguish them in OnStepError and call host.TerminateWorkflow(workflowId);
  • Create a step, for example RecordNotFoundStep and use it after each it is required. But this is not convenient, as another developer needs to understand where this step is required, and the workflow configuration will look like something messy.

Ideal solution: have it possible to call a step that will execute and the workflow will finish.

public class RequestBuffersStep(
    ISender mediator,
    IMarkingDbContext dbContext,
    ILogger? logger = default) : StepBodyAsync
{
    public int OrganizationId { get; set; }

    public override async Task<ExecutionResult> RunAsync(IStepExecutionContext context)
    {
        var query = new GetOrganizationQuery(OrganizationId: OrganizationId);

        var getOrganizationResponse = await mediator.Send(query)
            .ConfigureAwait(false);

        // If there is an error when trying to get the organization from the database
        if (getOrganizationResponse.IsError)
        {
            await SetErrorMessageToOrderAndThrowExceptionAsync(getOrganizationResponse.FirstError.Description,
                context.CancellationToken);
        }

        var organization = getOrganizationResponse.Value;

        // ...

        return ExecutionResult.Next();
    }

    private async Task SetErrorMessageToOrderAndThrowExceptionAsync(string message, CancellationToken cancellationToken)
    {
        var order = await dbContext.Orders.Where(o => o.Id == OrderData.Id)
            .FirstOrDefaultAsync(cancellationToken)
            .ConfigureAwait(false);

        if (order is null)
        {
            logger?.Error("Order with ID {Id} not found in database.", OrderData.Id);
            throw new Exception($"Order with ID {OrderData.Id} not found in database."); // throw exception and terminate workflow in OnStepError
        }

        var error = new Error(message);

        order.SetError(error);

        await dbContext.SaveChangesAsync(cancellationToken);

        throw new Exception(message); // throw exception and terminate workflow in OnStepError
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant