Class: Rufus::Scheduler::RepeatJob

Inherits:
Job
  • Object
show all
Defined in:
lib/rufus/scheduler/jobs_repeat.rb

Direct Known Subclasses

CronJob, EvInJob

Constant Summary collapse

FIRSTS =
[ :now, :immediately, 0 ].freeze

Constants inherited from Job

Job::EoTime

Instance Attribute Summary collapse

Attributes inherited from Job

#callable, #count, #handler, #id, #last_time, #last_work_time, #locals, #mean_work_time, #name, #next_time, #opts, #original, #previous_time, #scheduled_at, #tags, #unscheduled_at

Instance Method Summary collapse

Methods inherited from Job

#[], #[]=, #call, #check_frequency, #entries, #has_key?, #keys, #kill, #past?, #resume_discard_past=, #running?, #scheduled?, #source_location, #threads, #trigger_off_schedule, #unschedule, #values

Constructor Details

#initialize(scheduler, duration, opts, block) ⇒ RepeatJob

Returns a new instance of RepeatJob.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 10

def initialize(scheduler, duration, opts, block)

  super

  @paused_at = nil

  @times = opts[:times]

  @first_at_no_error = opts[:first_at_no_error] || false

  fail ArgumentError.new(
    "cannot accept :times => #{@times.inspect}, not nil or an int"
  ) unless @times == nil || @times.is_a?(Integer)

  self.first_at =
    opts[:first] || opts[:first_time] ||
    opts[:first_at] || opts[:first_in] ||
    nil
  self.last_at =
    opts[:last] || opts[:last_at] || opts[:last_in]

  @resume_discard_past = nil
end

Instance Attribute Details

#first_atObject

Returns the value of attribute first_at.



6
7
8
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 6

def first_at
  @first_at
end

#last_atObject

Returns the value of attribute last_at.



7
8
9
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 7

def last_at
  @last_at
end

#paused_atObject (readonly)

Returns the value of attribute paused_at.



4
5
6
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 4

def paused_at
  @paused_at
end

#timesObject

Returns the value of attribute times.



8
9
10
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 8

def times
  @times
end

Instance Method Details

#determine_idObject



107
108
109
110
111
112
113
114
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 107

def determine_id

  [
    self.class.name.split(':').last.downcase[0..-4],
    @scheduled_at.to_f,
    (self.object_id < 0 ? 'm' : '') + self.object_id.to_s
  ].map(&:to_s).join('_')
end

#next_times(count) ⇒ Object

Starting from now, returns the Job#count next occurences (EtOrbi::EoTime instances) for this job.

Warning, for IntervalJob, the @mean_work_time is used since “interval” works from the end of a job to its next trigger (not from one trigger to the next, as for “cron” and “every”).



144
145
146
147
148
149
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 144

def next_times(count)

  (count - 1).times.inject([ next_time ]) { |a|
    a << next_time_from(a.last)
    a }
end

#occurrences(time0, time1) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 116

def occurrences(time0, time1)

  a = []

  nt = @next_time
  ts = @times

  loop do

    break if nt > time1
    break if ts && ts <= 0

    a << nt if nt >= time0

    nt = next_time_from(nt)
    ts = ts - 1 if ts
  end

  a
end

#pauseObject



91
92
93
94
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 91

def pause

  @paused_at = EoTime.now
end

#paused?Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 102

def paused?

  !! @paused_at
end

#resume(opts = {}) ⇒ Object



96
97
98
99
100
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 96

def resume(opts={})

  @resume_discard_past = opts[:discard_past]
  @paused_at = nil
end

#trigger(time) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rufus/scheduler/jobs_repeat.rb', line 76

def trigger(time)

  return if @paused_at
  #return set_next_time(time) if @paused_at

  return (@next_time = nil) if @times && @times < 1
  return (@next_time = nil) if @last_at && time >= @last_at
    #
    # It keeps jobs one step too much in @jobs, but it's OK

  super

  @times -= 1 if @times
end