Class: Asynchronic::Worker
- Inherits:
-
Object
- Object
- Asynchronic::Worker
- Defined in:
- lib/asynchronic/worker.rb
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#listener ⇒ Object
readonly
Returns the value of attribute listener.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#queue_name ⇒ Object
readonly
Returns the value of attribute queue_name.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(queue_name, environment) ⇒ Worker
constructor
A new instance of Worker.
- #start ⇒ Object
- #stop ⇒ Object
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
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
3 4 5 |
# File 'lib/asynchronic/worker.rb', line 3 def environment @environment end |
#listener ⇒ Object (readonly)
Returns the value of attribute listener.
3 4 5 |
# File 'lib/asynchronic/worker.rb', line 3 def listener @listener end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
3 4 5 |
# File 'lib/asynchronic/worker.rb', line 3 def queue @queue end |
#queue_name ⇒ Object (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
#start ⇒ Object
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 |
#stop ⇒ Object
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 |