Class: Gearman::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/gearman/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, payload = nil, opts = {}) ⇒ Task

Returns a new instance of Task.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gearman/task.rb', line 7

def initialize(name, payload = nil, opts = {})
  @name       = name.to_s
  @payload    = payload || ''
  @priority   = opts.delete(:priority).to_sym rescue nil
  @background = opts.delete(:background) ? true : false

  @retries_done = 0
  @retries      = opts.delete(:retries) || 0

  @poll_status_interval = opts.delete(:poll_status_interval)
  @uniq = opts.has_key?(:uuid) ? opts.delete(:uuid) : `uuidgen`.strip
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



5
6
7
# File 'lib/gearman/task.rb', line 5

def background
  @background
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/gearman/task.rb', line 4

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



4
5
6
# File 'lib/gearman/task.rb', line 4

def payload
  @payload
end

#poll_status_intervalObject

Returns the value of attribute poll_status_interval.



5
6
7
# File 'lib/gearman/task.rb', line 5

def poll_status_interval
  @poll_status_interval
end

#priorityObject

Returns the value of attribute priority.



5
6
7
# File 'lib/gearman/task.rb', line 5

def priority
  @priority
end

#retriesObject

Returns the value of attribute retries.



5
6
7
# File 'lib/gearman/task.rb', line 5

def retries
  @retries
end

#retries_doneObject (readonly)

Returns the value of attribute retries_done.



4
5
6
# File 'lib/gearman/task.rb', line 4

def retries_done
  @retries_done
end

Instance Method Details

#background?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/gearman/task.rb', line 86

def background?
  background
end

#dispatch(event, *args) ⇒ Object



90
91
92
93
# File 'lib/gearman/task.rb', line 90

def dispatch(event, *args)
  callback = instance_variable_get("@#{event}".to_sym)
  callback.call(*args) if callback
end

#hashObject



95
96
97
# File 'lib/gearman/task.rb', line 95

def hash
  @uniq
end

#on_complete(&f) ⇒ Object

Set a block of code to be executed when this task completes successfully. The returned data will be passed to the block.



23
24
25
# File 'lib/gearman/task.rb', line 23

def on_complete(&f)
  @on_complete = f
end

#on_data(&f) ⇒ Object

Set a block of code to be executed when we receive a (partial) data packet for this task. The data received will be passed as an argument to the block.



72
73
74
# File 'lib/gearman/task.rb', line 72

def on_data(&f)
  @on_data = f
end

#on_exception(&f) ⇒ Object

Set a block of code to be executed when a remote exception is sent by a worker. The block will receive the message of the exception passed from the worker. The user can return true for retrying or false to mark it as finished

NOTE: this is actually deprecated, cf. bugs.launchpad.net/gearmand/+bug/405732



48
49
50
# File 'lib/gearman/task.rb', line 48

def on_exception(&f)
  @on_exception = f
end

#on_fail(&f) ⇒ Object

Set a block of code to be executed when this task fails.



29
30
31
# File 'lib/gearman/task.rb', line 29

def on_fail(&f)
  @on_fail = f
end

#on_retry(&f) ⇒ Object

Set a block of code to be executed when this task is retried after failing. The number of retries that have been attempted (including the current one) will be passed to the block.



37
38
39
# File 'lib/gearman/task.rb', line 37

def on_retry(&f)
  @on_retry = f
end

#on_status(&f) ⇒ Object

Set a block of code to be executed when we receive a status update for this task. The block will receive two arguments, a numerator and denominator describing the task’s status.



56
57
58
# File 'lib/gearman/task.rb', line 56

def on_status(&f)
  @on_status = f
end

#on_warning(&f) ⇒ Object

Set a block of code to be executed when we receive a warning from a worker. It is recommended for workers to send work_warning, followed by work_fail if an exception occurs on their side. Don’t expect this behavior from workers NOT using this very library ATM, though. (cf. bugs.launchpad.net/gearmand/+bug/405732)



65
66
67
# File 'lib/gearman/task.rb', line 65

def on_warning(&f)
  @on_warning = f
end

#should_retry?Boolean

Record a failure and check whether we should be retried.

Returns:

  • (Boolean)

    true if we should be resubmitted; false otherwise



80
81
82
83
84
# File 'lib/gearman/task.rb', line 80

def should_retry?
  return false if @retries_done >= @retries
  @retries_done += 1
  true
end