class Rufus::Scheduler::JobQueue

Tracking at/in/every jobs.

In order of trigger time.

Constants

JOB_TYPES

Mapping :at|:in|:every to their respective job classes.

Public Class Methods

new() click to toggle source
# File lib/rufus/sc/jobqueues.rb, line 47
def initialize

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

Public Instance Methods

<<(job) click to toggle source

Adds this job to the map.

# File lib/rufus/sc/jobqueues.rb, line 66
def << (job)

  @mutex.synchronize do
    delete(job.job_id)
    @jobs << job
    @jobs.sort! { |j0, j1| j0.at <=> j1.at }
  end
end
select(type) click to toggle source

Returns a list of jobs of the given type (:at|:in|:every)

# File lib/rufus/sc/jobqueues.rb, line 91
def select (type)

  type = JOB_TYPES[type]
  @jobs.select { |j| j.is_a?(type) }
end
size() click to toggle source
# File lib/rufus/sc/jobqueues.rb, line 97
def size

  @jobs.size
end
to_h() click to toggle source

Returns a mapping job_id => job

# File lib/rufus/sc/jobqueues.rb, line 84
def to_h

  @jobs.inject({}) { |h, j| h[j.job_id] = j; h }
end
trigger_matching_jobs() click to toggle source

Triggers all the jobs that are scheduled for 'now'.

# File lib/rufus/sc/jobqueues.rb, line 55
def trigger_matching_jobs

  now = Time.now

  while job = job_to_trigger(now)
    job.trigger
  end
end
unschedule(job_id) click to toggle source

Removes a job (given its id). Returns nil if the job was not found.

# File lib/rufus/sc/jobqueues.rb, line 77
def unschedule (job_id)

  @mutex.synchronize { delete(job_id) }
end

Protected Instance Methods

delete(job_id) click to toggle source
# File lib/rufus/sc/jobqueues.rb, line 104
def delete (job_id)

  j = @jobs.find { |j| j.job_id == job_id }
  @jobs.delete(j) if j
end
job_to_trigger(now) click to toggle source

Returns the next job to trigger. Returns nil if none eligible.

# File lib/rufus/sc/jobqueues.rb, line 112
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