lib/asynchronic/worker.rb in asynchronic-0.0.1 vs lib/asynchronic/worker.rb in asynchronic-0.1.0
- old
+ new
@@ -1,27 +1,36 @@
-module Asynchronic
- class Worker
+class Asynchronic::Worker
- attr_reader :queue
+ attr_reader :queue
+ attr_reader :queue_name
+ attr_reader :env
+ attr_reader :listener
- def initialize(queue=nil)
- @queue = queue || Asynchronic.default_queue
- end
+ def initialize(queue_name, env)
+ @queue_name = queue_name
+ @queue = env.queue_engine[queue_name]
+ @env = env
+ @listener = env.queue_engine.listener
+ end
- def start
- Signal.trap('INT') { stop }
+ def start
+ Asynchronic.logger.info('Asynchronic') { "Starting worker of #{queue_name} (#{Process.pid})" }
- Ost[@queue].pop do |pid|
- Process.find(pid).run
- end
+ Signal.trap('INT') { stop }
+
+ listener.listen(queue) do |pid|
+ env.load_process(pid).execute
end
+ end
- def stop
- Ost[@queue].stop
- end
+ def stop
+ Asynchronic.logger.info('Asynchronic') { "Stopping worker of #{@queue_name} (#{Process.pid})" }
+ listener.stop
+ end
- def self.start(queue=nil)
- new(queue).tap(&:start)
- 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
\ No newline at end of file