Last active
July 29, 2017 17:54
-
-
Save nicksieger/8854494c085d30af1a9bae32eb9e8166 to your computer and use it in GitHub Desktop.
Monkeypatch DJ to make payload_object exception-safe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Monkeypatch payload_object like https://github.com/collectiveidea/delayed_job/pull/990 | |
module Delayed | |
module Backend | |
class InvalidPayload | |
ParseObjectFromYaml = %r{\!ruby/\w+\:([^\s]+)} # rubocop:disable ConstantName | |
def initialize(handler, exception) | |
@handler = handler | |
@exception = deserialization_error(exception) | |
end | |
def display_name | |
result = ParseObjectFromYaml.match(@handler) | |
result && result[1] || self.class.name | |
end | |
def perform | |
raise @exception | |
end | |
def deserialization_error(e) | |
return e if e.is_a?(DeserializationError) | |
DeserializationError.new "Job failed to load: #{e.message}. Handler: #{@handler.inspect}" | |
end | |
end | |
end | |
end | |
module Delayed | |
module Backend | |
module Base | |
def payload_object | |
return @payload_object if @payload_object | |
@payload_object = YAML.load_dj(handler) | |
unless @payload_object.respond_to?(:perform) | |
@payload_object = Backend::InvalidPayload.new(handler, NoMethodError.new("undefined method `perform' for #{@payload_object.inspect}")) | |
end | |
@payload_object | |
rescue Exception => e # rubocop:disable RescueException | |
@payload_object = Backend::InvalidPayload.new(handler, e) | |
end | |
end | |
end | |
end | |
## End Monkeypatch #990 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment