lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.13 vs lib/rufus/sc/scheduler.rb in rufus-scheduler-2.0.14
- old
+ new
@@ -175,16 +175,18 @@
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.
+ # Unschedules a job (cron or at/every/in job).
#
# Returns the job that got unscheduled.
#
- def unschedule(job_id)
+ def unschedule(job_or_id)
+ job_id = job_or_id.respond_to?(:job_id) ? job_or_id.job_id : job_or_id
+
@jobs.unschedule(job_id) || @cron_jobs.unschedule(job_id)
end
# Given a tag, unschedules all the jobs that bear that tag.
#
@@ -194,10 +196,26 @@
jobs.each { |job| unschedule(job.job_id) }
jobs
end
+ # Pauses a given job. If the argument is an id (String) and the
+ # corresponding job cannot be found, an ArgumentError will get raised.
+ #
+ def pause(job_or_id)
+
+ find(job_or_id).pause
+ end
+
+ # Resumes a given job. If the argument is an id (String) and the
+ # corresponding job cannot be found, an ArgumentError will get raised.
+ #
+ def resume(job_or_id)
+
+ find(job_or_id).resume
+ end
+
#--
# MISC
#++
# Determines if there is #log_exception, #handle_exception or #on_exception
@@ -268,10 +286,29 @@
def find_by_tag(tag)
all_jobs.values.select { |j| j.tags.include?(tag) }
end
+ # Mostly used to find a job given its id. If the argument is a job, will
+ # simply return it.
+ #
+ # If the argument is an id, and no job with that id is found, it will
+ # raise an ArgumentError.
+ #
+ def find(job_or_id)
+
+ return job_or_id if job_or_id.respond_to?(:job_id)
+
+ job = all_jobs[job_or_id]
+
+ raise ArgumentError.new(
+ "couldn't find job #{job_or_id.inspect}"
+ ) unless job
+
+ job
+ end
+
# Returns the current list of trigger threads (threads) dedicated to
# the execution of jobs.
#
def trigger_threads
@@ -384,10 +421,10 @@
def start
@thread = Thread.new do
loop do
sleep(@frequency)
- self.step
+ step
end
end
@thread[:name] =
@options[:thread_name] ||