Class: Oxidized::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/oxidized/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ Worker

Returns a new instance of Worker.



5
6
7
8
9
# File 'lib/oxidized/worker.rb', line 5

def initialize nodes
  @nodes   = nodes
  @jobs    = Jobs.new CFG.threads, CFG.interval, @nodes
  Thread.abort_on_exception = true
end

Instance Method Details

#process(job) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/oxidized/worker.rb', line 30

def process job
  node = job.node
  node.last = job
  node.stats.add job
  @jobs.duration job.time
  node.running = false
  if job.status == :success
    Oxidized.Hooks.handle :node_success, :node => node,
                                         :job => job
    msg = "update #{node.name}"
    msg += " from #{node.from}" if node.from
    msg += " with message '#{node.msg}'" if node.msg
    if node.output.new.store node.name, job.config,
                          :msg => msg, :user => node.user, :group => node.group
      Log.info "Configuration updated for #{node.group}/#{node.name}"
      Oxidized.Hooks.handle :post_store, :node => node,
                                         :job => job
    end
    node.reset
  else
    msg = "#{node.name} status #{job.status}"
    if node.retry < CFG.retries
      node.retry += 1
      msg += ", retry attempt #{node.retry}"
      @nodes.next node.name
    else
      msg += ", retries exhausted, giving up"
      node.retry = 0
      Oxidized.Hooks.handle :node_fail, :node => node,
                                        :job => job
    end
    Log.warn msg
  end
rescue NodeNotFound
  Log.warn "#{node.name} not found, removed while collecting?"
end

#workObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oxidized/worker.rb', line 11

def work
  ended = []
  @jobs.delete_if { |job| ended << job if not job.alive? }
  ended.each      { |job| process job }
  @jobs.work
  while @jobs.size < @jobs.want
    Log.debug "Jobs #{@jobs.size}, Want: #{@jobs.want}"
    # ask for next node in queue non destructive way
    nextnode = @nodes.first
    unless nextnode.last.nil?
      break if nextnode.last.end + CFG.interval > Time.now.utc
    end
    # shift nodes and get the next node
    node = @nodes.get
    node.running? ? next : node.running = true
    @jobs.push Job.new node
  end
end