Class: Gearman::TaskSet

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gearman/task_set.rb

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(client) ⇒ TaskSet

Returns a new instance of TaskSet.



7
8
9
10
11
# File 'lib/gearman/task_set.rb', line 7

def initialize(client)
  @client = client
  @tasks_in_progress = []
  @finished_tasks = []
end

Instance Method Details

#add_task(task) ⇒ Object

Add a new task to this TaskSet.

Parameters:

  • args

    A Task object

Returns:

  • true if the task was created successfully, false otherwise



18
19
20
# File 'lib/gearman/task_set.rb', line 18

def add_task(task)
  @tasks_in_progress << task
end

#wait(timeout = 1) ⇒ Object

Wait for all tasks in the set to finish.

Parameters:

  • timeout (defaults to: 1)

    maximum amount of time to wait, in seconds - if this is nil, waits forever



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gearman/task_set.rb', line 26

def wait(timeout = 1)
  end_time = if timeout
    Time.now.to_f + timeout
  else
    nil
  end

  while @tasks_in_progress.length > 0
    remaining = if end_time
      (t = end_time - Time.now.to_f) > 0 ? t : 0
    else
      nil
    end
    begin
      task = @tasks_in_progress.pop
      if
        @client.submit_job(task, true, remaining)
        @finished_tasks << task
      end
    rescue SocketTimeoutError
      return false
    end

  end

  @finished_tasks.each do |t|
    if ( (t.background.nil? || t.background == false) && !t.successful)
      logger.warn 'GearmanRuby: TaskSet failed'
      return false
    end
  end
  true
end

#wait_foreverObject

Wait for all tasks in set to finish, with no timeout



61
62
63
# File 'lib/gearman/task_set.rb', line 61

def wait_forever
  wait(nil)
end