Class: Zoidberg::Task
- Inherits:
-
Object
- Object
- Zoidberg::Task
- Defined in:
- lib/zoidberg/task.rb
Overview
Run a task
Constant Summary collapse
- SUPPORTED_STYLES =
Supported task styles
[:serial, :async]
Instance Attribute Summary collapse
-
#content ⇒ Proc
readonly
Block to execute.
-
#content_arguments ⇒ Object
readonly
Returns the value of attribute content_arguments.
-
#origin ⇒ Object
readonly
Originator of task.
-
#style ⇒ Symbol
readonly
:fiber or :thread.
-
#task ⇒ Thread, Fiber
readonly
Underlying task container.
Instance Method Summary collapse
-
#cease(*args) ⇒ Object+
Reliquish running state and return optional value(s).
-
#complete? ⇒ TrueClass, FalseClass
Task is complete.
-
#error? ⇒ TrueClass, FalseClass
Task complete in error state.
-
#halt! ⇒ NilClass
Force task to stop prior to completion if still in running state.
-
#initialize(task_style, origin, block_args = []) { ... } ⇒ self
constructor
Create a new task.
-
#proceed(*args) ⇒ Object+
Regain running state with optional value(s).
-
#running? ⇒ TrueClass, FalseClass
Task is running.
-
#success? ⇒ TrueClass, FalseClass
Task complete in success state.
-
#value ⇒ Object
Result of task.
-
#waiting? ⇒ TrueClass, FalseClass
Task currently waiting to run.
Constructor Details
#initialize(task_style, origin, block_args = []) { ... } ⇒ self
Create a new task
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/zoidberg/task.rb', line 27 def initialize(task_style, origin, block_args=[], &block) unless(SUPPORTED_STYLES.include?(task_style)) raise ArgumentError.new("Allowed style values: #{SUPPORTED_STYLES.map(&:inspect).join(', ')} but received: #{task_style.inspect}") end @style = task_style @origin = origin @content = block @content_arguments = block_args @result = nil send("run_#{style}") end |
Instance Attribute Details
#content ⇒ Proc (readonly)
Returns block to execute.
16 17 18 |
# File 'lib/zoidberg/task.rb', line 16 def content @content end |
#content_arguments ⇒ Object (readonly)
Returns the value of attribute content_arguments.
17 18 19 |
# File 'lib/zoidberg/task.rb', line 17 def content_arguments @content_arguments end |
#origin ⇒ Object (readonly)
Returns originator of task.
14 15 16 |
# File 'lib/zoidberg/task.rb', line 14 def origin @origin end |
#style ⇒ Symbol (readonly)
Returns :fiber or :thread.
12 13 14 |
# File 'lib/zoidberg/task.rb', line 12 def style @style end |
#task ⇒ Thread, Fiber (readonly)
Returns underlying task container.
19 20 21 |
# File 'lib/zoidberg/task.rb', line 19 def task @task end |
Instance Method Details
#cease(*args) ⇒ Object+
Reliquish running state and return optional value(s)
91 92 93 94 95 96 97 98 99 |
# File 'lib/zoidberg/task.rb', line 91 def cease(*args) if(style == :async) task[:task_args] = args task.stop task[:task_args] else Fiber.yield(*args) end end |
#complete? ⇒ TrueClass, FalseClass
Returns task is complete.
73 74 75 |
# File 'lib/zoidberg/task.rb', line 73 def complete? task.nil? || !task.alive? end |
#error? ⇒ TrueClass, FalseClass
Returns task complete in error state.
78 79 80 |
# File 'lib/zoidberg/task.rb', line 78 def error? complete? && value.is_a?(Exception) end |
#halt! ⇒ NilClass
Force task to stop prior to completion if still in running state
50 51 52 53 54 55 56 |
# File 'lib/zoidberg/task.rb', line 50 def halt! if(style == :async) task.kill else @task = nil end end |
#proceed(*args) ⇒ Object+
Regain running state with optional value(s)
104 105 106 107 108 109 110 111 112 |
# File 'lib/zoidberg/task.rb', line 104 def proceed(*args) if(style == :serial) task.resume(*args) else task[:task_args] = args task.run task[:task_args] end end |
#running? ⇒ TrueClass, FalseClass
Returns task is running.
64 65 66 67 68 69 70 |
# File 'lib/zoidberg/task.rb', line 64 def running? if(task) style == :async && task.alive? && !task.stop? else false end end |
#success? ⇒ TrueClass, FalseClass
Returns task complete in success state.
83 84 85 |
# File 'lib/zoidberg/task.rb', line 83 def success? complete? && !error? end |
#value ⇒ Object
Returns result of task.
40 41 42 43 44 45 |
# File 'lib/zoidberg/task.rb', line 40 def value if(task.alive?) @result = style == :async ? task.join : task.resume end @result end |
#waiting? ⇒ TrueClass, FalseClass
Returns task currently waiting to run.
59 60 61 |
# File 'lib/zoidberg/task.rb', line 59 def waiting? task && task.alive? && task.respond_to?(:stop?) ? task.stop? : true end |