What is the best way to configure asynchronous configuration with Sidekiq? I don't want to pass the full Raven::Event instance to the Sidekiq worker so I'm thinking of doing something like this:
config.async = lambda do |event|
SentryWorker.perform_async(event_to_hash.to_json)
end
And then in my worker, I would convert the JSON back into a Raven::Event before calling Raven.send_event on it:
class SentryWorker
include Sidekiq::Worker
def perform(event_json)
event = create_event_from_json(event_json)
Raven.send_event(event)
rescue JSON::ParseError
end
end
I've looked through the Raven::Event source and there doesn't appear to be an obvious way to re-create the event from a hash. It'd be nice if there were Raven::Event.from_hash method, but in lieu of that should I create a new event in the worker and pass in the details from the hash. That approach seems possible with the exception of the interfaces portion of the object.
Am I missing something? Or am I on the right track?
Hi!
It looks like you're mistaken about when and how objects are converted into hashes and JSON in Sidekiq and raven-ruby. Here's all you need to do:
config.async = lambda do |event|
# Event is a hash in raven-ruby 2+
SentryWorker.perform_async(event)
# Sentry converts the hash into JSON for us, we don't need to call to_json.
end
class SentryWorker
include Sidekiq::Worker
def perform(event_hash)
# Sidekiq has converted the argument back into a Ruby hash object
Raven.send_event(event_hash) # send_event takes a hash or a Raven::Event.
# Rescuing a parseerror here isn't necessary because Sidekiq would have already raised
end
end
Thanks @nateberkopec. Indeed I was looking at an older version of the gem that was fairly out of date. I see the new implementation passes a hash object. Thanks for your help! 馃槃
Most helpful comment
Hi!
It looks like you're mistaken about when and how objects are converted into hashes and JSON in Sidekiq and
raven-ruby. Here's all you need to do: