lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.1 vs lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.2
- old
+ new
@@ -31,11 +31,11 @@
module Rufus::Scheduler
# This gem's version
#
- VERSION = '2.0.1'
+ VERSION = '2.0.2'
#
# It's OK to pass an object responding to :trigger when scheduling a job
# (instead of passing a block).
#
@@ -102,12 +102,12 @@
#
def initialize (opts={})
@options = opts
- @jobs = JobQueue.new
- @cron_jobs = CronJobQueue.new
+ @jobs = get_queue(:at, opts)
+ @cron_jobs = get_queue(:cron, opts)
@frequency = @options[:frequency] || 0.330
end
# Instantiates and starts a new Rufus::Scheduler.
@@ -205,10 +205,11 @@
else
puts '=' * 80
puts "scheduler caught exception :"
puts exception
+ exception.backtrace.each { |l| puts l }
puts '=' * 80
end
end
#--
@@ -243,10 +244,27 @@
all_jobs.values.select { |j| j.tags.include?(tag) }
end
protected
+ # Returns a job queue instance.
+ #
+ # (made it into a method for easy override)
+ #
+ def get_queue (type, opts)
+
+ q = if type == :cron
+ opts[:cron_job_queue] || Rufus::Scheduler::CronJobQueue.new
+ else
+ opts[:job_queue] || Rufus::Scheduler::JobQueue.new
+ end
+
+ q.scheduler = self if q.respond_to?(:scheduler=)
+
+ q
+ end
+
def combine_opts (schedulable, opts)
if schedulable.respond_to?(:trigger)
opts[:schedulable] = schedulable
@@ -261,34 +279,15 @@
# The method that does the "wake up and trigger any job that should get
# triggered.
#
def step
- cron_step
- at_step
- end
- # calls every second
- #
- def cron_step
-
- now = Time.now
- return if now.sec == @last_cron_second
- @last_cron_second = now.sec
- #
- # ensuring the crons are checked within 1 second (not 1.2 second)
-
- @cron_jobs.trigger_matching_jobs(now)
+ @cron_jobs.trigger_matching_jobs
+ @jobs.trigger_matching_jobs
end
- def at_step
-
- while job = @jobs.job_to_trigger
- job.trigger
- end
- end
-
def add_job (job)
complain_if_blocking_and_timeout(job)
return if job.params[:discard_past] && Time.now.to_f >= job.at
@@ -373,10 +372,31 @@
#class BlockingScheduler < PlainScheduler
# # use a Queue and a worker thread for the 'blocking' jobs
#end
#
+ # A rufus-scheduler that steps only when the ruby process receives the
+ # 10 / USR1 signal.
+ #
+ class SignalScheduler < SchedulerCore
+
+ def initialize (opts={})
+
+ super(opts)
+
+ trap(@options[:signal] || 10) do
+ step
+ end
+ end
+
+ def stop
+
+ trap(@options[:signal] || 10)
+ end
+ end
+
+ #
# A rufus-scheduler that uses an EventMachine periodic timer instead of a
# loop.
#
class EmScheduler < SchedulerCore
@@ -384,10 +404,10 @@
raise LoadError.new(
'EventMachine missing, "require \'eventmachine\'" might help'
) unless defined?(EM)
- super
+ super(opts)
end
def start
@em_thread = nil