lib/zhong/scheduler.rb in zhong-0.1.7 vs lib/zhong/scheduler.rb in zhong-0.1.8
- old
+ new
@@ -1,8 +1,8 @@
module Zhong
class Scheduler
- attr_reader :config, :redis, :jobs
+ attr_reader :config, :redis, :jobs, :logger
DEFAULT_CONFIG = {
timeout: 0.5,
grace: 15.minutes,
long_running_timeout: 5.minutes,
@@ -59,11 +59,11 @@
raise "unknown callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
(@callbacks[event.to_sym] ||= []) << block
end
def start
- @logger.info "starting at #{redis_time}"
+ logger.info "starting at #{redis_time}"
@stop = false
trap_signals
@@ -85,23 +85,26 @@
fire_callbacks(:after_tick)
heartbeat(now)
break if @stop
- sleep_until_next_second
+ else
+ logger.info "skipping tick due to a `:before_tick` callback"
end
+ sleep_until_next_second
+
break if @stop
end
@running = false
- Thread.new { @logger.info "stopped" }.join
+ Thread.new { logger.info "stopped" }.join
end
def stop
- Thread.new { @logger.error "stopping" } if @running # thread necessary due to trap context
+ Thread.new { logger.error "stopping" } if @running # thread necessary due to trap context
@stop = true
end
def find_by_name(job_name)
@jobs[Digest::SHA256.hexdigest(job_name)]
@@ -117,18 +120,23 @@
TRAPPED_SIGNALS = %w(QUIT INT TERM).freeze
private_constant :TRAPPED_SIGNALS
def fire_callbacks(event, *args)
- @callbacks[event].to_a.all? { |h| h.call(*args) }
+ @callbacks[event].to_a.map do |callback|
+ callback.call(*args)
+ end.compact.all? # do not skip on nils
end
def jobs_to_run(time = redis_time)
jobs.select { |_, job| job.run?(time) }
end
def run_job(job, time = redis_time)
- return unless fire_callbacks(:before_run, job, time)
+ unless fire_callbacks(:before_run, job, time)
+ logger.info "skipping #{job} due to a `:before_run` callback"
+ return
+ end
ran = job.run(time, error_handler)
fire_callbacks(:after_run, job, time, ran)
end