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
# 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 }
end

Instance Method Details

#enqueue(command, data) ⇒ Object



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

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

  return nil
end

#join(max_wait = nil) ⇒ Object



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

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



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

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

  return nil
end

#shutdown(&block) ⇒ Object



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

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

  return nil
end