Browse Source

Refactoring - Delayed::Job to ActiveJob migration: Allow tracking of ActiveJob#executions via Delayed::Job#attempts sync workaround.

Thorsten Eckel 6 years ago
parent
commit
4d0130810f
2 changed files with 28 additions and 0 deletions
  1. 10 0
      app/jobs/application_job.rb
  2. 18 0
      spec/jobs/application_job_spec.rb

+ 10 - 0
app/jobs/application_job.rb

@@ -4,4 +4,14 @@ class ApplicationJob < ActiveJob::Base
 
   # Most jobs are safe to ignore if the underlying records are no longer available
   # discard_on ActiveJob::DeserializationError
+
+  # We (currently) rely on Delayed::Job#attempts to check for stuck backends
+  # e.g. in the MonitoringController.
+  # This is a workaround to sync ActiveJob#executions to Delayed::Job#attempts
+  # until we resolve this dependency.
+  around_enqueue do |job, block|
+    block.call.tap do |delayed_job|
+      delayed_job.update!(attempts: job.executions)
+    end
+  end
 end

+ 18 - 0
spec/jobs/application_job_spec.rb

@@ -0,0 +1,18 @@
+require 'rails_helper'
+
+class FailingTestJob < ApplicationJob
+  retry_on(StandardError, attempts: 5)
+
+  def perform
+    Rails.logger.debug 'Failing'
+    raise 'Some error...'
+  end
+end
+
+RSpec.describe ApplicationJob, type: :job do
+
+  it 'syncs ActiveJob#executions to Delayed::Job#attempts' do
+    FailingTestJob.perform_later
+    expect { Delayed::Worker.new.work_off }.to change { Delayed::Job.last.attempts }
+  end
+end