From cc088956bc2dbab95fd77574886c84c3ea5f0215 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 30 May 2016 12:28:05 +1000 Subject: [PATCH] correct some test concurrency bugs --- lib/scheduler/manager.rb | 36 +++++++++++++++-------- spec/components/scheduler/manager_spec.rb | 10 ++++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/scheduler/manager.rb b/lib/scheduler/manager.rb index 0607804258f..0e264d2492c 100644 --- a/lib/scheduler/manager.rb +++ b/lib/scheduler/manager.rb @@ -8,7 +8,7 @@ require_dependency 'distributed_mutex' module Scheduler class Manager - attr_accessor :random_ratio, :redis + attr_accessor :random_ratio, :redis, :enable_stats class Runner def initialize(manager) @@ -69,13 +69,15 @@ module Scheduler begin info.prev_result = "RUNNING" @mutex.synchronize { info.write! } - stat = SchedulerStat.create!( - name: klass.to_s, - hostname: hostname, - pid: Process.pid, - started_at: Time.zone.now, - live_slots_start: GC.stat[:heap_live_slots] - ) + if @manager.enable_stats + stat = SchedulerStat.create!( + name: klass.to_s, + hostname: hostname, + pid: Process.pid, + started_at: Time.zone.now, + live_slots_start: GC.stat[:heap_live_slots] + ) + end klass.new.perform rescue Jobs::HandledExceptionWrapper # Discourse.handle_exception was already called, and we don't have any extra info to give @@ -88,11 +90,13 @@ module Scheduler info.prev_duration = duration info.prev_result = failed ? "FAILED" : "OK" info.current_owner = nil - stat.update_columns( - duration_ms: duration, - live_slots_finish: GC.stat[:heap_live_slots], - success: !failed - ) + if stat + stat.update_columns( + duration_ms: duration, + live_slots_finish: GC.stat[:heap_live_slots], + success: !failed + ) + end attempts(3) do @mutex.synchronize { info.write! } end @@ -151,6 +155,12 @@ module Scheduler @hostname = options && options[:hostname] @manager_id = SecureRandom.hex + + if options && options.key?(:enable_stats) + @enable_stats = options[:enable_stats] + else + @enable_stats = true + end end def self.current diff --git a/spec/components/scheduler/manager_spec.rb b/spec/components/scheduler/manager_spec.rb index 2f82deed73c..8b1bff1a878 100644 --- a/spec/components/scheduler/manager_spec.rb +++ b/spec/components/scheduler/manager_spec.rb @@ -54,7 +54,9 @@ describe Scheduler::Manager do end end - let(:manager) { Scheduler::Manager.new(DiscourseRedis.new) } + let(:manager) { + Scheduler::Manager.new(DiscourseRedis.new, enable_stats: false) + } before do $redis.del manager.class.lock_key @@ -79,7 +81,7 @@ describe Scheduler::Manager do hosts.map do |host| - manager = Scheduler::Manager.new(DiscourseRedis.new, hostname: host) + manager = Scheduler::Manager.new(DiscourseRedis.new, hostname: host, enable_stats: false) manager.ensure_schedule!(Testing::PerHostJob) info = manager.schedule_info(Testing::PerHostJob) @@ -126,7 +128,7 @@ describe Scheduler::Manager do $redis.del manager.identity_key - manager = Scheduler::Manager.new(DiscourseRedis.new) + manager = Scheduler::Manager.new(DiscourseRedis.new, enable_stats: false) manager.reschedule_orphans! info = manager.schedule_info(Testing::SuperLongJob) @@ -174,7 +176,7 @@ describe Scheduler::Manager do (0..5).map do Thread.new do - manager = Scheduler::Manager.new(DiscourseRedis.new) + manager = Scheduler::Manager.new(DiscourseRedis.new, enable_stats: false) manager.blocking_tick manager.stop! end