Class: Workers::Worker

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/workers/worker.rb

Instance Method Summary collapse

Methods included from Helpers

#concat_e, #log_debug, #log_error, #log_info, #log_warn

Constructor Details

#initialize(options = {}) ⇒ Worker

Returns a new instance of Worker.



5
6
7
8
9
10
11
# File 'lib/workers/worker.rb', line 5

def initialize(options = {})
  @logger = Workers::LogProxy.new(options[:logger])
  @input_queue = options[:input_queue] || Queue.new
  @thread = Thread.new { start_event_loop }

  return nil
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/workers/worker.rb', line 39

def alive?
  return @thread && @thread.alive?
end

#enqueue(command, data = nil) ⇒ Object



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

def enqueue(command, data = nil)
  @input_queue.push(Event.new(command, data))

  return nil
end

#inspectObject



43
44
45
# File 'lib/workers/worker.rb', line 43

def inspect
  return "#<#{self.class.to_s}:0x#{(object_id << 1).to_s(16)} #{alive? ? 'alive' : 'dead'}>"
end

#join(max_wait = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/workers/worker.rb', line 31

def join(max_wait = nil)
  raise "Worker can't join itself." if @thread == Thread.current

  return true if !@thread.join(max_wait).nil?

  @thread.kill and return false
end

#perform(&block) ⇒ Object



19
20
21
22
23
# File 'lib/workers/worker.rb', line 19

def perform(&block)
  enqueue(:perform, block)

  return nil
end

#shutdown(&block) ⇒ Object



25
26
27
28
29
# File 'lib/workers/worker.rb', line 25

def shutdown(&block)
  enqueue(:shutdown, block)

  return nil
end