Class: Gearman::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



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

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

  @job_servers = Array[*job_servers]

  @uniq = opts.delete(:uniq)
  @opts = opts
end

Instance Attribute Details

#jobsObject

Returns the value of attribute jobs.



3
4
5
# File 'lib/gearman/client.rb', line 3

def jobs
  @jobs
end

#uniqObject

Returns the value of attribute uniq.



3
4
5
# File 'lib/gearman/client.rb', line 3

def uniq
  @uniq
end

Instance Method Details

#run(taskset, timeout = nil, async = false) ⇒ Object

Run a Task or Taskset



16
17
18
19
20
21
22
23
24
25
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
59
# File 'lib/gearman/client.rb', line 16

def run(taskset, timeout = nil, async = false)
  timeout ||= 0
  use_em_stop = EM.reactor_running?
  EM.run do
    @taskset ||= Taskset.new
    @taskset += Taskset.create(taskset)

    job_servers = @job_servers.dup
    @reactors.each do |reactor|
      unless reactor.connected?
        reactor.reconnect true
        reactor.keep_connected = async
        reactor.callback { create_job(@taskset.shift, reactor) }
        job_servers.delete reactor.to_s
      else
        create_job(@taskset.shift, reactor)
      end
    end
    job_servers.each do |hostport|
      host, port = hostport.split(":")
      reactor = Gearman::Evented::ClientReactor.connect(host, port, @opts)
      reactor.keep_connected = async
      reactor.callback { create_job(@taskset.shift, reactor) }
      @reactors << reactor
    end

    if timeout > 0
      if use_em_stop
        EM.add_timer(timeout) { EM.stop }
      else
        sleep timeout
      end
    elsif !async
      Thread.new do
        loop do
          sleep 0.1
          live = 0
          @reactors.each {|reactor| live += 1 if reactor.connected? }
          break if live == 0
        end
      end.join
    end
  end
end