How to conditionally suspend a workflow after an error
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.
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 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:
- retry count
Things to capture in onError (instead of relying on the ReportError event)
- exception type
- 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
@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
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.
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 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
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.
@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.
@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.
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 hey, was on vacation. could you please write some code example of how you want this chain behavior to be used?
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.