Skip to content

Commit

Permalink
Add sidekiq group (#1)
Browse files Browse the repository at this point in the history
* add gem files

* add github actions

* change gem description

* add github actions

* add publish workflow

* update readme

* fix rubocop
  • Loading branch information
vikabelous committed Mar 18, 2021
1 parent cb30825 commit 0500a98
Show file tree
Hide file tree
Showing 22 changed files with 750 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: publish gem

on:
release:
types: [ published ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v1

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6.2
bundler: Gemfile.lock

- name: Release Gem
if: contains(github.ref, 'refs/tags/v')
uses: cadwallion/publish-rubygems-action@master
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
RELEASE_COMMAND: rake release

62 changes: 62 additions & 0 deletions .github/workflows/quality_gates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: quality gates

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
rubocop:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.4.0
bundler: Gemfile.lock
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Install dependencies
run: |
bundle config path ~/.bundle
bundle install --jobs 4 --retry 3
- name: Run rubocop
run: bundle exec rubocop --parallel

rspec:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.4.0
bundler: Gemfile.lock
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Install dependencies
run: |
bundle config path ~/.bundle
bundle install --jobs 4 --retry 3
- name: Run rspec
run: bundle exec rspec
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.idea
.ruby-gemset
.ruby-version
.vscode

/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
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
21 changes: 21 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require:
- rubocop-rake
- rubocop-rspec
- rubocop-performance

AllCops:
Exclude:
- "bin/**/*"
- "vendor/**/*"
NewCops: enable
TargetRubyVersion: 2.4

Metrics/BlockLength:
Exclude:
- spec/**/*_spec.rb

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source 'http://rubygems.org'

gemspec

group :test, :development do
gem 'fakeredis'
gem 'rspec'
gem 'rubocop'
gem 'rubocop-performance'
gem 'rubocop-rake'
gem 'rubocop-rspec'
end
94 changes: 94 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
PATH
remote: .
specs:
sidekiq-group (0.1.0)
activesupport (>= 5.0)
sidekiq (>= 5.1, < 6)

GEM
remote: http://rubygems.org/
specs:
activesupport (5.2.4.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
ast (2.4.1)
concurrent-ruby (1.1.7)
connection_pool (2.2.3)
diff-lcs (1.4.4)
fakeredis (0.8.0)
redis (~> 4.1)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
minitest (5.14.3)
parallel (1.20.1)
parser (3.0.0.0)
ast (~> 2.4.1)
rack (2.0.9)
rack-protection (2.1.0)
rack
rainbow (3.0.0)
rake (13.0.3)
redis (4.2.5)
regexp_parser (2.0.3)
rexml (3.2.4)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.1)
rubocop (1.8.1)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.2.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.4.0)
parser (>= 2.7.1.5)
rubocop-performance (1.9.2)
rubocop (>= 0.90.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rake (0.5.1)
rubocop
rubocop-rspec (2.1.0)
rubocop (~> 1.0)
rubocop-ast (>= 1.1.0)
ruby-progressbar (1.11.0)
sidekiq (5.2.8)
connection_pool (~> 2.2, >= 2.2.2)
rack (< 2.1.0)
rack-protection (>= 1.5.0)
redis (>= 3.3.5, < 5)
thread_safe (0.3.6)
tzinfo (1.2.7)
thread_safe (~> 0.1)
unicode-display_width (2.0.0)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.0)
fakeredis
rake (~> 13.0)
rspec
rubocop
rubocop-performance
rubocop-rake
rubocop-rspec
sidekiq-group!

BUNDLED WITH
2.1.4
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) 2021 Viktoriia Bilous

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.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
### Sidekiq Group
# Sidekiq::Group
Addon for Sidekiq that provides similar functionality to Pro version Batches feature. Allows to group jobs into a set and follow their progress

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'sidekiq-group'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install sidekiq-group

## Usage

```ruby
class ImportBatchWorker
include Sidekiq::Worker
include Sidekiq::Group

def on_complete(options)
Import.find(options['import_id']).done!
end

def perform(import_id)
import = Report.find(import_id)

sidekiq_group(import_id: import.id) do |group|
import.rows.each do |import_row|
group.add(ImportWorker.perform_async(import_row.id))
end
end
end
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sidekiq-group.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'sidekiq/group'

# 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.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

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
34 changes: 34 additions & 0 deletions lib/sidekiq/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'sidekiq/logging'
require 'sidekiq/group/version'
require 'sidekiq/group/collection'
require 'sidekiq/group/middleware'

module Sidekiq
module Group
class NoBlockGivenError < StandardError; end

def sidekiq_group(options = {})
raise NoBlockGivenError unless block_given?

group = Sidekiq::Group::Collection.new
group.callback_class = self.class.name
group.callback_options = options

Thread.current[:group_collection] = group

yield(group)

group.spawned_jobs!

Thread.current[:group_collection] = nil
end

def on_complete(_options = {})
sidekiq_logger.warn 'on_complete function is not defined'
end

def sidekiq_logger
Sidekiq::Logging.logger
end
end
end

0 comments on commit 0500a98

Please sign in to comment.