Sha256: 378510477eb2fc34a58896d9128f98d283326fa27872502c44e7501769500fc5

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

module Rufus

  class Scheduler

    #
    # The array rufus-scheduler uses to keep jobs in order (next to trigger
    # first).
    #
    class 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

      # Only used when shutting down, directly yields the underlying array.
      #
      def array

        @array
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rufus-scheduler-3.6.0 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.5.2 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.5.1 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.5.0 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.4.2 lib/rufus/scheduler/job_array.rb
rufus-scheduler-3.4.0 lib/rufus/scheduler/job_array.rb