Class: Concurrent::SafeTaskExecutor
- Inherits:
-
Concurrent::Synchronization::LockableObject
- Object
- Concurrent::Synchronization::LockableObject
- Concurrent::SafeTaskExecutor
- Defined in:
- lib/concurrent/executor/safe_task_executor.rb
Overview
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
Instance Method Summary collapse
- #execute(*args) ⇒ Array
-
#initialize(task, opts = {}) ⇒ SafeTaskExecutor
constructor
A new instance of SafeTaskExecutor.
Constructor Details
#initialize(task, opts = {}) ⇒ SafeTaskExecutor
Returns a new instance of SafeTaskExecutor.
11 12 13 14 15 |
# File 'lib/concurrent/executor/safe_task_executor.rb', line 11 def initialize(task, opts = {}) @task = task @exception_class = opts.fetch(:rescue_exception, false) ? Exception : StandardError super() # ensures visibility end |
Instance Method Details
#execute(*args) ⇒ Array
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/concurrent/executor/safe_task_executor.rb', line 18 def execute(*args) synchronize do success = false value = reason = nil begin value = @task.call(*args) success = true rescue @exception_class => ex reason = ex success = false end [success, value, reason] end end |