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
CHRONO_SCHEDULE_METHODS =
i[minutes hours days months wdays]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.launch_scheduled_jobs!(time_from, time_to) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/kuroko2/job_schedule.rb', line 109

def self.launch_scheduled_jobs!(time_from, time_to)
  includes(job_definition: :job_suspend_schedules).find_each do |schedule|
    definition = schedule.job_definition
    suspend_times = schedule.suspend_times(time_from, time_to)

    schedule.scheduled_times(time_from, time_to).each do |time|
      if definition.suspended?
        Kuroko2.logger.info("Skipped suspended \"##{definition.id} #{definition.name}\" that is scheduled at #{I18n.l(time, format: :short)} by `#{schedule.cron}`")
      elsif suspend_times.include?(time)
        Kuroko2.logger.info("Skipped schedule suspended \"##{definition.id} #{definition.name}\" that is scheduled at #{I18n.l(time, format: :short)} by `#{schedule.cron}`")
      else
        launched_by = "\"##{definition.id} #{definition.name}\" that is scheduled at #{I18n.l(time, format: :short)} by `#{schedule.cron}`"
        definition.create_instance(launched_by: launched_by)
      end
    end
  end
end

Instance Method Details

#next(now = Time.current) ⇒ Object



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

def next(now = Time.current)
  return if suspended_all?

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

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

#scheduled_times(time_from, time_to) ⇒ Object



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

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.map(&:in_time_zone)
end

#suspend_times(time_from, time_to) ⇒ Object



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

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