Class: Gearman::Worker

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

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Worker

Returns a new instance of Worker.



13
14
15
16
# File 'lib/gearman/worker.rb', line 13

def initialize(address)
  @address = URI(address)
  configure_connection Connection.method(:new_link)
end

Instance Method Details

#build_connectionObject



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

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

#can_do(ability) ⇒ Object



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

def can_do(ability)
  @connection.write(Packet::CAN_DO.new(function_name: ability))
end

#configure_connection(connection_routine) ⇒ Object



55
56
57
58
# File 'lib/gearman/worker.rb', line 55

def configure_connection(connection_routine)
  @connect = connection_routine
  reconnect
end

#disconnectObject



40
41
42
43
44
# File 'lib/gearman/worker.rb', line 40

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

#grab_jobObject



27
28
29
30
# File 'lib/gearman/worker.rb', line 27

def grab_job
  @connection.write(Packet::GRAB_JOB.new)
  @connection.next(Packet::JOB_ASSIGN, Packet::NO_JOB)
end

#pre_sleepObject



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

def pre_sleep
  @connection.write(Packet::PRE_SLEEP.new)
  @connection.next(Packet::NOOP)
end

#reconnect(*_) ⇒ Object



50
51
52
53
# File 'lib/gearman/worker.rb', line 50

def reconnect(*_)
  disconnect
  build_connection
end

#work_complete(handle, data) ⇒ Object



36
37
38
# File 'lib/gearman/worker.rb', line 36

def work_complete(handle, data)
  @connection.write(Packet::WORK_COMPLETE.new(handle: handle, data: data))
end

#work_exception(handle, data) ⇒ Object



32
33
34
# File 'lib/gearman/worker.rb', line 32

def work_exception(handle, data)
  @connection.write(Packet::WORK_EXCEPTION.new(handle: handle, data: data))
end