Class: Gearman::Worker::Ability
- Inherits:
-
Object
- Object
- Gearman::Worker::Ability
- Defined in:
- lib/gearman/worker/ability.rb
Instance Attribute Summary collapse
-
#func_name ⇒ Object
readonly
Returns the value of attribute func_name.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#after_complete(&block) ⇒ Object
Add an after-ability hook.
-
#initialize(func_name, block, timeout = nil) ⇒ Ability
constructor
Create a new ability.
-
#run(data, job) ⇒ Object
Run the block of code given for a job of this type.
Constructor Details
#initialize(func_name, block, timeout = nil) ⇒ Ability
Create a new ability. Setting timeout means we register with CAN_DO_TIMEOUT
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_name ⇒ Object (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 |
#timeout ⇒ Object (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.
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.
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 |