Class: SolidQueue::RecurringTask

Inherits:
Record
  • Object
show all
Defined in:
app/models/solid_queue/recurring_task.rb

Defined Under Namespace

Classes: Arguments

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

non_blocking_lock

Class Method Details

.create_or_update_all(tasks) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'app/models/solid_queue/recurring_task.rb', line 36

def create_or_update_all(tasks)
  if connection.supports_insert_conflict_target?
    # PostgreSQL fails and aborts the current transaction when it hits a duplicate key conflict
    # during two concurrent INSERTs for the same value of an unique index. We need to explicitly
    # indicate unique_by to ignore duplicate rows by this value when inserting
    upsert_all tasks.map(&:attributes_for_upsert), unique_by: :key
  else
    upsert_all tasks.map(&:attributes_for_upsert)
  end
end

.from_configuration(key, **options) ⇒ Object



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

def from_configuration(key, **options)
  new \
    key: key,
    class_name: options[:class],
    command: options[:command],
    arguments: options[:args],
    schedule: options[:schedule],
    queue_name: options[:queue].presence,
    priority: options[:priority].presence,
    description: options[:description],
    static: true
end

.wrap(args) ⇒ Object



19
20
21
# File 'app/models/solid_queue/recurring_task.rb', line 19

def wrap(args)
  args.is_a?(self) ? args : from_configuration(args.first, **args.second)
end

Instance Method Details

#attributes_for_upsertObject



86
87
88
# File 'app/models/solid_queue/recurring_task.rb', line 86

def attributes_for_upsert
  attributes.without("id", "created_at", "updated_at")
end

#delay_from_nowObject



48
49
50
# File 'app/models/solid_queue/recurring_task.rb', line 48

def delay_from_now
  [ (next_time - Time.current).to_f, 0 ].max
end

#enqueue(at:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/solid_queue/recurring_task.rb', line 56

def enqueue(at:)
  SolidQueue.instrument(:enqueue_recurring_task, task: key, at: at) do |payload|
    active_job = if using_solid_queue_adapter?
      enqueue_and_record(run_at: at)
    else
      payload[:other_adapter] = true

      perform_later.tap do |job|
        unless job.successfully_enqueued?
          payload[:enqueue_error] = job.enqueue_error&.message
        end
      end
    end

    active_job.tap do |enqueued_job|
      payload[:active_job_id] = enqueued_job.job_id
    end
  rescue RecurringExecution::AlreadyRecorded
    payload[:skipped] = true
    false
  rescue Job::EnqueueError => error
    payload[:enqueue_error] = error.message
    false
  end
end

#next_timeObject



52
53
54
# File 'app/models/solid_queue/recurring_task.rb', line 52

def next_time
  parsed_schedule.next_time.utc
end

#to_sObject



82
83
84
# File 'app/models/solid_queue/recurring_task.rb', line 82

def to_s
  "#{class_name}.perform_later(#{arguments.map(&:inspect).join(",")}) [ #{parsed_schedule.original} ]"
end