Skip to content

Commit

Permalink
Fix DelayedJob callback
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisbernard committed Apr 18, 2024
1 parent 55a65aa commit 4718c50
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Fix DelayedJob callback
* Limit exception message to 1 millions chars
* Fix issue #28 when for Rodauth authentication

Expand Down
14 changes: 8 additions & 6 deletions lib/rorvswild/plugin/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ module Plugin
module DelayedJob
def self.setup
return if @installed
return unless defined?(Delayed::Worker)
Delayed::Worker.lifecycle.around(:invoke_job, &method(:around_perform))
return unless defined?(Delayed::Plugin)
Delayed::Worker.plugins << Class.new(Delayed::Plugin) do
callbacks do |lifecycle|
lifecycle.around(:invoke_job) do |job, *args, &block|
RorVsWild.agent.measure_job(job.name, parameters: job.payload_object) { block.call(job) }
end
end
end
@installed = true
end

def self.around_perform(job, &block)
RorVsWild.agent.measure_job(job.name, parameters: job.payload_object) { block.call(job) }
end
end
end
end
15 changes: 6 additions & 9 deletions test/plugin/delayed_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,24 @@ def perform
end
end

Delayed::Worker.delay_jobs = false

class SampleBackend
include Delayed::Backend::Base

attr_accessor :handler

def initialize(options)
@payload_object = options[:payload_object]
end
end

def test_callback
agent.expects(:queue_job)
SampleBackend.enqueue(SampleJob.new(true))
backend = SampleBackend.new
backend.payload_object = SampleJob.new(true)
backend.invoke_job
assert_equal("RorVsWild::Plugin::DelayedJobTest::SampleJob", agent.current_data[:name])
end

def test_callback_on_exception
agent.expects(:queue_job)
SampleBackend.enqueue(job = SampleJob.new(false))
backend = SampleBackend.new
backend.payload_object = job = SampleJob.new(false)
backend.invoke_job
rescue
ensure
assert_equal(job, agent.current_data[:error][:parameters])
Expand Down

0 comments on commit 4718c50

Please sign in to comment.