lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.6 vs lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.7

- old
+ new

@@ -38,14 +38,14 @@ # # This is simply a helper module. The rufus-scheduler will check if scheduled # object quack (respond to :trigger anyway). # module Schedulable - def call (job) + def call(job) trigger(job.params) end - def trigger (params) + def trigger(params) raise NotImplementedError.new('implementation is missing') end end # @@ -59,11 +59,11 @@ # # Consider all methods here as 'deprecated'. # module LegacyMethods - def find_jobs (tag=nil) + def find_jobs(tag=nil) tag ? find_by_tag(tag) : all_jobs.values end def at_job_count @jobs.select(:at).size + @jobs.select(:in).size @@ -95,11 +95,11 @@ # attr_reader :options # Instantiates a Rufus::Scheduler. # - def initialize (opts={}) + def initialize(opts={}) @options = opts @jobs = get_queue(:at, opts) @cron_jobs = get_queue(:cron, opts) @@ -107,11 +107,11 @@ @frequency = @options[:frequency] || 0.330 end # Instantiates and starts a new Rufus::Scheduler. # - def self.start_new (opts={}) + def self.start_new(opts={}) s = self.new(opts) s.start s end @@ -126,11 +126,11 @@ # puts "order ristretto" # end # # will order an espresso (well sort of) in 20 minutes. # - def in (t, s=nil, opts={}, &block) + def in(t, s=nil, opts={}, &block) add_job(InJob.new(self, t, combine_opts(s, opts), &block)) end alias :schedule_in :in @@ -140,11 +140,11 @@ # puts 'order pizza' # end # # pizza is for Thursday at 2000 (if the shop brochure is right). # - def at (t, s=nil, opts={}, &block) + def at(t, s=nil, opts={}, &block) add_job(AtJob.new(self, t, combine_opts(s, opts), &block)) end alias :schedule_at :at @@ -154,11 +154,11 @@ # puts 'check blood pressure' # end # # checking blood pressure every 5 months and 1 week. # - def every (t, s=nil, opts={}, &block) + def every(t, s=nil, opts={}, &block) add_job(EveryJob.new(self, t, combine_opts(s, opts), &block)) end alias :schedule_every :every @@ -167,21 +167,21 @@ # scheduler.cron '0 22 * * 1-5' do # # every day of the week at 00:22 # puts 'activate security system' # end # - def cron (cronstring, s=nil, opts={}, &block) + def cron(cronstring, s=nil, opts={}, &block) add_cron_job(CronJob.new(self, cronstring, combine_opts(s, opts), &block)) end alias :schedule :cron # Unschedules a job (cron or at/every/in job) given its id. # # Returns the job that got unscheduled. # - def unschedule (job_id) + def unschedule(job_id) @jobs.unschedule(job_id) || @cron_jobs.unschedule(job_id) end #-- @@ -189,11 +189,11 @@ #++ # Feel free to override this method. The default implementation simply # outputs the error message to STDOUT # - def handle_exception (job, exception) + def handle_exception(job, exception) if self.respond_to?(:log_exception) # # some kind of backward compatibility @@ -234,22 +234,32 @@ jobs.merge(cron_jobs) end # Returns a list of jobs with the given tag # - def find_by_tag (tag) + def find_by_tag(tag) all_jobs.values.select { |j| j.tags.include?(tag) } end + # Returns the current list of trigger threads (threads) dedicated to + # the execution of jobs. + # + def trigger_threads + + Thread.list.select { |t| + t["rufus_scheduler__trigger_thread__#{self.object_id}"] == true + } + end + protected # Returns a job queue instance. # # (made it into a method for easy override) # - def get_queue (type, opts) + 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 @@ -258,11 +268,11 @@ q.scheduler = self if q.respond_to?(:scheduler=) q end - def combine_opts (schedulable, opts) + def combine_opts(schedulable, opts) if schedulable.respond_to?(:trigger) || schedulable.respond_to?(:call) opts[:schedulable] = schedulable @@ -281,33 +291,33 @@ @cron_jobs.trigger_matching_jobs @jobs.trigger_matching_jobs end - def add_job (job) + def add_job(job) complain_if_blocking_and_timeout(job) return if job.params[:discard_past] && Time.now.to_f >= job.at @jobs << job job end - def add_cron_job (job) + def add_cron_job(job) complain_if_blocking_and_timeout(job) @cron_jobs << job job end # Raises an error if the job has the params :blocking and :timeout set # - def complain_if_blocking_and_timeout (job) + def complain_if_blocking_and_timeout(job) raise( ArgumentError.new('cannot set a :timeout on a :blocking job') ) if job.params[:blocking] and job.params[:timeout] end @@ -317,11 +327,11 @@ # Else, it will call the block in a dedicated thread. # # TODO : clarify, the blocking here blocks the whole scheduler, while # EmScheduler blocking triggers for the next tick. Not the same thing ... # - def trigger_job (blocking, &block) + def trigger_job(blocking, &block) if blocking block.call else Thread.new { block.call } @@ -351,11 +361,11 @@ @thread[:name] = @options[:thread_name] || "#{self.class} - #{Rufus::Scheduler::VERSION}" end - def stop (opts={}) + def stop(opts={}) @thread.exit end def join @@ -374,11 +384,11 @@ # A rufus-scheduler that steps only when the ruby process receives the # 10 / USR1 signal. # class SignalScheduler < SchedulerCore - def initialize (opts={}) + def initialize(opts={}) super(opts) trap(@options[:signal] || 10) do step @@ -395,11 +405,11 @@ # A rufus-scheduler that uses an EventMachine periodic timer instead of a # loop. # class EmScheduler < SchedulerCore - def initialize (opts={}) + def initialize(opts={}) raise LoadError.new( 'EventMachine missing, "require \'eventmachine\'" might help' ) unless defined?(EM) @@ -429,11 +439,11 @@ # Stops the scheduler. # # If the :stop_em option is passed and set to true, it will stop the # EventMachine (but only if it started the EM by itself !). # - def stop (opts={}) + def stop(opts={}) @timer.cancel EM.stop if opts[:stop_em] and @em_thread end @@ -449,10 +459,10 @@ protected # If 'blocking' is set to true, the block will get called at the # 'next_tick'. Else the block will get called via 'defer' (own thread). # - def trigger_job (blocking, &block) + def trigger_job(blocking, &block) m = blocking ? :next_tick : :defer # # :next_tick monopolizes the EM # :defer executes its block in another thread