Class: ThreeScale::Backend::FailedJobsScheduler

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/3scale/backend/failed_jobs_scheduler.rb

Class Method Summary collapse

Methods included from Logging

enable!, included

Class Method Details

.reschedule_failed_jobsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/3scale/backend/failed_jobs_scheduler.rb', line 31

def reschedule_failed_jobs
  # There might be several callers trying to requeue failed jobs at the
  # same time. We need to use a lock to avoid rescheduling the same
  # failed job more than once.
  key = dist_lock.lock

  ttl_expiration_time = Time.now + TTL_RESCHEDULE_S
  rescheduled = failed_while_rescheduling = 0

  if key
    number_of_jobs_to_reschedule.times do
      break unless time_for_another_reschedule?(ttl_expiration_time)

      requeue_result = requeue_oldest_failed_job

      if requeue_result[:rescheduled?]
        rescheduled += 1
      else
        failed_while_rescheduling += 1
      end

      # :ok_to_remove? is false only when the requeue() call fails
      # because there are no more jobs in the queue.
      requeue_result[:ok_to_remove?] ? remove_oldest_failed_job : break
    end

    dist_lock.unlock if key == dist_lock.current_lock_key
  end

  { rescheduled: rescheduled,
    failed_while_rescheduling: failed_while_rescheduling,
    failed_current: failed_queue.count }
end