Class: Geary::Performer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid
Defined in:
lib/geary/performer.rb

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Performer

Returns a new instance of Performer.



15
16
17
18
# File 'lib/geary/performer.rb', line 15

def initialize(address)
  @address = address
  configure_connection Gearman::Worker.method(:new_link)
end

Instance Method Details

#build_connectionObject



63
64
65
# File 'lib/geary/performer.rb', line 63

def build_connection
  @gearman = @connect.call(@address)
end

#configure_connection(connection_routine) ⇒ Object



67
68
69
70
# File 'lib/geary/performer.rb', line 67

def configure_connection(connection_routine)
  @connect = connection_routine
  reconnect(current_actor, nil)
end

#disconnectObject



52
53
54
55
56
# File 'lib/geary/performer.rb', line 52

def disconnect
  if @gearman
    @gearman.terminate if @gearman.alive?
  end
end

#perform(packet) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/geary/performer.rb', line 37

def perform(packet)
  job = JSON.parse(packet.data)
  job_result = nil

  begin
    worker = ::Object.const_get(job['class']).new

    job_result = worker.perform(*job['args'])
  rescue => error
    @gearman.async.work_exception(packet.handle, error.message)
  else
    @gearman.async.work_complete(packet.handle, job_result)
  end
end

#reconnect(actor, reason) ⇒ Object



58
59
60
61
# File 'lib/geary/performer.rb', line 58

def reconnect(actor, reason)
  disconnect
  build_connection
end

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/geary/performer.rb', line 20

def start
  @gearman.can_do('Geary.default')

  loop do
    packet = @gearman.grab_job

    case packet
    when Gearman::Packet::JOB_ASSIGN
      perform(packet)
    when Gearman::Packet::NO_JOB
      @gearman.pre_sleep
    else
      break
    end
  end
end