Sha256: 7ff7ecd1e4eb61084fead31f3c38d38d16a2acb2ab5fb2fb1450870cd81326bb

Contents?: true

Size: 1.74 KB

Versions: 6

Compression:

Stored size: 1.74 KB

Contents

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 }
      @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

    def process job
      node = job.node
      node.last = job
      node.stats.add job
      @jobs.duration job.time
      node.running = false
      if job.status == :success
        msg = "update #{node.name}"
        msg += " from #{node.from}" if node.from
        msg += " with message '#{node.msg}'" if node.msg
        node.output.new.store node.name, job.config,
                              :msg => msg, :user => node.user, :group => node.group
        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
        end
        Log.warn msg
      end
    rescue NodeNotFound
      Log.warn "#{node.name} not found, removed while collecting?"
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
oxidized-0.7.1 lib/oxidized/worker.rb
oxidized-0.7.0 lib/oxidized/worker.rb
oxidized-0.6.0 lib/oxidized/worker.rb
oxidized-0.5.0 lib/oxidized/worker.rb
oxidized-0.4.1 lib/oxidized/worker.rb
oxidized-0.4.0 lib/oxidized/worker.rb