Class: Workers::TaskGroup

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/workers/task_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @logger = Workers::LogProxy.new(options[:logger])
  @pool = options[:pool] || Workers.pool
  @state = :initialized
  @tasks = []
  @internal_lock = Mutex.new
  @external_lock = Mutex.new
  @finished_count = 0
  @conditional = ConditionVariable.new

  nil
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



5
6
7
# File 'lib/workers/task_group.rb', line 5

def state
  @state
end

#tasksObject (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(options = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/workers/task_group.rb', line 21

def add(options = {}, &block)
  state!(:initialized)

  options[:on_finished] = method(:finished)
  options[:on_perform] ||= block

  @tasks << Workers::Task.new(options)

  nil
end

#failuresObject



61
62
63
# File 'lib/workers/task_group.rb', line 61

def failures
  @tasks.select { |t| t.failed? }
end

#map(inputs, options = {}, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/workers/task_group.rb', line 65

def map(inputs, options = {}, &block)
  inputs.each do |input|
    add(:input => input, :max_tries => options[:max_tries]) do |i|
      block.call(i)
    end
  end

  run

  if (failure = failures[0])
    a = failure.input.inspect
    c = failure.exception.class.to_s
    m = failure.exception.message
    b = failure.exception.backtrace.join("\n")

    raise Workers::FailedTaskError, "#{failures.count} task(s) failed (Only the first failure is shown).\nARGS=#{a}, EXCEPTION=#{c}: #{m}\n#{b}\n----------\n"
  end

  tasks.map { |t| t.result }
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/workers/task_group.rb', line 32

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

    loop do
      @conditional.wait(@internal_lock)
      # The wait can return even if nothing called @conditional.signal,
      # so we need to check to see if the condition actually changed.
      # See https://github.com/chadrem/workers/issues/7
      break if all_tasks_finished?
    end
  end

  @tasks.all? { |t| t.succeeded? }
end

#successesObject



57
58
59
# File 'lib/workers/task_group.rb', line 57

def successes
  @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);



88
89
90
91
92
# File 'lib/workers/task_group.rb', line 88

def synchronize(&block)
  @external_lock.synchronize { block.call }

  nil
end