lib/say_when/scheduler.rb in say_when-0.3.0 vs lib/say_when/scheduler.rb in say_when-0.4.0

- old
+ new

@@ -6,11 +6,11 @@ DEFAULT_STORAGE_STRATEGY = :memory @@scheduler = nil @@lock = nil - attr_accessor :storage_strategy, :processor_class, :tick_length, :reset_acquired_length + attr_accessor :storage_strategy, :processor_class, :tick_length, :reset_acquired_length, :reset_next_at attr_accessor :running # support for a singleton scheduler, but you are not restricted to this class << self @@ -61,70 +61,71 @@ [$stdout, $stderr].each{|s| s.sync = true; s.flush} trap("TERM", "EXIT") begin - self.running = true logger.info "SayWhen::Scheduler running" - job = nil - reset_next_at = Time.now while running - begin - time_now = Time.now + process_jobs + end + rescue Exception => ex + logger.error "SayWhen::Scheduler stopping, error: #{ex.class.name}: #{ex.message}" + exit + end - if reset_acquired_length > 0 && reset_next_at <= time_now - reset_next_at = time_now + reset_acquired_length - logger.debug "SayWhen:: reset acquired at #{time_now}, try again at #{reset_next_at}" - job_class.reset_acquired(reset_acquired_length) - end + logger.info "SayWhen::Scheduler stopped" + end - begin - logger.debug "SayWhen:: Looking for job that should be ready to fire before #{time_now}" - job = job_class.acquire_next(time_now) - rescue StandardError => ex - job_error("Failure to acquire job", job, ex) - job = nil - end + def process_jobs + job = nil + time_now = Time.now + self.reset_next_at ||= Time.now - if job.nil? - logger.debug "SayWhen:: no jobs to acquire, sleep" - sleep(tick_length) - next - end + if reset_acquired_length > 0 && reset_next_at <= time_now + self.reset_next_at = time_now + reset_acquired_length + logger.debug "SayWhen:: reset acquired at #{time_now}, try again at #{reset_next_at}" + job_class.reset_acquired(reset_acquired_length) + end - begin - logger.debug "SayWhen:: got a job: #{job.inspect}" - # delegate processing the trigger to the processor - self.processor.process(job) - logger.debug "SayWhen:: job processed" + begin + logger.debug "SayWhen:: Looking for job that should be ready to fire before #{time_now}" + job = job_class.acquire_next(time_now) + rescue StandardError => ex + job_error("Failure to acquire job", job, ex) + job = nil + end - # if successful, update next fire at, put back to waiting / ended - job.fired(time_now) - logger.debug "SayWhen:: job fired complete" - rescue StandardError=>ex - job_error("Failure to process", job, ex) - end + if job.nil? + logger.debug "SayWhen:: no jobs to acquire, sleep" + sleep(tick_length) + return + end - rescue Interrupt => ex - job_error("Interrupt!", job, ex) - raise ex - rescue StandardError => ex - job_error("Error!", job, ex) - sleep(tick_length) - rescue Exception => ex - job_error("Exception!", job, ex) - raise ex - end - end - rescue Exception=>ex - logger.error "SayWhen::Scheduler stopping, error: #{ex.class.name}: #{ex.message}" - exit + begin + logger.debug "SayWhen:: got a job: #{job.inspect}" + # delegate processing the trigger to the processor + self.processor.process(job) + logger.debug "SayWhen:: job processed" + + # if successful, update next fire at, put back to waiting / ended + job.fired(time_now) + logger.debug "SayWhen:: job fired complete" + rescue StandardError => ex + job_error("Failure to process", job, ex) end - logger.info "SayWhen::Scheduler stopped" + rescue Interrupt => ex + job_error("Interrupt!", job, ex) + raise ex + rescue StandardError => ex + job_error("Error!", job, ex) + sleep(tick_length) + rescue Exception => ex + job_error("Exception!", job, ex) + raise ex end def job_error(msg, job, ex) job_msg = job && " job:'#{job.inspect}'" logger.error "SayWhen::Scheduler #{msg}#{job_msg}: #{ex.message}\n\t#{ex.backtrace.join("\t\n")}" @@ -152,8 +153,7 @@ end def logger SayWhen::logger end - end end