Skip to content

Commit

Permalink
Send correct hash value for delayed job 'handler' object.
Browse files Browse the repository at this point in the history
It'll be useful for users send a Hash object there instead sending a
YAML string.
  • Loading branch information
Jon de Andres committed Sep 15, 2015
1 parent fd86fbc commit cd2c994
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
28 changes: 26 additions & 2 deletions lib/rollbar/delayed_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
require 'delayed_job'

module Rollbar
module Delayed
class << self
attr_accessor :wrapped
end

class JobData
attr_reader :job

def initialize(job)
@job = job
end

def to_hash
job_data = job.as_json
# Here job_data['handler'] is a YAML object comming
# from the storage backend
job_data['handler'] = job.payload_object.as_json

job_data
end
end

self.wrapped = false

def self.wrap_worker
Expand Down Expand Up @@ -33,10 +52,15 @@ def self.invoke_job_callback
def self.report(e, job)
return unless job.attempts <= ::Rollbar.configuration.dj_threshold

job_data = job.as_json
data = ::Rollbar.configuration.report_dj_data ? job_data : nil
data = build_job_data(job)

::Rollbar.scope(:request => data).error(e, :use_exception_level_filters => true)
end

def self.build_job_data(job)
return nil unless ::Rollbar.configuration.report_dj_data

JobData.new(job).to_hash
end
end
end
35 changes: 35 additions & 0 deletions spec/rollbar/delayed_job/job_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'

require 'rollbar/delayed_job'
require 'delayed/backend/test'

describe Rollbar::Delayed::JobData do
describe '#to_hash' do
let(:handler) { { 'foo' => 'bar' } }

let(:attrs) do
{
'id' => 1,
'priority' => 0,
'attempts' => 1,
'handler' => handler.to_yaml
}
end

let(:job) do
::Delayed::Backend::Test::Job.new(attrs)
end

subject { described_class.new(job) }

it 'returns the correct job data' do
expected_result = attrs.dup
expected_result.delete('id')
expected_result['handler'] = handler

result = subject.to_hash

expect(result).to be_eql(expected_result)
end
end
end
27 changes: 25 additions & 2 deletions spec/rollbar/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,41 @@ def do_job_please!(a, b)
Delayed::Backend::Test::Job.delete_all
end

let(:logger) { Rollbar.logger }
let(:expected_args) do
[kind_of(NoMethodError), { :use_exception_level_filters => true }]
end

context 'with delayed method without arguments failing' do
it 'sends the exception' do
expect_any_instance_of(::Delayed::Job).to receive(:as_json).and_call_original
expect(Rollbar).to receive(:scope).with(kind_of(Hash)).and_call_original
expect_any_instance_of(Rollbar::Notifier).to receive(:error).with(*expected_args)

FailingJob.new.delay.do_job_please!(:foo, :bar)
end
end


describe '.build_job_data' do
let(:job) { {} }
context 'if report_dj_data is disabled' do
before do
allow(Rollbar.configuration).to receive(:report_dj_data).and_return(false)
end

it 'returns nil' do
expect(described_class.build_job_data(job)).to be_nil
end
end

context 'with report_dj_data enabled' do
before do
allow(Rollbar.configuration).to receive(:report_dj_data).and_return(true)
end

it 'returns a hash' do
result = described_class.build_job_data(job)
expect(result).to be_kind_of(Hash)
end
end
end
end

0 comments on commit cd2c994

Please sign in to comment.