summaryrefslogtreecommitdiff
path: root/lib/oxidized/worker.rb
blob: adbaa0ea8e24d86becf337c92a1927a7bd4787d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Oxidized
  require 'oxidized/job'
  require 'oxidized/jobs'
  class Worker
    def initialize nodes
      @nodes   = nodes
      @jobs    = Jobs.new CFG.threads, CFG.interval, @nodes
      Thread.abort_on_exception = true
    end
    def work
      ended = []
      @jobs.delete_if { |job| ended << job if not job.alive? }
      ended.each      { |job| process job }
      while @jobs.size < @jobs.want
        Log.debug "Jobs #{@jobs.size}, Want: #{@jobs.want}"
        node = @nodes.get
        node.running? ? next : node.running = true
        @jobs.push Job.new node
      end
    end
    def process job
      node = job.node
      node.last = job
      @jobs.duration job.time
      if job.status == :success
        node.output.new.update node.name, job.config, 
                               :msg => "update #{node.name}", :group => node.group
      else
        Log.warn "#{node.name} status #{job.status}"
      end
      node.running = false
    end
  end
end