summaryrefslogtreecommitdiff
path: root/app/models/concerns/schedulable.rb
blob: 6fdca4f50c3e7f1bebf61e7c9b7bc97bab05d301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

module Schedulable
  extend ActiveSupport::Concern

  included do
    scope :runnable_schedules, -> { active.where("next_run_at < ?", Time.zone.now) }

    before_save :set_next_run_at
  end

  def schedule_next_run!
    save! # with set_next_run_at
  rescue ActiveRecord::RecordInvalid
    update_column(:next_run_at, nil) # update without validation
  end

  def set_next_run_at
    raise NotImplementedError
  end
end