sidekiq-bunch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2235cb2837561e33624cf5879327982209465a1e94bf8c527893213824f25f26
4
+ data.tar.gz: 8c4c93d14c8efc4b9d5419a20f77145a913eab17c69263e6ef36cd7e52855a48
5
+ SHA512:
6
+ metadata.gz: a5f6af39be6601ffa47d1246c5693a93c52c8955a70878cd8e94474a137cb6bfdb4d046db51e2aba4816288d79d01e631fec64ebea6411967baad9c6c2b8a2af
7
+ data.tar.gz: f03cea75ecd875b1fe3cac27f4d58ea843ffa3cb6475514ecc5e5335ee8f1ad2835901987525e67332d2e7d87cb294be55f175f7888fed5311e0cc7597a72454
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /gemfiles/*.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,29 @@
1
+ language: ruby
2
+ sudo: false
3
+ services:
4
+ - redis-server
5
+ rvm:
6
+ - 2.1
7
+ - 2.2
8
+ - 2.3
9
+ - 2.4
10
+ - 2.5
11
+ gemfile:
12
+ - gemfiles/sidekiq_4.0.gemfile
13
+ - gemfiles/sidekiq_4.1.gemfile
14
+ - gemfiles/sidekiq_4.2.gemfile
15
+ - gemfiles/sidekiq_5.0.gemfile
16
+ - gemfiles/sidekiq_5.1.gemfile
17
+ - gemfiles/sidekiq_master.gemfile
18
+ matrix:
19
+ fast_finish: true
20
+ exclude:
21
+ - gemfile: gemfiles/sidekiq_5.1.gemfile
22
+ rvm: 2.1
23
+ - gemfile: gemfiles/sidekiq_5.0.gemfile
24
+ rvm: 2.1
25
+ - gemfile: gemfiles/sidekiq_master.gemfile
26
+
27
+ before_install: gem install bundler -v 1.15
28
+ script:
29
+ - bundle exec rake spec
@@ -0,0 +1,23 @@
1
+ appraise 'sidekiq-4.0' do
2
+ gem 'sidekiq', '~> 4.0.0'
3
+ end
4
+
5
+ appraise 'sidekiq-4.1' do
6
+ gem 'sidekiq', '~> 4.1.0'
7
+ end
8
+
9
+ appraise 'sidekiq-4.2' do
10
+ gem 'sidekiq', '~> 4.2.0'
11
+ end
12
+
13
+ appraise 'sidekiq-5.0' do
14
+ gem 'sidekiq', '~> 5.0.0'
15
+ end
16
+
17
+ appraise 'sidekiq-5.1' do
18
+ gem 'sidekiq', '~> 5.1.0'
19
+ end
20
+
21
+ appraise 'sidekiq-master' do
22
+ gem 'sidekiq', github: 'mperham/sidekiq'
23
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sidekiq-bunch.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Vladimir Kochnev
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.
@@ -0,0 +1,51 @@
1
+ [![Build Status](https://travis-ci.org/marshall-lee/sidekiq-bunch.svg?branch=master)](https://travis-ci.org/marshall-lee/sidekiq-bunch)
2
+
3
+ # sidekiq-bunch
4
+
5
+ Lightweight Sidekiq job bunches implemented with Redis set data structure.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sidekiq-bunch'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sidekiq-bunch
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ bunch = Sidekiq::Bunch.new
27
+ bunch.hook_jobs do
28
+ 1000.times do
29
+ MyWorker.perform_async
30
+ end
31
+ end
32
+
33
+ bunch.size # => 1000
34
+ bunch.empty? # => false
35
+ bunch.wait_for_empty # ...
36
+ bunch.empty? # => true
37
+ ```
38
+
39
+ ## Development
40
+
41
+ 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.
42
+
43
+ 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marshall-lee/sidekiq-bunch.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sidekiq/bunch"
5
+
6
+ require "pry"
7
+ Pry.start
@@ -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
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sidekiq", "~> 4.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sidekiq", "~> 4.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sidekiq", "~> 4.2.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sidekiq", "~> 5.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sidekiq", "~> 5.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sidekiq", git: "https://github.com/mperham/sidekiq"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sidekiq'
4
+ require 'sidekiq/postpone'
5
+
6
+ class Sidekiq::Bunch
7
+ require 'sidekiq/bunch/version'
8
+ require 'sidekiq/bunch/middleware'
9
+ require 'sidekiq/bunch/worker'
10
+
11
+ TimeoutError = Class.new(StandardError)
12
+
13
+ def self.generate_bunch_id
14
+ SecureRandom.hex(12)
15
+ end
16
+
17
+ def initialize(bunch_id = self.class.generate_bunch_id)
18
+ @bunch_id = bunch_id
19
+ @jids_key = build_jids_key
20
+ end
21
+
22
+ def hook(ignore: nil)
23
+ Thread.current[:sidekiq_bunch] = self
24
+ Sidekiq::Postpone.wrap(join_parent: false, flush: false) do |postpone|
25
+ yield
26
+ jids =
27
+ if ignore
28
+ postpone.all_jobs.reject(&ignore).map { |j| j['jid'] }
29
+ else
30
+ postpone.jids
31
+ end
32
+ multi do
33
+ add(jids)
34
+ postpone.flush!
35
+ end
36
+ end
37
+ ensure
38
+ Thread.current[:sidekiq_bunch] = nil
39
+ end
40
+
41
+ def on_success(job)
42
+ rem(job['jid'])
43
+ end
44
+
45
+ def add(jids)
46
+ jids = Array(jids)
47
+
48
+ redis do |r|
49
+ r.sadd(@jids_key, jids)
50
+ end
51
+ end
52
+
53
+ def rem(jids)
54
+ jids = Array(jids)
55
+
56
+ redis do |r|
57
+ r.srem(@jids_key, jids)
58
+ end
59
+ end
60
+
61
+ def size
62
+ redis { |r| r.scard(@jids_key) }
63
+ end
64
+
65
+ def empty?
66
+ 0 == size
67
+ end
68
+
69
+ def wait_for_empty(poll_interval: 1.0, timeout: nil)
70
+ start_time = Time.now if timeout
71
+ loop do
72
+ break if empty?
73
+ raise TimeoutError if timeout && (Time.now - start_time) >= timeout
74
+ sleep(poll_interval)
75
+ end
76
+ end
77
+
78
+ attr_reader :bunch_id
79
+
80
+ private
81
+
82
+ def build_jids_key
83
+ "bunches:#{@bunch_id}-jids"
84
+ end
85
+
86
+ def redis
87
+ Sidekiq.redis { |r| yield r }
88
+ end
89
+
90
+ def multi
91
+ redis { |r| r.multi { |m| yield m } }
92
+ end
93
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidekiq::Bunch::Middleware
4
+ class Client
5
+ def call(_worker_class, msg, _queue, _redis_pool)
6
+ bunch = Thread.current[:sidekiq_bunch]
7
+ msg['bunch_id'] = bunch.bunch_id if bunch
8
+ yield
9
+ end
10
+ end
11
+
12
+ class Server
13
+ def call(worker, msg, _queue)
14
+ bunch_id = msg['bunch_id']
15
+ if bunch_id
16
+ worker.bunch_id = bunch_id
17
+ result = yield
18
+ Sidekiq::Bunch.new(bunch_id).on_success(msg)
19
+ result
20
+ else
21
+ yield
22
+ end
23
+ end
24
+ end
25
+
26
+ Sidekiq.configure_client do |config|
27
+ config.client_middleware do |chain|
28
+ chain.add Client
29
+ end
30
+ end
31
+
32
+ Sidekiq.configure_server do |config|
33
+ config.client_middleware do |chain|
34
+ chain.add Client
35
+ end
36
+
37
+ config.server_middleware do |chain|
38
+ chain.add Server
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ class Bunch
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sidekiq/worker'
4
+
5
+ class Sidekiq::Bunch
6
+ module Worker
7
+ attr_accessor :bunch_id
8
+
9
+ def bunch
10
+ @bunch ||= Sidekiq::Bunch.new(bunch_id) if bunch_id
11
+ end
12
+ end
13
+
14
+ Sidekiq::Worker.prepend Worker
15
+ end
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/bunch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sidekiq-bunch'
8
+ spec.version = Sidekiq::Bunch::VERSION
9
+ spec.authors = ['Vladimir Kochnev']
10
+ spec.email = ['hashtable@yandex.ru']
11
+
12
+ spec.summary = %q{Lightweight implementation of job bunches.}
13
+ spec.homepage = 'https://github.com/marshall-lee/sidekiq-bunch'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'sidekiq', '>= 3'
24
+ spec.add_dependency 'sidekiq-postpone', '>= 0.3.1'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 12.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'appraisal', '~> 2.2.0'
30
+ spec.add_development_dependency 'redis-namespace', '~> 1.5.2'
31
+
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'pry-byebug'
34
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-bunch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Kochnev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq-postpone
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.2.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis-namespace
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.5.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.5.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - hashtable@yandex.ru
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - Appraisals
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - gemfiles/sidekiq_4.0.gemfile
157
+ - gemfiles/sidekiq_4.1.gemfile
158
+ - gemfiles/sidekiq_4.2.gemfile
159
+ - gemfiles/sidekiq_5.0.gemfile
160
+ - gemfiles/sidekiq_5.1.gemfile
161
+ - gemfiles/sidekiq_master.gemfile
162
+ - lib/sidekiq/bunch.rb
163
+ - lib/sidekiq/bunch/middleware.rb
164
+ - lib/sidekiq/bunch/version.rb
165
+ - lib/sidekiq/bunch/worker.rb
166
+ - sidekiq-bunch.gemspec
167
+ homepage: https://github.com/marshall-lee/sidekiq-bunch
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.7.6
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Lightweight implementation of job bunches.
191
+ test_files: []