Sha256: 6cd7f88d8f645010e72cc71833d022d1abfa83d2284df4db1f37ef22101b8d1a

Contents?: true

Size: 843 Bytes

Versions: 4

Compression:

Stored size: 843 Bytes

Contents

class Asynchronic::Worker

  attr_reader :queue, :queue_name, :environment, :listener

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

  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

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

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

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
asynchronic-4.0.3 lib/asynchronic/worker.rb
asynchronic-4.0.2 lib/asynchronic/worker.rb
asynchronic-4.0.1 lib/asynchronic/worker.rb
asynchronic-4.0.0 lib/asynchronic/worker.rb