Class: Workers::TaskGroup
- Inherits:
-
Object
- Object
- Workers::TaskGroup
- Includes:
- Helpers
- Defined in:
- lib/workers/task_group.rb
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
Instance Method Summary collapse
- #add(*args, &block) ⇒ Object
- #failures ⇒ Object
-
#initialize(options = {}) ⇒ TaskGroup
constructor
A new instance of TaskGroup.
- #map(inputs, &block) ⇒ Object
- #run ⇒ Object
- #successes ⇒ Object
-
#synchronize(&block) ⇒ Object
Convenient mutex to be used by a users’s task code that needs serializing.
Methods included from Helpers
#concat_e, #log_debug, #log_error, #log_info, #log_warn
Constructor Details
#initialize(options = {}) ⇒ TaskGroup
Returns a new instance of TaskGroup.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/workers/task_group.rb', line 8 def initialize( = {}) @logger = Workers::LogProxy.new([:logger]) @pool = [:pool] || Workers.pool @state = :initialized @tasks = [] @internal_lock = Mutex.new @external_lock = Mutex.new @finished_count = 0 @conditional = ConditionVariable.new return nil end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
5 6 7 |
# File 'lib/workers/task_group.rb', line 5 def state @state end |
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
6 7 8 |
# File 'lib/workers/task_group.rb', line 6 def tasks @tasks end |
Instance Method Details
#add(*args, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/workers/task_group.rb', line 21 def add(*args, &block) state!(:initialized) if args[0].is_a?(Workers::Task) @tasks << args[0] else @tasks << Workers::Task.new(:args => args, :perform => block, :finished => method(:finished)) end return nil end |
#failures ⇒ Object
56 57 58 |
# File 'lib/workers/task_group.rb', line 56 def failures return @tasks.select { |t| t.failed? } end |
#map(inputs, &block) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/workers/task_group.rb', line 60 def map(inputs, &block) inputs.each do |input| add(input) do |i| block.call(i) end end run if (failure = failures[0]) a = failure.args.inspect m = failure.exception. b = failure.exception.backtrace.join("\n") raise "At least one task failed. ARGS=#{a}, TRACE=#{m}\n#{b}\n----------\n" end return tasks.map { |t| t.result } end |
#run ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/workers/task_group.rb', line 33 def run state!(:initialized) @state = :running @run_thread = Thread.current return [] if @tasks.empty? @internal_lock.synchronize do @tasks.each do |task| @pool.perform { task.run } end @conditional.wait(@internal_lock) end return @tasks.all? { |t| t.succeeded? } end |
#successes ⇒ Object
52 53 54 |
# File 'lib/workers/task_group.rb', line 52 def successes return @tasks.select { |t| t.succeeded? } end |
#synchronize(&block) ⇒ Object
Convenient mutex to be used by a users’s task code that needs serializing. This should NEVER be used by TaskGroup code (use the @internal_lock instead);
82 83 84 85 86 |
# File 'lib/workers/task_group.rb', line 82 def synchronize(&block) @external_lock.synchronize { block.call } return nil end |