Class: Gearman::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_servers, opts = {}) ⇒ Worker

Returns a new instance of Worker.



6
7
8
9
10
11
12
13
# File 'lib/gearman/worker.rb', line 6

def initialize(job_servers, opts = {})
  @reactors = []
  @abilities = {}

  @job_servers = Array[*job_servers]

  @opts = opts
end

Instance Attribute Details

#abilitiesObject (readonly)

Returns the value of attribute abilities.



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

def abilities
  @abilities
end

Instance Method Details

#add_ability(name, timeout = nil, &f) ⇒ Object



15
16
17
18
# File 'lib/gearman/worker.rb', line 15

def add_ability(name, timeout = nil, &f)
  remove_ability(name) if @abilities.has_key?(name)
  @abilities[name] = { :callback => f, :timeout => timeout }
end

#has_ability?(name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/gearman/worker.rb', line 24

def has_ability?(name)
  @abilities.has_key?(name)
end

#remove_ability(name) ⇒ Object



20
21
22
# File 'lib/gearman/worker.rb', line 20

def remove_ability(name)
  @abilities.delete(name)
end

#workObject



28
29
30
31
32
33
34
35
36
# File 'lib/gearman/worker.rb', line 28

def work
  EM.run do
    @job_servers.each do |hostport|
      host, port = hostport.split(":")
      opts = { :abilities => @abilities }.merge(@opts)
      Gearman::Evented::WorkerReactor.connect(host, port, opts)
    end
  end
end