While introducing sidekiq that processes jobs asynchronously, if you want to customize it for your own application while taking advantage of convenience, settings that are useful in such cases It is summarized.
sidekiq is a library that enables asynchronous processing. When executing multiple jobs at the same time, you can specify the processing priority by separating the queue name of each job. I think there are similar libraries such as resque and delayed_job. It's easy to install, but there are also troublesome points such as retrying the stumbled queue 25 times or 26 times by default, and then not being able to see the log of the queue that was running as DEAD. There is. So, in this article, I will talk about how to set the upper limit to DEAD without retrying when throwing an exception, and then set the contents of the queue and error message to slack at that time.
Ruby 2.6.6 Rails 6.0.2 sidekiq requires redis.
# On OSX
brew update
brew install redis
brew services start redis
You can check it on the dashboard by adding sidekiq-failures to the gem, and you can also retry all failed queues.
gem 'sidekiq-failures'
You can also check the details of the error in the failure list.
It seems that there are many other gems such as knowing & analyzing the thrown cue, so I will post it for reference.
I think that sidekiq was designed to kill the queue after retrying about 25 or 6 times by default. So, for those who want to process a lot of jobs at once, I don't have to retry so much, so I tried several times and told me if it failed. Describe in the file.
app/jobs/your_job.rb
class YourJob < ActiveJob::Base
...
queue_as :default
sidekiq_options retry: 5
...
end
With this, the queue that failed 5 times becomes a DEAD queue.
Slack-incoming-webhooks is a gem that makes slack notifications very easy in Rails apps.
gem 'slack-incoming-webhooks'
Get the URL of the channel to be notified [this page](https://slack.com/intl/ja-jp/help/articles/115005265063-Slack-%E3%81%A7%E3%81%AE-Incoming-Webhook -Check% E3% 81% AE% E5% 88% A9% E7% 94% A8).
Set what to do if the queue dies in initializer (save channel URL in .env)
app/config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
config.death_handlers << ->(job, ex) do
slack = Slack::Incoming::Webhooks.new(ENV['SLACK_WEBHOOK_URL'])
attachments = [{
title: "Sidekiq failure",
text: "ONE DEAD JOB IS FOUND:\n (#{job['args']}) \n msg(#{job['error_message']})",
color: "#fb2489"
}]
slack.post "", attachments: attachments
end
end
Queues that have naturally become DEAD will now be notified in slack.
--No notification comes when killing a queue on the sidekiq dashboard If you set notifications in config.death_handlers, you will not be notified if you manually kill the queue on the dashboard. Therefore, you will be notified only when you exceed the retry limit of 5 times.
--Rescure_from Exception does not retry when notification is set Since it is an exception handling, once the job is processed and an error occurs, it is considered dead, and it will notify you of slack but will not retry. If for some reason it is not processed and you still want to retry, we recommend setting slack notification in config.death_handlers as described above.
Recommended Posts