Sha256: 0a7b1471447c5158c2117d6b1e6d5a01fd7398dc1dc47f374420f9873c2fcf09

Contents?: true

Size: 868 Bytes

Versions: 7

Compression:

Stored size: 868 Bytes

Contents

#
# The array rufus-scheduler uses to keep jobs in order (next to trigger
# first).
#
class Rufus::Scheduler::JobArray

  def initialize

    @mutex = Mutex.new
    @array = []
  end

  def push(job)

    @mutex.synchronize { @array << job unless @array.index(job) }

    self
  end

  def size

    @array.size
  end

  def each(now, &block)

    to_a.sort_by do |job|

      job.next_time || (now + 1)

    end.each do |job|

      nt = job.next_time
      break if ( ! nt) || (nt > now)

      block.call(job)
    end
  end

  def delete_unscheduled

    @mutex.synchronize {
      @array.delete_if { |j| j.next_time.nil? || j.unscheduled_at } }
  end

  def to_a

    @mutex.synchronize { @array.dup }
  end

  def [](job_id)

    @mutex.synchronize { @array.find { |j| j.job_id == job_id } }
  end

  def unschedule_all

    @array.each(&:unschedule)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rufus-scheduler-3.9.2 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.9.1 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.9.0 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.8.2 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.8.1 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.8.0 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.7.0 lib/rufus/scheduler/job_array.rb