Class: Gearman::Worker::Ability

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(func_name, block, timeout = nil) ⇒ Ability

Create a new ability. Setting timeout means we register with CAN_DO_TIMEOUT

Parameters:

  • func_name

    Function name of this ability

  • block

    Code to run

  • timeout (defaults to: nil)

    Server gives up on us after this many seconds



10
11
12
13
14
15
# File 'lib/gearman/worker/ability.rb', line 10

def initialize(func_name, block, timeout=nil)
  @func_name = func_name
  @block = block
  @timeout = timeout
  @on_complete = nil
end

Instance Attribute Details

#func_nameObject (readonly)

Returns the value of attribute func_name.



17
18
19
# File 'lib/gearman/worker/ability.rb', line 17

def func_name
  @func_name
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/gearman/worker/ability.rb', line 17

def timeout
  @timeout
end

Instance Method Details

#after_complete(&block) ⇒ Object

Add an after-ability hook

The passed-in block of code will be executed after the work block for jobs with the same function name. It takes two arguments, the result of the work and the original job data. This way, if you need to hook into after the job_complete packet is sent to the server, you can do so.

N.B The after-ability hook ONLY runs if the ability was successful and no exceptions were raised.

Parameters:

  • func

    function name (without prefix)



47
48
49
# File 'lib/gearman/worker/ability.rb', line 47

def after_complete(&block)
  @on_complete = block
end

#run(data, job) ⇒ Object

Run the block of code given for a job of this type.

Parameters:

  • data

    data passed to us by a client

  • job

    interface to report job information to the server



24
25
26
27
28
29
30
31
32
# File 'lib/gearman/worker/ability.rb', line 24

def run(data, job)
  begin
    result = @block.call(data, job) if @block
    @on_complete.call(result, data) if @on_complete
    return result
  rescue => ex
    raise ex
  end
end