Class: Pgbus::RecurringExecution

Inherits:
BusRecord
  • Object
show all
Defined in:
app/models/pgbus/recurring_execution.rb

Class Method Summary collapse

Class Method Details

.last_execution(task_key) ⇒ Object

Find the most recent execution for a task



29
30
31
# File 'app/models/pgbus/recurring_execution.rb', line 29

def self.last_execution(task_key)
  for_task(task_key).order(run_at: :desc).first
end

.record(task_key, run_at) ⇒ Object

Record a recurring execution with deduplication. Uses the unique index on (task_key, run_at) to prevent duplicates. Yields to the caller to perform the actual enqueue.



17
18
19
20
21
22
23
24
25
26
# File 'app/models/pgbus/recurring_execution.rb', line 17

def self.record(task_key, run_at)
  transaction do
    execution = create!(task_key: task_key, run_at: run_at)
    yield execution if block_given?
    execution
  end
rescue ActiveRecord::RecordNotUnique
  raise Pgbus::Recurring::AlreadyRecorded,
        "Recurring task '#{task_key}' already recorded for #{run_at.iso8601}"
end