Sha256: 481201a6e5321b445c5cff6849f3cda1cb8c54cdda0de469c24de9afefd41149

Contents?: true

Size: 862 Bytes

Versions: 8

Compression:

Stored size: 862 Bytes

Contents

class Kuroko2::JobSuspendSchedule < Kuroko2::ApplicationRecord
  include Kuroko2::TableNameCustomizable

  belongs_to :job_definition

  validates :cron, format: { with: Kuroko2::JobSchedule::CRON_FORMAT }, uniqueness: { scope: :job_definition_id }
  validate :validate_cron_schedule

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

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

    suspend_times.map(&:in_time_zone)
  end

  private

  def validate_cron_schedule
    if Kuroko2::JobSchedule::CRON_FORMAT === cron
      Chrono::Iterator.new(self.cron).next
    end
    nil
  rescue Chrono::Fields::Base::InvalidField => e
    errors.add(:cron, "has invalid field: #{e.message}")
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
kuroko2-0.4.2 app/models/kuroko2/job_suspend_schedule.rb
kuroko2-0.4.1 app/models/kuroko2/job_suspend_schedule.rb
kuroko2-0.4.0 app/models/kuroko2/job_suspend_schedule.rb
kuroko2-0.3.4 app/models/kuroko2/job_suspend_schedule.rb
kuroko2-0.3.3 app/models/kuroko2/job_suspend_schedule.rb
kuroko2-0.3.2 app/models/kuroko2/job_suspend_schedule.rb
kuroko2-0.3.1 app/models/kuroko2/job_suspend_schedule.rb
kuroko2-0.3.0 app/models/kuroko2/job_suspend_schedule.rb