Class: Unicron::JobQueue

Inherits:
Monitor
  • Object
show all
Defined in:
lib/unicron/job_queue.rb

Overview

JobQueue manages a set of Job instances for a Schedule. Users of Unicron should not have to deal directly with JobQueue instances.

Instance Method Summary collapse

Constructor Details

#initializeJobQueue

Create an empty JobQueue.



34
35
36
37
38
# File 'lib/unicron/job_queue.rb', line 34

def initialize
  super
  @jobs = []
  @jobs_changed = new_cond
end

Instance Method Details

#<<(job) ⇒ Object

Add a Job to the JobQueue.



10
11
12
13
14
15
16
17
18
19
# File 'lib/unicron/job_queue.rb', line 10

def << job
  synchronize do
    unless @jobs.include? job
      @jobs << job
      job.send :queue=, self
      jobs_changed
    end
    self
  end
end

#delete(job) ⇒ Object

Delete a Job from the JobQueue.



22
23
24
25
26
27
28
29
30
31
# File 'lib/unicron/job_queue.rb', line 22

def delete job
  synchronize do
    if @jobs.include? job
      job.send :queue=, nil
      @jobs.delete job
      jobs_changed
    end
    job
  end
end

#popObject

Return the next Job in the JobQueue, sorted chronologically, no earlier than its appointed time. This call will block until the next Job is ready. If the JobQueue is empty, it will block until a Job is added and that Job becomes ready.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/unicron/job_queue.rb', line 46

def pop
  synchronize do
    while true
      @jobs_changed.wait_while {@jobs.empty?}
      job = @jobs.first
      delay = job.next_run - Time.now
      break if delay <= 0
      @jobs_changed.wait delay
    end
    job.send :queue=, nil
    @jobs.delete job
  end
end