-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from agrare/map_state_add_tolerated_failure
Map state add tolerated failure
- Loading branch information
Showing
5 changed files
with
222 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module Floe | ||
class Workflow | ||
module States | ||
module RetryCatchMixin | ||
def find_retrier(error) | ||
self.retry.detect { |r| r.match_error?(error) } | ||
end | ||
|
||
def find_catcher(error) | ||
self.catch.detect { |c| c.match_error?(error) } | ||
end | ||
|
||
def retry_state!(context, error) | ||
retrier = find_retrier(error["Error"]) if error | ||
return if retrier.nil? | ||
|
||
# If a different retrier is hit reset the context | ||
if !context["State"].key?("RetryCount") || context["State"]["Retrier"] != retrier.error_equals | ||
context["State"]["RetryCount"] = 0 | ||
context["State"]["Retrier"] = retrier.error_equals | ||
end | ||
|
||
context["State"]["RetryCount"] += 1 | ||
|
||
return if context["State"]["RetryCount"] > retrier.max_attempts | ||
|
||
wait_until!(context, :seconds => retrier.sleep_duration(context["State"]["RetryCount"])) | ||
context.next_state = context.state_name | ||
context.output = error | ||
logger.info("Running state: [#{long_name}] with input [#{context.json_input}] got error[#{context.json_output}]...Retry - delay: #{wait_until(context)}") | ||
true | ||
end | ||
|
||
def catch_error!(context, error) | ||
catcher = find_catcher(error["Error"]) if error | ||
return if catcher.nil? | ||
|
||
context.next_state = catcher.next | ||
context.output = catcher.result_path.set(context.input, error) | ||
logger.info("Running state: [#{long_name}] with input [#{context.json_input}]...CatchError - next state: [#{context.next_state}] output: [#{context.json_output}]") | ||
|
||
true | ||
end | ||
|
||
def fail_workflow!(context, error) | ||
# next_state is nil, and will be set to nil again in super | ||
# keeping in here for completeness | ||
context.next_state = nil | ||
context.output = error | ||
logger.error("Running state: [#{long_name}] with input [#{context.json_input}]...Complete workflow - output: [#{context.json_output}]") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters