From 1b9cce245de59ae1d2cff0979d640e679b720590 Mon Sep 17 00:00:00 2001 From: Danilo Sousa Date: Mon, 14 Aug 2017 18:50:48 -0300 Subject: create `worker_done` event trigger a event when a full cycle was completed --- lib/oxidized/worker.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lib/oxidized/worker.rb') diff --git a/lib/oxidized/worker.rb b/lib/oxidized/worker.rb index 80d65cb..dfe9803 100644 --- a/lib/oxidized/worker.rb +++ b/lib/oxidized/worker.rb @@ -3,6 +3,7 @@ module Oxidized require 'oxidized/jobs' class Worker def initialize nodes + @jobs_done = 0 @nodes = nodes @jobs = Jobs.new(Oxidized.config.threads, Oxidized.config.interval, @nodes) @nodes.jobs = @jobs @@ -14,8 +15,9 @@ module Oxidized @jobs.delete_if { |job| ended << job if not job.alive? } ended.each { |job| process job } @jobs.work + while @jobs.size < @jobs.want - Oxidized.logger.debug "lib/oxidized/worker.rb: Jobs #{@jobs.size}, Want: #{@jobs.want}" + Oxidized.logger.debug "lib/oxidized/worker.rb: Jobs running: #{@jobs.size} of #{@jobs.want} - ended: #{@jobs_done} of #{@nodes.size}" # ask for next node in queue non destructive way nextnode = @nodes.first unless nextnode.last.nil? @@ -29,6 +31,8 @@ module Oxidized @jobs.push Job.new node Oxidized.logger.debug "lib/oxidized/worker.rb: Added #{node.name} to the job queue" end + + run_done_hook if is_cycle_finished? Oxidized.logger.debug("lib/oxidized/worker.rb: #{@jobs.size} jobs running in parallel") unless @jobs.empty? end @@ -38,6 +42,8 @@ module Oxidized node.stats.add job @jobs.duration job.time node.running = false + @jobs_done += 1 # needed for worker_done event + if job.status == :success Oxidized.Hooks.handle :node_success, :node => node, :job => job @@ -71,5 +77,20 @@ module Oxidized Oxidized.logger.warn "#{node.name} not found, removed while collecting?" end + private + + def is_cycle_finished? + @jobs_done > 0 && @jobs_done % @nodes.count == 0 + end + + def run_done_hook + Oxidized.logger.debug "lib/oxidized/worker.rb: Running :nodes_done hook" + Oxidized.Hooks.handle :nodes_done + rescue => e + # swallow the hook erros and continue as normal + Oxidized.logger.error "lib/oxidized/worker.rb: #{e.message}" + ensure + @jobs_done = 0 + end end end -- cgit v1.2.1