module RocketJob::Plugins::Cron

Allow jobs to run on a predefined schedule, much like a crontab.

Notes:

Public Instance Methods

rocket_job_cron_set_run_at() click to toggle source
# File lib/rocket_job/plugins/cron.rb, line 56
def rocket_job_cron_set_run_at
  return if cron_schedule.nil? || !(cron_schedule_changed? && !run_at_changed?)

  self.run_at = Fugit::Cron.new(cron_schedule).next_time.to_utc_time
end

Private Instance Methods

rocket_job_cron_duplicate?() click to toggle source

Returns [true|false] whether another instance of this job with the same cron schedule is already active

# File lib/rocket_job/plugins/cron.rb, line 81
def rocket_job_cron_duplicate?
  self.class.with(read: {mode: :primary}) do |conn|
    conn.where(:state.in => %i[queued running failed paused], :id.ne => id, cron_schedule: cron_schedule).exists?
  end
end
rocket_job_cron_end_state() click to toggle source
# File lib/rocket_job/plugins/cron.rb, line 72
def rocket_job_cron_end_state
  return unless cron_schedule && !cron_after_start

  current_cron_schedule = cron_schedule
  update_attribute(:cron_schedule, nil)
  create_restart!(cron_schedule: current_cron_schedule)
end
rocket_job_cron_on_start() click to toggle source
# File lib/rocket_job/plugins/cron.rb, line 64
def rocket_job_cron_on_start
  return unless cron_schedule && cron_after_start

  current_cron_schedule = cron_schedule
  update_attribute(:cron_schedule, nil)
  create_restart!(cron_schedule: current_cron_schedule)
end
rocket_job_cron_singleton_check() click to toggle source

Prevent creation of a new job when another is running with the same cron schedule.

# File lib/rocket_job/plugins/cron.rb, line 88
def rocket_job_cron_singleton_check
  return if cron_schedule.nil? || completed? || aborted? || !rocket_job_cron_duplicate?

  errors.add(:state, "Another instance of #{self.class.name} is already queued, running, failed, or paused with the same cron schedule: #{cron_schedule}")
end