-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 20d2a83
Showing
26 changed files
with
589 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
pull_request: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
name: Ruby ${{ matrix.ruby }} | ||
services: | ||
redis: | ||
image: redis | ||
ports: | ||
- 6379:6379 | ||
options: >- | ||
--health-cmd "redis-cli ping" | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
strategy: | ||
matrix: | ||
ruby: | ||
- '2.7' | ||
- '3.0' | ||
- '3.1' | ||
- '3.2' | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ matrix.ruby }} | ||
bundler-cache: true | ||
- name: Install dependencies | ||
run: make init | ||
- name: Run tests | ||
run: make test | ||
- name: Run lints | ||
run: make lint | ||
results: | ||
if: ${{ always() }} | ||
runs-on: ubuntu-latest | ||
name: Final Results | ||
needs: [test] | ||
steps: | ||
- run: exit 1 | ||
# see https://stackoverflow.com/a/67532120/4907315 | ||
if: >- | ||
${{ | ||
contains(needs.*.result, 'failure') | ||
|| contains(needs.*.result, 'cancelled') | ||
}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
Gemfile.lock | ||
|
||
# rspec failure tracking | ||
.rspec_status | ||
|
||
# Appraisals | ||
/gemfiles |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--format documentation | ||
--color | ||
--require spec_helper |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require: | ||
- rubocop-rake | ||
|
||
AllCops: | ||
TargetRubyVersion: 2.7 | ||
NewCops: enable | ||
|
||
Style/StringLiterals: | ||
Enabled: true | ||
EnforcedStyle: double_quotes | ||
|
||
Style/StringLiteralsInInterpolation: | ||
Enabled: true | ||
EnforcedStyle: double_quotes | ||
|
||
Layout/LineLength: | ||
Max: 120 | ||
|
||
Naming/FileName: | ||
Exclude: ['lib/sidekiq-rescue.rb'] | ||
|
||
Style/FrozenStringLiteralComment: | ||
Exclude: | ||
- 'gemfiles/**/*' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
appraise "sidekiq-6.5.x" do | ||
group :test do | ||
gem "sidekiq", "~> 6.5.0" | ||
end | ||
end | ||
|
||
appraise "sidekiq-7.0.x" do | ||
group :test do | ||
gem "sidekiq", "~> 7.0.0" | ||
end | ||
end | ||
|
||
appraise "sidekiq-7.1.x" do | ||
group :test do | ||
gem "sidekiq", "~> 7.1.0" | ||
end | ||
end | ||
|
||
appraise "sidekiq-7.2.x" do | ||
group :test do | ||
gem "sidekiq", "~> 7.2.0" | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## [Unreleased] | ||
|
||
## [0.1.0] - 2023-12-27 | ||
|
||
- Initial release |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# Specify your gem's dependencies in sidekiq_rescue.gemspec | ||
gemspec | ||
|
||
gem "appraisal" | ||
gem "rake", "~> 13.0" | ||
|
||
gem "rspec", "~> 3.0" | ||
|
||
gem "rubocop", "~> 1.21" | ||
gem "rubocop-rake" | ||
gem "rubocop-rspec" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023 Dmitrii Ivliev | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
PHONY: test lint | ||
|
||
init: | ||
bundle install | ||
bundle exec appraisal generate | ||
bundle exec appraisal install | ||
|
||
test: | ||
bundle exec appraisal rake spec | ||
|
||
lint: | ||
bundle exec rubocop |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Sidekiq::Rescue | ||
|
||
[![Build Status](https://github.com/moofkit/sidekiq-rescue/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/moofkit/sidekiq-rescue/actions/workflows/main.yml) | ||
|
||
[Sidekiq](https://github.com/sidekiq/sidekiq) plugin to rescue jobs from expected errors and retry them later. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem "sidekiq-rescue" | ||
``` | ||
|
||
And then execute: | ||
|
||
$ bundle install | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install sidekiq-rescue | ||
|
||
## Usage | ||
|
||
1. Add the middleware to your Sidekiq configuration: | ||
|
||
```ruby | ||
Sidekiq.configure_server do |config| | ||
config.server_middleware do |chain| | ||
chain.add Sidekiq::Rescue::Middleware | ||
end | ||
end | ||
``` | ||
|
||
2. Add DSL to your job: | ||
|
||
```ruby | ||
class MyJob | ||
include Sidekiq::Job | ||
include Sidekiq::Rescue::DSL | ||
|
||
sidekiq_rescue ExpectedError | ||
|
||
def perform(*) | ||
# ... | ||
end | ||
end | ||
``` | ||
|
||
## Configuration | ||
|
||
You can configure the number of retries and the delay (in seconds) between retries: | ||
|
||
```ruby | ||
class MyJob | ||
include Sidekiq::Job | ||
include Sidekiq::Rescue::DSL | ||
|
||
sidekiq_rescue ExpectedError, delay: 60, limit: 5 | ||
|
||
def perform(*) | ||
# ... | ||
end | ||
end | ||
``` | ||
|
||
The default values are: | ||
- `delay`: 60 seconds | ||
- `limit`: 5 retries | ||
|
||
Delay does not increase between retries (unlike Sidekiq standard retry mechanism). | ||
|
||
## Use cases | ||
|
||
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. | ||
|
||
## Motivation | ||
|
||
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. | ||
In addition, it provides a way to configure the number of retries and the delay between retries independently from the Sidekiq standard retry mechanism. | ||
|
||
## Supported Ruby versions | ||
|
||
This gem supports Ruby 2.7+ | ||
|
||
If something doesn't work on one of these versions, it's a bug | ||
|
||
## Supported Sidekiq versions | ||
|
||
This gem supports Sidekiq 6.5+. It may work with older versions, but it's not tested. | ||
|
||
If you need support for older versions, please open an issue | ||
|
||
## Development | ||
|
||
To install dependencies and run tests: | ||
```bash | ||
make init | ||
make test | ||
make lint | ||
``` | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/moofkit/sidekiq_rescue. | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
require "rubocop/rake_task" | ||
|
||
RuboCop::RakeTask.new | ||
|
||
task default: %i[spec rubocop] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "sidekiq_rescue" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
require "irb" | ||
IRB.start(__FILE__) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require "sidekiq_rescue" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
# Sidekiq::Rescue is a Sidekiq plugin which allows you to easily handle | ||
# exceptions thrown by your jobs. | ||
# | ||
# To use Sidekiq::Rescue, you need to include Sidekiq::Rescue::DSL module | ||
# in your job class and use the sidekiq_rescue class method to define | ||
# exception handlers. | ||
# | ||
# class MyJob | ||
# include Sidekiq::Job | ||
# include Sidekiq::Rescue::DSL | ||
# | ||
# sidekiq_rescue NetworkError, delay: 60, limit: 10 | ||
# | ||
# def perform | ||
# # ... | ||
# end | ||
# end | ||
# | ||
# Also it needs to be registered in Sidekiq server middleware chain: | ||
# Sidekiq.configure_server do |config| | ||
# config.server_middleware do |chain| | ||
# chain.add Sidekiq::Rescue::ServerMiddleware | ||
# ... | ||
module Rescue | ||
DEFAULT_DELAY = 60 | ||
DEFAULT_LIMIT = 10 | ||
|
||
class << self | ||
attr_writer :logger | ||
|
||
# Returns the logger instance. If no logger is set, it defaults to Sidekiq.logger. | ||
# | ||
# @return [Logger] The logger instance. | ||
def logger | ||
@logger ||= Sidekiq.logger | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.