Class: MKIt::JobManager
- Inherits:
-
Object
- Object
- MKIt::JobManager
- Defined in:
- lib/mkit/job_manager.rb
Instance Method Summary collapse
-
#initialize ⇒ JobManager
constructor
A new instance of JobManager.
- #register_worker(worker, topics) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ JobManager
Returns a new instance of JobManager.
6 7 8 |
# File 'lib/mkit/job_manager.rb', line 6 def initialize @workers = {} end |
Instance Method Details
#register_worker(worker, topics) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/mkit/job_manager.rb', line 10 def register_worker(worker, topics) topics.each { | topic | @workers[topic] ||= [] MKItLogger.info("register #{worker.class} for topic #{topic}") @workers[topic] << worker } end |
#start ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mkit/job_manager.rb', line 18 def start MKItLogger.info('starting job manager') @thread = Thread.new do loop do job = MkitJob.take begin if job.nil? sleep(10) else topic = job.topic job.processing! if @workers[topic].nil? MKItLogger.warn("no workers found for topic '#{topic}'") else workers = @workers[topic] workers.each { | worker | worker.do_the(job) } end end job.done! unless job.nil? rescue Exception => e job.error! unless job.nil? MKItLogger.error e, e., e.backtrace.join("\n") end end end end |
#stop ⇒ Object
47 48 49 |
# File 'lib/mkit/job_manager.rb', line 47 def stop @thread.exit if @thread end |