Skip to content

Commit

Permalink
init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
moofkit committed Jan 2, 2024
0 parents commit d7df878
Show file tree
Hide file tree
Showing 26 changed files with 566 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
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 install
- name: Run tests
run: make test
- name: Run lints
run: make lint
15 changes: 15 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
24 changes: 24 additions & 0 deletions .rubocop.yml
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/**/*'
25 changes: 25 additions & 0 deletions Appraisals
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2023-12-27

- Initial release
15 changes: 15 additions & 0 deletions Gemfile
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"
21 changes: 21 additions & 0 deletions LICENSE.txt
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.
12 changes: 12 additions & 0 deletions Makefile
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
109 changes: 109 additions & 0 deletions README.md
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).
12 changes: 12 additions & 0 deletions Rakefile
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]
11 changes: 11 additions & 0 deletions bin/console
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__)
8 changes: 8 additions & 0 deletions bin/setup
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
3 changes: 3 additions & 0 deletions lib/sidekiq-rescue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require "sidekiq_rescue"
42 changes: 42 additions & 0 deletions lib/sidekiq/rescue.rb
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
37 changes: 37 additions & 0 deletions lib/sidekiq/rescue/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Sidekiq
module Rescue
# This module is included into the job class to provide the DSL for
# configuring rescue options.
module DSL
def self.included(base)
base.extend(ClassMethods)
base.sidekiq_class_attribute(:sidekiq_rescue_options)
end

# Module containing the DSL methods
module ClassMethods
# Configure rescue options for the job.
# @param error [StandardError] The error class to rescue.
# @param delay [Integer] The delay in seconds before retrying the job.
# @param limit [Integer] The maximum number of retries.
# @return [void]
# @raise [ArgumentError] if error is not a StandardError
# @example
# sidekiq_rescue NetworkError, delay: 60, limit: 10
def sidekiq_rescue(error, **options)
raise ArgumentError, "error must be an ancestor of StandardError" unless error < StandardError
raise ArgumentError, "delay must be integer" if options[:delay] && !options[:delay].is_a?(Integer)
raise ArgumentError, "limit must be integer" if options[:limit] && !options[:limit].is_a?(Integer)

self.sidekiq_rescue_options = {
error: error,
delay: Sidekiq::Rescue::DEFAULT_DELAY,
limit: Sidekiq::Rescue::DEFAULT_LIMIT
}.merge(options)
end
end
end
end
end
Loading

0 comments on commit d7df878

Please sign in to comment.