workflow-core icon indicating copy to clipboard operation
workflow-core copied to clipboard

How to conditionally suspend a workflow after an error

Open arturek opened this issue 7 years ago • 12 comments

I'd like control how each step (and the whole workflow) behaves after an exception is thrown. For example depending on the exception I'd like to repeat the step a variable number of times and then suspend the workflow the error is persistent. Is there any way to do it? From within an OnStepError handler I cannot do anything (probably because the workflow instance is still locked). On the other hand OnError doesn't provide any extension points.

arturek avatar Feb 08 '18 14:02 arturek

Hi @arturek

This is a definitely something I would like to add to the library, unfortunately, the current version it does not support complex error handling scenarios but the plan is to add this in a future version, if you would like to contribute this feature to the project, that would be awesome.

danielgerlag avatar Feb 12 '18 21:02 danielgerlag

@danielgerlag what error handling features do you have in mind? If we can enumerate them, it may help contributors to come up with something.

Additional things to track per workflow instance:

  1. retry count

Things to capture in onError (instead of relying on the ReportError event)

  1. exception type
  2. retry count

OnError can have the following signature OnError(Action<ErrorContext, ErrorHandlingResult>) where

  • ErrorContext - contains information about the error (type, retry count)
  • ErrorHandlingResult - determine how to handle the error

Miggleness avatar Nov 30 '18 09:11 Miggleness

@danielgerlag is there a recommended way on how to handle different errors within the current version of the framework?

Ex: If no internet connection > retry If filenotfound exception > terminate

gonengf avatar May 24 '19 07:05 gonengf

There is currently no built in way to do this, but you could catch the exceptions inside your steps and output results that the workflow can make decisions on.

danielgerlag avatar May 25 '19 22:05 danielgerlag

Hi @danielgerlag,

I would like to work on @Miggleness proposition. Would it be possible for you to give some directions on where to start?

Thanks in advance!

gonengf avatar Jul 03 '19 12:07 gonengf

@gonengf you'd need to implement the core logic in ExecutionResultProcessor And then extend the OnError fluent method to take exception types as an optional parameter. Also, would need to add it to the JSON format here

danielgerlag avatar Jul 10 '19 02:07 danielgerlag

As an option you can consider using Polly.NET embedded in your workflow, it is a resiliency framework, it has all that behavior built in and it is pretty good at that.

emardini avatar Oct 14 '19 13:10 emardini

@danielgerlag I'd like to try to contribute, but I lack understanding of some details. How do I save some error counter information in the step? As far as I understand now, I need to change handler logic in ExecutionResultProcessor line 116-120.

MrZoidberg avatar Sep 08 '20 21:09 MrZoidberg

@danielgerlag please take a look at this POC: https://github.com/MrZoidberg/workflow-core/pull/1. I see some code that I don't like, but before making changes I'd like to check your opinion.

MrZoidberg avatar Sep 09 '20 12:09 MrZoidberg

I think we'd want some kind of policy object where you could chain multiple behaviors together, rather than a static single fallback. Something like the way this library works - https://github.com/App-vNext/Polly

danielgerlag avatar Sep 10 '20 02:09 danielgerlag

@danielgerlag hey, was on vacation. could you please write some code example of how you want this chain behavior to be used?

MrZoidberg avatar Sep 23 '20 09:09 MrZoidberg

Looks like using middleware with Polly can workaround the issue: https://workflow-core.readthedocs.io/en/latest/workflow-middleware/#step-middleware E.g. implement retries for the step with Polly and add .OnError(WorkflowErrorHandling.Suspend) after the step.

SergiiKram avatar Nov 11 '21 11:11 SergiiKram