Class: QueueClassicPlus::CustomWorker

Inherits:
QC::Worker
  • Object
show all
Defined in:
lib/queue_classic_plus/worker.rb

Constant Summary collapse

CONNECTION_ERRORS =
[PG::UnableToSend, PG::ConnectionBad].freeze
BACKOFF_WIDTH =
10
FailedQueue =
QC::Queue.new("failed_jobs")

Instance Method Summary collapse

Instance Method Details

#handle_failure(job, e) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/queue_classic_plus/worker.rb', line 10

def handle_failure(job, e)
  QueueClassicPlus.logger.info "Handling exception #{e.class} - #{e.message} for job #{job[:id]}"

  force_retry = false
  if connection_error?(e)
    # If we've got here, unfortunately ActiveRecord's rollback mechanism may
    # not have kicked in yet and we might be in a failed transaction. To be
    # *absolutely* sure the retry/failure gets enqueued, we do a rollback
    # just in case (and if we're not in a transaction it will be a no-op).
    QueueClassicPlus.logger.info "Reset connection for job #{job[:id]}"
    @conn_adapter.connection.reset
    @conn_adapter.execute 'ROLLBACK'

    # We definitely want to retry because the connection was lost mid-task.
    force_retry = true
  end

  @failed_job_class_memoized = false
  @failed_job = job
  @raw_args = job[:args]

  if queue_classic_plus_job?
    @failed_job_args = failed_job_class.deserialized(@raw_args)

    if force_retry && !failed_job_class.disable_retries
      Metrics.increment("qc.force_retry", source: @q_name)
      retry_with_remaining(e)
    elsif failed_job_class.retries_on?(e)
      Metrics.increment("qc.retry", source: @q_name)
      retry_with_remaining(e)
    else
      enqueue_failed(e)
    end
  else
    enqueue_failed(e)
  end

  FailedQueue.delete(@failed_job[:id])
end