Class: Unicron::Job

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

Overview

Job contains all of the information pertinent to a particular task that can be managed by Unicron.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &task) ⇒ Job

Create a new Job. The code to run can be specified either as a block to the constructor or as an option named task. If both are specified, the option takes precedence over the block.

For non-repeating Jobs, you must use either at or delay to specify when the Job should be run. If at is specified, the job will run at that time (or immediately, if at is in the past). If delay is specified, it will run that number of seconds after it is scheduled.

For repeating Jobs, set repeat to the number of times to repeat the Job, or true to repeat infinitely. The delay option becomes mandatory and specifies the number of seconds between invocations. If the at option is specified, it gives the time of the first invocation; otherwise, it first fires delay seconds in the future.

For both repeating and non-repeating jobs, if both at and delay are specified, and at is in the past, the first invocation will occur at the first even multiple of delay seconds after at. This provides an easy way of ensuring that the jobs are run at the same time every hour or day.



85
86
87
# File 'lib/unicron/job.rb', line 85

def initialize options={}, &task
  self.options = options.merge task: task
end

Instance Attribute Details

#atObject

Time of the first run of this Job.



8
9
10
# File 'lib/unicron/job.rb', line 8

def at
  @at
end

#cancelledObject (readonly)

True if a cancellation is pending. Only meaningful while the Job is running.



18
19
20
# File 'lib/unicron/job.rb', line 18

def cancelled
  @cancelled
end

#delayObject

Time before the first run, and between repeats of this Job.



21
22
23
# File 'lib/unicron/job.rb', line 21

def delay
  @delay
end

#next_runObject (readonly)

Time of the next Job run.



29
30
31
# File 'lib/unicron/job.rb', line 29

def next_run
  @next_run
end

#previous_runObject (readonly)

Time of the previous Job run.



32
33
34
# File 'lib/unicron/job.rb', line 32

def previous_run
  @previous_run
end

#repeatObject

The number of times to repeat, or true for an infinitely repeating Job.



35
36
37
# File 'lib/unicron/job.rb', line 35

def repeat
  @repeat
end

#taskObject

Proc that gets called when the Job is run.



43
44
45
# File 'lib/unicron/job.rb', line 43

def task
  @task
end

Instance Method Details

#<=>(other) ⇒ Object

Compare two Job instances chronologically by time of next run.



51
52
53
# File 'lib/unicron/job.rb', line 51

def <=> other
  next_run <=> other.next_run
end

#cancelObject

Deschedules the Job.



56
57
58
59
60
61
62
# File 'lib/unicron/job.rb', line 56

def cancel
  if queue
    queue.delete self
  else
    @cancelled = true
  end
end

#optionsObject

Return a Hash of this Job’s options.



90
91
92
# File 'lib/unicron/job.rb', line 90

def options
  {at: at, delay: delay, repeat: repeat, task: task}
end

#options=(value) ⇒ Object

Set many Job options at once.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/unicron/job.rb', line 97

def options= value
  notify_queue = !queue.nil? && [:at, :delay].any? {|k| value.has_key? k}
  value = {at: at, delay: delay, repeat: repeat, task: task}.merge! value
  if value[:task].nil?
    raise ArgumentError, 'No task specified.'
  end
  if value[:at].nil? && value[:delay].nil?
    raise ArgumentError, 'Either at or delay must be specified.'
  end
  if value[:repeat] && value[:delay].nil?
    raise ArgumentError, 'Repeating jobs must specify delay.'
  end
  @at = value[:at]
  @delay = value[:delay]
  @repeat = value[:repeat]
  @task = value[:task]
  queue.send :jobs_changed if notify_queue
  value
end

#runObject

Runs the Job, yielding self.



120
121
122
123
124
# File 'lib/unicron/job.rb', line 120

def run
  self.repeat -= 1 if repeat.is_a? Numeric
  task.call self
  @previous_run = next_run
end