lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.19 vs lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.20

- old
+ new

@@ -325,10 +325,25 @@ Thread.list.collect { |t| t["rufus_scheduler__trigger_thread__#{self.object_id}"] }.compact end + # This is a blocking call, it will return when all the jobs have been + # unscheduled, waiting for any running one to finish before unscheduling + # it. + # + def terminate_all_jobs + + all_jobs.each do |job_id, job| + job.unschedule + end + + while running_jobs.size > 0 + sleep 0.01 + end + end + protected # Returns a job queue instance. # # (made it into a method for easy override) @@ -453,13 +468,27 @@ @thread[:name] = @options[:thread_name] || "#{self.class} - #{Rufus::Scheduler::VERSION}" end + # Stops this scheduler. + # + # == :terminate => true + # + # If the option :terminate is set to true, + # the method will return once all the jobs have been unscheduled and + # are done with their current run if any. + # + # (note that if a job is + # currently running, this method will wait for it to terminate, it + # will not interrupt the job run). + # def stop(opts={}) @thread.exit + + terminate_all_jobs if opts[:terminate] end def join @thread.join @@ -485,13 +514,28 @@ trap(@options[:signal] || 10) do step end end - def stop + # Stops this scheduler. + # + # == :terminate => true + # + # If the option :terminate is set to true, + # the method will return once all the jobs have been unscheduled and + # are done with their current run if any. + # + # (note that if a job is + # currently running, this method will wait for it to terminate, it + # will not interrupt the job run). + # + def stop(opts={}) + trap(@options[:signal] || 10) + + terminate_all_jobs if opts[:terminate] end end # # A rufus-scheduler that uses an EventMachine periodic timer instead of a @@ -528,15 +572,29 @@ @timer = EM::PeriodicTimer.new(@frequency) { step } end # Stops the scheduler. # + # == :stop_em => true + # # 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 !). # + # == :terminate => true + # + # If the option :terminate is set to true, + # the method will return once all the jobs have been unscheduled and + # are done with their current run if any. + # + # (note that if a job is + # currently running, this method will wait for it to terminate, it + # will not interrupt the job run). + # def stop(opts={}) @timer.cancel + + terminate_all_jobs if opts[:terminate] EM.stop if opts[:stop_em] and @em_thread end # Joins this scheduler. Will actually join it only if it started the