mosquito-cr / mosquito

A background task runner for crystal applications supporting periodic (CRON) and manually queued jobs
MIT License
227 stars 24 forks source link

Support error handlers #97

Closed confact closed 2 years ago

confact commented 2 years ago

To be able to send exceptions and errors to Sentry, rollbar, bugsnag, errordeck, and so on.

It would help to figure out easier what is going on in the jobs.

robacarp commented 2 years ago

@confact thanks, this has been in my mind, but I didn't have an issue for it yet.

robacarp commented 2 years ago

@confact I think this is already possible with the current RC, which includes before/after hooks. Here's a minimal example:

class ErrorHandlerJob < Mosquito::QueuedJob
  def perform
    raise "Error!"
  end

  after do
    return unless failed?
    Log.error(exception: exception) { "Job failed after hook" }
    # Honeybadger.dispatch exception
  end
end

This pattern could be monkey-patched into Mosquito::Job to be applied application wide, or packed up as a module to be selectively included in jobs which need it.

Notably this would miss exceptions which are thrown in an after-hook. That would actually crash the runner itself, and it would require an additional catch written around the runner initiation. Let me know if that doesn't cast a wide enough net. I'm going to mark this as done, and go write a page for the website about it.

robacarp commented 2 years ago

https://mosquito-cr.github.io/manual/error_reporting.html via https://github.com/mosquito-cr/mosquito-cr.github.io/commit/f6dc433efb9c950cfb966b143a27edba12e61328