Class: Rufus::Scheduler::JobQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/rufus/sc/jobqueues.rb

Overview

Tracking at/in/every jobs.

In order of trigger time.

Direct Known Subclasses

CronJobQueue

Constant Summary collapse

JOB_TYPES =

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

{
  :at => Rufus::Scheduler::AtJob,
  :in => Rufus::Scheduler::InJob,
  :every => Rufus::Scheduler::EveryJob
}

Instance Method Summary collapse

Constructor Details

#initializeJobQueue

Returns a new instance of JobQueue.



47
48
49
50
51
# File 'lib/rufus/sc/jobqueues.rb', line 47

def initialize

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

Instance Method Details

#<<(job) ⇒ Object

Adds this job to the map.



66
67
68
69
70
71
72
73
# 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) ⇒ Object

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



91
92
93
94
95
# File 'lib/rufus/sc/jobqueues.rb', line 91

def select (type)

  type = JOB_TYPES[type]
  @jobs.select { |j| j.is_a?(type) }
end

#sizeObject



97
98
99
100
# File 'lib/rufus/sc/jobqueues.rb', line 97

def size

  @jobs.size
end

#to_hObject

Returns a mapping job_id => job



84
85
86
87
# File 'lib/rufus/sc/jobqueues.rb', line 84

def to_h

  @jobs.inject({}) { |h, j| h[j.job_id] = j; h }
end

#trigger_matching_jobsObject

Triggers all the jobs that are scheduled for ‘now’.



55
56
57
58
59
60
61
62
# 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) ⇒ Object

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



77
78
79
80
# File 'lib/rufus/sc/jobqueues.rb', line 77

def unschedule (job_id)

  @mutex.synchronize { delete(job_id) }
end