Class: MKIt::JobManager

Inherits:
Object
  • Object
show all
Defined in:
lib/mkit/job_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeJobManager

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

#startObject



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.message, e.backtrace.join("\n")
      end
    end
  end
end

#stopObject



47
48
49
# File 'lib/mkit/job_manager.rb', line 47

def stop
  @thread.exit if @thread
end