Skip to content

Commit f708bed

Browse files
committed
init repo
0 parents  commit f708bed

26 files changed

+566
-0
lines changed

.github/workflows/main.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Ruby
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
name: Ruby ${{ matrix.ruby }}
14+
strategy:
15+
matrix:
16+
ruby:
17+
- '2.7'
18+
- '3.0'
19+
- '3.1'
20+
- '3.2'
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
- name: Set up Ruby
25+
uses: ruby/setup-ruby@v1
26+
with:
27+
ruby-version: ${{ matrix.ruby }}
28+
bundler-cache: true
29+
- name: Install dependencies
30+
run: make init
31+
- name: Run tests
32+
run: make test
33+
- name: Run lints
34+
run: make lint

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
Gemfile.lock
10+
11+
# rspec failure tracking
12+
.rspec_status
13+
14+
# Appraisals
15+
/gemfiles

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.rubocop.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require:
2+
- rubocop-rake
3+
4+
AllCops:
5+
TargetRubyVersion: 2.7
6+
NewCops: enable
7+
8+
Style/StringLiterals:
9+
Enabled: true
10+
EnforcedStyle: double_quotes
11+
12+
Style/StringLiteralsInInterpolation:
13+
Enabled: true
14+
EnforcedStyle: double_quotes
15+
16+
Layout/LineLength:
17+
Max: 120
18+
19+
Naming/FileName:
20+
Exclude: ['lib/sidekiq-rescue.rb']
21+
22+
Style/FrozenStringLiteralComment:
23+
Exclude:
24+
- 'gemfiles/**/*'

Appraisals

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
appraise "sidekiq-6.5.x" do
4+
group :test do
5+
gem "sidekiq", "~> 6.5.0"
6+
end
7+
end
8+
9+
appraise "sidekiq-7.0.x" do
10+
group :test do
11+
gem "sidekiq", "~> 7.0.0"
12+
end
13+
end
14+
15+
appraise "sidekiq-7.1.x" do
16+
group :test do
17+
gem "sidekiq", "~> 7.1.0"
18+
end
19+
end
20+
21+
appraise "sidekiq-7.2.x" do
22+
group :test do
23+
gem "sidekiq", "~> 7.2.0"
24+
end
25+
end

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [Unreleased]
2+
3+
## [0.1.0] - 2023-12-27
4+
5+
- Initial release

Gemfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
# Specify your gem's dependencies in sidekiq_rescue.gemspec
6+
gemspec
7+
8+
gem "appraisal"
9+
gem "rake", "~> 13.0"
10+
11+
gem "rspec", "~> 3.0"
12+
13+
gem "rubocop", "~> 1.21"
14+
gem "rubocop-rake"
15+
gem "rubocop-rspec"

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Dmitrii Ivliev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
PHONY: test lint
2+
3+
init:
4+
bundle install
5+
bundle exec appraisal generate
6+
bundle exec appraisal install
7+
8+
test:
9+
bundle exec appraisal rake spec
10+
11+
lint:
12+
bundle exec rubocop

README.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Sidekiq::Rescue
2+
3+
[![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)
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).

Rakefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require "bundler/gem_tasks"
4+
require "rspec/core/rake_task"
5+
6+
RSpec::Core::RakeTask.new(:spec)
7+
8+
require "rubocop/rake_task"
9+
10+
RuboCop::RakeTask.new
11+
12+
task default: %i[spec rubocop]

bin/console

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "sidekiq_rescue"
6+
7+
# You can add fixtures and/or initialization code here to make experimenting
8+
# with your gem easier. You can also use a different console, if you like.
9+
10+
require "irb"
11+
IRB.start(__FILE__)

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

lib/sidekiq-rescue.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
require "sidekiq_rescue"

lib/sidekiq/rescue.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
module Sidekiq
4+
# Sidekiq::Rescue is a Sidekiq plugin which allows you to easily handle
5+
# exceptions thrown by your jobs.
6+
#
7+
# To use Sidekiq::Rescue, you need to include Sidekiq::Rescue::DSL module
8+
# in your job class and use the sidekiq_rescue class method to define
9+
# exception handlers.
10+
#
11+
# class MyJob
12+
# include Sidekiq::Job
13+
# include Sidekiq::Rescue::DSL
14+
#
15+
# sidekiq_rescue NetworkError, delay: 60, limit: 10
16+
#
17+
# def perform
18+
# # ...
19+
# end
20+
# end
21+
#
22+
# Also it needs to be registered in Sidekiq server middleware chain:
23+
# Sidekiq.configure_server do |config|
24+
# config.server_middleware do |chain|
25+
# chain.add Sidekiq::Rescue::ServerMiddleware
26+
# ...
27+
module Rescue
28+
DEFAULT_DELAY = 60
29+
DEFAULT_LIMIT = 10
30+
31+
class << self
32+
attr_writer :logger
33+
34+
# Returns the logger instance. If no logger is set, it defaults to Sidekiq.logger.
35+
#
36+
# @return [Logger] The logger instance.
37+
def logger
38+
@logger ||= Sidekiq.logger
39+
end
40+
end
41+
end
42+
end

lib/sidekiq/rescue/dsl.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module Sidekiq
4+
module Rescue
5+
# This module is included into the job class to provide the DSL for
6+
# configuring rescue options.
7+
module DSL
8+
def self.included(base)
9+
base.extend(ClassMethods)
10+
base.sidekiq_class_attribute(:sidekiq_rescue_options)
11+
end
12+
13+
# Module containing the DSL methods
14+
module ClassMethods
15+
# Configure rescue options for the job.
16+
# @param error [StandardError] The error class to rescue.
17+
# @param delay [Integer] The delay in seconds before retrying the job.
18+
# @param limit [Integer] The maximum number of retries.
19+
# @return [void]
20+
# @raise [ArgumentError] if error is not a StandardError
21+
# @example
22+
# sidekiq_rescue NetworkError, delay: 60, limit: 10
23+
def sidekiq_rescue(error, **options)
24+
raise ArgumentError, "error must be an ancestor of StandardError" unless error < StandardError
25+
raise ArgumentError, "delay must be integer" if options[:delay] && !options[:delay].is_a?(Integer)
26+
raise ArgumentError, "limit must be integer" if options[:limit] && !options[:limit].is_a?(Integer)
27+
28+
self.sidekiq_rescue_options = {
29+
error: error,
30+
delay: Sidekiq::Rescue::DEFAULT_DELAY,
31+
limit: Sidekiq::Rescue::DEFAULT_LIMIT
32+
}.merge(options)
33+
end
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)