lib/rufus/sc/jobqueues.rb in rufus-scheduler-2.0.1 vs lib/rufus/sc/jobqueues.rb in rufus-scheduler-2.0.2
- old
+ new
@@ -48,20 +48,18 @@
@mutex = Mutex.new
@jobs = []
end
- # Returns the next job to trigger. Returns nil if none eligible.
+ # Triggers all the jobs that are scheduled for 'now'.
#
- def job_to_trigger
+ def trigger_matching_jobs
- @mutex.synchronize do
- if @jobs.size > 0 && Time.now.to_f >= @jobs.first.at
- @jobs.shift
- else
- nil
- end
+ now = Time.now
+
+ while job = job_to_trigger(now)
+ job.trigger
end
end
# Adds this job to the map.
#
@@ -102,54 +100,59 @@
end
protected
def delete (job_id)
+
j = @jobs.find { |j| j.job_id == job_id }
@jobs.delete(j) if j
- j
end
+
+ # Returns the next job to trigger. Returns nil if none eligible.
+ #
+ def job_to_trigger (now)
+
+ @mutex.synchronize do
+ if @jobs.size > 0 && now.to_f >= @jobs.first.at
+ @jobs.shift
+ else
+ nil
+ end
+ end
+ end
end
#
# Tracking cron jobs.
#
- # (mostly synchronizing access to the map of cron jobs)
- #
- class CronJobQueue
+ class CronJobQueue < JobQueue
def initialize
- @mutex = Mutex.new
- @jobs = {}
+ super
+ @last_cron_second = nil
end
- def unschedule (job_id)
+ def trigger_matching_jobs
- @mutex.synchronize { @jobs.delete(job_id) }
- end
+ now = Time.now
- def trigger_matching_jobs (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)
- js = @mutex.synchronize { @jobs.values }
- # maybe this sync is a bit paranoid
+ jobs = @mutex.synchronize { @jobs.dup }
- js.each { |job| job.trigger_if_matches(now) }
+ jobs.each { |job| job.trigger_if_matches(now) }
end
def << (job)
- @mutex.synchronize { @jobs[job.job_id] = job }
- end
-
- def size
-
- @jobs.size
- end
-
- def to_h
-
- @jobs.dup
+ @mutex.synchronize do
+ delete(job.job_id)
+ @jobs << job
+ end
end
end
end
end