lib/rufus/scheduler/job_array.rb in rufus-scheduler-3.6.0 vs lib/rufus/scheduler/job_array.rb in rufus-scheduler-3.7.0

- old
+ new

@@ -1,72 +1,62 @@ -module Rufus +# +# The array rufus-scheduler uses to keep jobs in order (next to trigger +# first). +# +class Rufus::Scheduler::JobArray - class Scheduler + def initialize - # - # The array rufus-scheduler uses to keep jobs in order (next to trigger - # first). - # - class JobArray + @mutex = Mutex.new + @array = [] + end - def initialize + def push(job) - @mutex = Mutex.new - @array = [] - end + @mutex.synchronize { @array << job unless @array.index(job) } - def push(job) + self + end - @mutex.synchronize { @array << job unless @array.index(job) } + def size - self - end + @array.size + end - def size + def each(now, &block) - @array.size - end + to_a.sort_by do |job| - def each(now, &block) + job.next_time || (now + 1) - to_a.sort_by do |job| + end.each do |job| - job.next_time || (now + 1) + nt = job.next_time + break if ( ! nt) || (nt > now) - end.each do |job| + block.call(job) + end + end - nt = job.next_time - break if ( ! nt) || (nt > now) + def delete_unscheduled - block.call(job) - end - end + @mutex.synchronize { + @array.delete_if { |j| j.next_time.nil? || j.unscheduled_at } } + end - def delete_unscheduled + def to_a - @mutex.synchronize { + @mutex.synchronize { @array.dup } + end - @array.delete_if { |j| j.next_time.nil? || j.unscheduled_at } - } - end + def [](job_id) - def to_a + @mutex.synchronize { @array.find { |j| j.job_id == job_id } } + end - @mutex.synchronize { @array.dup } - end + def unschedule_all - def [](job_id) - - @mutex.synchronize { @array.find { |j| j.job_id == job_id } } - end - - # Only used when shutting down, directly yields the underlying array. - # - def array - - @array - end - end + @array.each(&:unschedule) end end