Class: Kuroko2::JobSchedule

Inherits:
ApplicationRecord show all
Includes:
TableNameCustomizable
Defined in:
app/models/kuroko2/job_schedule.rb

Constant Summary collapse

CRON_FORMAT =
/\A
  (?:[1-5]?[0-9]|(?:[1-5]?[0-9]\-[1-5]?[0-9]|\*)(?:\/[1-5]?[0-9])?)(?:,(?:[1-5]?[0-9]|(?:[1-5]?[0-9]\-[1-5]?[0-9]|\*)(?:\/[1-5]?[0-9])?))*
  \s+
  (?:1?[0-9]|2[0-3]|(?:(?:1?[0-9]|2[0-3])\-(?:1?[0-9]|2[0-3])|\*)(?:\/(?:1?[0-9]|2[0-3]))?)(?:,(?:1?[0-9]|2[0-3]|(?:(?:1?[0-9]|2[0-3])\-(?:1?[0-9]|2[0-3])|\*)(?:\/(?:1?[0-9]|2[0-3]))?))*
  \s+
  (?:(?:[1-9]|[1-2][0-9]|3[0-1])|(?:(?:[1-9]|[1-2][0-9]|3[0-1])\-(?:[1-9]|[1-2][0-9]|3[0-1])|\*)(?:\/(?:[1-9]|[1-2][0-9]|3[0-1]))?)(?:,(?:(?:[1-9]|[1-2][0-9]|3[0-1])|(?:(?:[1-9]|[1-2][0-9]|3[0-1])\-(?:[1-9]|[1-2][0-9]|3[0-1])|\*)(?:\/(?:[1-9]|[1-2][0-9]|3[0-1]))?))*
  \s+
  (?:(?:[1-9]|1[0-2])|(?:(?:[1-9]|1[0-2])\-(?:[1-9]|1[0-2])|\*)(?:\/(?:[1-9]|1[0-2]))?)(?:,(?:(?:[1-9]|1[0-2])|(?:(?:[1-9]|1[0-2])\-(?:[1-9]|1[0-2])|\*)(?:\/(?:[1-9]|1[0-2]))?))*
  \s+
  (?:[0-6]|(?:(?:[0-6]\-[0-6]|\*)(?:\/[0-6])?))(?:,(?:[0-6]|(?:(?:[0-6]\-[0-6]|\*)(?:\/[0-6])?)))*
\z/x

Instance Method Summary collapse

Instance Method Details

#next(now = Time.current) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/kuroko2/job_schedule.rb', line 21

def next(now = Time.current)
  if 1.month.ago(now).future?
    Kuroko2.logger.warn("Exceeds the time of criteria #{now}. (Up to 1 month since)")
    return
  end

  next_time = Chrono::Iterator.new(self.cron, now: now).next
  suspend_times = suspend_times(now, next_time)

  if suspend_times.include?(next_time)
    self.next(next_time)
  else
    next_time
  end
end

#scheduled_times(time_from, time_to) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/kuroko2/job_schedule.rb', line 37

def scheduled_times(time_from, time_to)
  it = Chrono::Iterator.new(cron, now: time_from)
  scheduled_times = []

  loop do
    next_time = it.next
    if next_time <= time_to
      scheduled_times << next_time
    else
      break
    end
  end

  scheduled_times
end

#suspend_times(time_from, time_to) ⇒ Object



53
54
55
56
57
58
59
60
# File 'app/models/kuroko2/job_schedule.rb', line 53

def suspend_times(time_from, time_to)
  if job_definition && job_definition.job_suspend_schedules.present?
    job_definition.job_suspend_schedules.
      map { |schedule| schedule.suspend_times(time_from, time_to) }.flatten.uniq
  else
    []
  end
end