|
| 1 | +# Sidekiq::Rescue |
| 2 | + |
| 3 | +[](https://github.com/moofkit/sidekiq-rescue/actions/workflows/main.yml) |
| 4 | + |
| 5 | +[Sidekiq](https://github.com/sidekiq/sidekiq) plugin to rescue jobs from expected errors and retry them later. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Add this line to your application's Gemfile: |
| 10 | + |
| 11 | +```ruby |
| 12 | +gem "sidekiq-rescue" |
| 13 | +``` |
| 14 | + |
| 15 | +And then execute: |
| 16 | + |
| 17 | + $ bundle install |
| 18 | + |
| 19 | +Or install it yourself as: |
| 20 | + |
| 21 | + $ gem install sidekiq-rescue |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +1. Add the middleware to your Sidekiq configuration: |
| 26 | + |
| 27 | +```ruby |
| 28 | +Sidekiq.configure_server do |config| |
| 29 | + config.server_middleware do |chain| |
| 30 | + chain.add Sidekiq::Rescue::Middleware |
| 31 | + end |
| 32 | +end |
| 33 | +``` |
| 34 | + |
| 35 | +2. Add DSL to your job: |
| 36 | + |
| 37 | +```ruby |
| 38 | +class MyJob |
| 39 | + include Sidekiq::Job |
| 40 | + include Sidekiq::Rescue::DSL |
| 41 | + |
| 42 | + sidekiq_rescue ExpectedError |
| 43 | + |
| 44 | + def perform(*) |
| 45 | + # ... |
| 46 | + end |
| 47 | +end |
| 48 | +``` |
| 49 | + |
| 50 | +## Configuration |
| 51 | + |
| 52 | +You can configure the number of retries and the delay (in seconds) between retries: |
| 53 | + |
| 54 | +```ruby |
| 55 | +class MyJob |
| 56 | + include Sidekiq::Job |
| 57 | + include Sidekiq::Rescue::DSL |
| 58 | + |
| 59 | + sidekiq_rescue ExpectedError, delay: 60, limit: 5 |
| 60 | + |
| 61 | + def perform(*) |
| 62 | + # ... |
| 63 | + end |
| 64 | +end |
| 65 | +``` |
| 66 | + |
| 67 | +The default values are: |
| 68 | +- `delay`: 60 seconds |
| 69 | +- `limit`: 5 retries |
| 70 | + |
| 71 | +Delay does not increase between retries (unlike Sidekiq standard retry mechanism). |
| 72 | + |
| 73 | +## Use cases |
| 74 | + |
| 75 | +Sidekiq::Rescue is useful when you want to retry jobs that failed due to expected errors and not spam your exception tracker with these errors. For example, you may want to retry a job that failed due to a network error or a temporary outage of a third party service, rather than a bug in your code. |
| 76 | + |
| 77 | +## Motivation |
| 78 | + |
| 79 | +Sidekiq provides a retry mechanism for jobs that failed due to unexpected errors. However, it does not provide a way to retry jobs that failed due to expected errors. This gem aims to fill this gap. |
| 80 | +In addition, it provides a way to configure the number of retries and the delay between retries independently from the Sidekiq standard retry mechanism. |
| 81 | + |
| 82 | +## Supported Ruby versions |
| 83 | + |
| 84 | +This gem supports Ruby 2.7+ |
| 85 | + |
| 86 | +If something doesn't work on one of these versions, it's a bug |
| 87 | + |
| 88 | +## Supported Sidekiq versions |
| 89 | + |
| 90 | +This gem supports Sidekiq 6.5+. It may work with older versions, but it's not tested. |
| 91 | + |
| 92 | +If you need support for older versions, please open an issue |
| 93 | + |
| 94 | +## Development |
| 95 | + |
| 96 | +To install dependencies and run tests: |
| 97 | +```bash |
| 98 | +make init |
| 99 | +make test |
| 100 | +make lint |
| 101 | +``` |
| 102 | + |
| 103 | +## Contributing |
| 104 | + |
| 105 | +Bug reports and pull requests are welcome on GitHub at https://github.com/moofkit/sidekiq_rescue. |
| 106 | + |
| 107 | +## License |
| 108 | + |
| 109 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
0 commit comments