Class: SolidQueue::Dispatcher::RecurringTask

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_queue/dispatcher/recurring_task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, class_name:, schedule:, arguments: nil) ⇒ RecurringTask

Returns a new instance of RecurringTask.



17
18
19
20
21
22
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 17

def initialize(key, class_name:, schedule:, arguments: nil)
  @key = key
  @class_name = class_name
  @schedule = schedule
  @arguments = Array(arguments)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



15
16
17
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 15

def arguments
  @arguments
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



15
16
17
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 15

def class_name
  @class_name
end

#keyObject (readonly)

Returns the value of attribute key.



15
16
17
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 15

def key
  @key
end

#scheduleObject (readonly)

Returns the value of attribute schedule.



15
16
17
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 15

def schedule
  @schedule
end

Class Method Details

.from_configuration(key, **options) ⇒ Object



10
11
12
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 10

def from_configuration(key, **options)
  new(key, class_name: options[:class], schedule: options[:schedule], arguments: options[:args])
end

.wrap(args) ⇒ Object



6
7
8
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 6

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

Instance Method Details

#delay_from_nowObject



24
25
26
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 24

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

#enqueue(at:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 32

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

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

    payload[:active_job_id] = active_job.job_id if active_job
  rescue RecurringExecution::AlreadyRecorded
    payload[:skipped] = true
  rescue Job::EnqueueError => error
    payload[:enqueue_error] = error.message
  end
end

#next_timeObject



28
29
30
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 28

def next_time
  parsed_schedule.next_time.utc
end

#to_hObject



62
63
64
65
66
67
68
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 62

def to_h
  {
    schedule: schedule,
    class_name: class_name,
    arguments: arguments
  }
end

#to_sObject



58
59
60
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 58

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

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/solid_queue/dispatcher/recurring_task.rb', line 54

def valid?
  parsed_schedule.instance_of?(Fugit::Cron)
end