Class: Asynchronic::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/asynchronic/worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_name, environment) ⇒ Worker

Returns a new instance of Worker.



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

def initialize(queue_name, environment)
  @queue_name = queue_name
  @queue = environment.queue_engine[queue_name]
  @environment = environment
  @listener = environment.queue_engine.listener
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



3
4
5
# File 'lib/asynchronic/worker.rb', line 3

def environment
  @environment
end

#listenerObject (readonly)

Returns the value of attribute listener.



3
4
5
# File 'lib/asynchronic/worker.rb', line 3

def listener
  @listener
end

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/asynchronic/worker.rb', line 3

def queue
  @queue
end

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



3
4
5
# File 'lib/asynchronic/worker.rb', line 3

def queue_name
  @queue_name
end

Class Method Details

.start(queue_name, &block) ⇒ Object



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

def self.start(queue_name, &block)
  worker = new queue_name, Asynchronic.environment
  Thread.new { block.call(worker) } if block_given?
  worker.start
end

Instance Method Details

#startObject



12
13
14
15
16
17
18
19
20
# File 'lib/asynchronic/worker.rb', line 12

def start
  Asynchronic.logger.info('Asynchronic') { "Starting worker of #{queue_name} (#{Process.pid})" }

  Signal.trap('QUIT') { stop }

  listener.listen(queue) do |pid|
    environment.load_process(pid).execute
  end
end

#stopObject



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

def stop
  Asynchronic.logger.info('Asynchronic') { "Stopping worker of #{queue_name} (#{Process.pid})" }
  listener.stop
end