class Garcon::SafeTaskExecutor
A simple utility class that executes a callable and returns and array of three elements:
-
success: indicating if the callable has been executed without errors
-
value: filled by the callable result if it has been executed without
errors, nil otherwise
-
reason: the error risen by the callable if it has been executed with
errors, nil otherwise
Public Class Methods
new(task, opts = {})
click to toggle source
# File lib/garcon/task/safe_task_executor.rb, line 34 def initialize(task, opts = {}) @task = task @mutex = Mutex.new @ex = opts.fetch(:rescue_exception, false) ? Exception : StandardError end
Public Instance Methods
execute(*args)
click to toggle source
@return [Array]
# File lib/garcon/task/safe_task_executor.rb, line 41 def execute(*args) @mutex.synchronize do success = false value = reason = nil begin value = @task.call(*args) success = true rescue @ex => e reason = e success = false end [success, value, reason] end end