Class: Workers::Worker

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

Instance Attribute Summary collapse

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.


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

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

  nil
end

Instance Attribute Details

#exceptionObject

Returns the value of attribute exception


5
6
7
# File 'lib/workers/worker.rb', line 5

def exception
  @exception
end

#on_exceptionObject

Returns the value of attribute on_exception


6
7
8
# File 'lib/workers/worker.rb', line 6

def on_exception
  @on_exception
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)

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

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

#dispose(max_wait = nil) ⇒ Object


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

def dispose(max_wait = nil)
  shutdown
  join(max_wait)
end

#enqueue(command, data = nil) ⇒ Object


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

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

  nil
end

#inspectObject


59
60
61
# File 'lib/workers/worker.rb', line 59

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

#join(max_wait = nil) ⇒ Object

Raises:


42
43
44
45
46
47
48
# File 'lib/workers/worker.rb', line 42

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

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

  @thread.kill and return false
end

#killObject


36
37
38
39
40
# File 'lib/workers/worker.rb', line 36

def kill
  @thread.kill

  nil
end

#perform(&block) ⇒ Object


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

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

  nil
end

#shutdown(&block) ⇒ Object


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

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

  nil
end