class Rufus::Scheduler::JobQueue


In order of trigger time.
Tracking at/in/every jobs.

def <<(job)


Adds this job to the map.
def <<(job)
  @mutex.synchronize do
    delete(job.job_id)
    @jobs << job
    @jobs.sort! { |j0, j1| j0.at <=> j1.at }
  end
end

def delete(job_id)

def delete(job_id)
  if job = @jobs.find { |j| j.job_id == job_id }
    @jobs.delete(job)
  end
end

def initialize

def initialize
  @mutex = Mutex.new
  @jobs = []
end

def job_to_trigger(now)


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

def select(type)


Returns a list of jobs of the given type (:at|:in|:every)
def select(type)
  type = JOB_TYPES[type]
  @jobs.select { |j| j.is_a?(type) }
end

def size

def size
  @jobs.size
end

def to_h


Returns a mapping job_id => job
def to_h
  @jobs.inject({}) { |h, j| h[j.job_id] = j; h }
end

def trigger_matching_jobs


Triggers all the jobs that are scheduled for 'now'.
def trigger_matching_jobs
  now = Time.now
  while job = job_to_trigger(now)
    job.trigger
  end
end

def unschedule(job_id)


Removes a job (given its id). Returns nil if the job was not found.
def unschedule(job_id)
  @mutex.synchronize { delete(job_id) }
end