sidekiq-batchs 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 011c76443fe320ee58137aff6159d3cf4010818da75bb8c7bf1c81d5d26d4493
4
+ data.tar.gz: 5105e3e07c1cfa271cf308f8b7d9decae96985b628535f24a08295a8981e68c2
5
+ SHA512:
6
+ metadata.gz: 7c76f4b95624e17a840f2e9863bb24ddccd7fe1bb68b1916fef40261c3fd5c572fb3731aba5e2cc894af93d061e75e13bf25717b3d7cd7d72a1bf96e7e2ca6fe
7
+ data.tar.gz: fb82c8d808d165684639fd5307135c3505ff706b00438a262e1fd0ba85ee79b52d64093705ea8700bcb73a05a3a5214e312ba6f6fac6ccdab61cd53d9b71cc47
@@ -0,0 +1,3 @@
1
+ # These are supported funding model platforms
2
+ github: breamware
3
+ open_collective: sidekiq-batch
@@ -0,0 +1,8 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ time: "04:00"
8
+ open-pull-requests-limit: 10
@@ -0,0 +1,37 @@
1
+ name: CI
2
+
3
+ on: [pull_request, workflow_dispatch]
4
+
5
+ jobs:
6
+ test:
7
+ env:
8
+ REDIS_HOST: 'redis'
9
+
10
+ runs-on: ubuntu-latest
11
+ services:
12
+ redis:
13
+ image: redis
14
+ # Set health checks to wait until redis has started
15
+ options: >-
16
+ --health-cmd "redis-cli ping"
17
+ --health-interval 10s
18
+ --health-timeout 5s
19
+ --health-retries 5
20
+ ports:
21
+ # Maps port 6379 on service container to the host
22
+ - 6379:6379
23
+
24
+ strategy:
25
+ fail-fast: false
26
+ matrix:
27
+ ruby: ["3.0", "3.1", "3.2", ruby-head]
28
+
29
+ steps:
30
+ - uses: actions/checkout@v2
31
+ - name: Set up Ruby
32
+ uses: ruby/setup-ruby@v1
33
+ with:
34
+ bundler-cache: true # 'bundle install' and cache gems
35
+ ruby-version: ${{ matrix.ruby }}
36
+ - name: Run tests
37
+ run: bundle exec rake
@@ -0,0 +1,70 @@
1
+ # For most projects, this workflow file will not need changing; you simply need
2
+ # to commit it to your repository.
3
+ #
4
+ # You may wish to alter this file to override the set of languages analyzed,
5
+ # or to provide custom queries or build logic.
6
+ #
7
+ # ******** NOTE ********
8
+ # We have attempted to detect the languages in your repository. Please check
9
+ # the `language` matrix defined below to confirm you have the correct set of
10
+ # supported CodeQL languages.
11
+ #
12
+ name: "CodeQL"
13
+
14
+ on:
15
+ push:
16
+ branches: [ master ]
17
+ pull_request:
18
+ # The branches below must be a subset of the branches above
19
+ branches: [ master ]
20
+ schedule:
21
+ - cron: '24 3 * * 2'
22
+
23
+ jobs:
24
+ analyze:
25
+ name: Analyze
26
+ runs-on: ubuntu-latest
27
+ permissions:
28
+ actions: read
29
+ contents: read
30
+ security-events: write
31
+
32
+ strategy:
33
+ fail-fast: false
34
+ matrix:
35
+ language: [ 'ruby' ]
36
+ # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37
+ # Learn more about CodeQL language support at https://git.io/codeql-language-support
38
+
39
+ steps:
40
+ - name: Checkout repository
41
+ uses: actions/checkout@v3
42
+
43
+ # Initializes the CodeQL tools for scanning.
44
+ - name: Initialize CodeQL
45
+ uses: github/codeql-action/init@v2
46
+ with:
47
+ languages: ${{ matrix.language }}
48
+ # If you wish to specify custom queries, you can do so here or in a config file.
49
+ # By default, queries listed here will override any specified in a config file.
50
+ # Prefix the list here with "+" to use these queries and those in the config file.
51
+ # queries: ./path/to/local/query, your-org/your-repo/queries@main
52
+
53
+ # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54
+ # If this step fails, then you should remove it and run the build manually (see below)
55
+ - name: Autobuild
56
+ uses: github/codeql-action/autobuild@v2
57
+
58
+ # ℹ️ Command-line programs to run using the OS shell.
59
+ # 📚 https://git.io/JvXDl
60
+
61
+ # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62
+ # and modify them (or add more) to build your code if your project
63
+ # uses a compiled language
64
+
65
+ #- run: |
66
+ # make bootstrap
67
+ # make release
68
+
69
+ - name: Perform CodeQL Analysis
70
+ uses: github/codeql-action/analyze@v2
@@ -0,0 +1,19 @@
1
+ name: Mark stale issues and pull requests
2
+
3
+ on:
4
+ schedule:
5
+ - cron: "0 0 * * *"
6
+
7
+ jobs:
8
+ stale:
9
+
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/stale@v1
14
+ with:
15
+ repo-token: ${{ secrets.GITHUB_TOKEN }}
16
+ stale-issue-message: 'Stale issue message'
17
+ stale-pr-message: 'Stale pull request message'
18
+ stale-issue-label: 'no-issue-activity'
19
+ stale-pr-label: 'no-pr-activity'
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ dump.rdb
11
+ sidekiq-batch-*.gem
12
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
6
+ after_success:
7
+ - bundle exec codeclimate-test-reporter
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "simplecov"
7
+ gem "codeclimate-test-reporter", "~> 1.0.0"
8
+ end
data/Guardfile ADDED
@@ -0,0 +1,4 @@
1
+ guard 'rspec', cmd: 'rspec --color' do
2
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
3
+ watch(%r|^spec/(.*)_spec\.rb|)
4
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Breamware
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.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ [gem]: https://rubygems.org/gems/sidekiq-batch
2
+ [travis]: https://travis-ci.org/breamware/sidekiq-batch
3
+ [codeclimate]: https://codeclimate.com/github/breamware/sidekiq-batch
4
+
5
+ # Sidekiq::Batch
6
+
7
+ [![Join the chat at https://gitter.im/breamware/sidekiq-batch](https://badges.gitter.im/breamware/sidekiq-batch.svg)](https://gitter.im/breamware/sidekiq-batch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
8
+
9
+ [![Gem Version](https://badge.fury.io/rb/sidekiq-batch.svg)][gem]
10
+ [![Build Status](https://travis-ci.org/breamware/sidekiq-batch.svg?branch=master)][travis]
11
+ [![Code Climate](https://codeclimate.com/github/breamware/sidekiq-batch/badges/gpa.svg)][codeclimate]
12
+ [![Code Climate](https://codeclimate.com/github/breamware/sidekiq-batch/badges/coverage.svg)][codeclimate]
13
+ [![Code Climate](https://codeclimate.com/github/breamware/sidekiq-batch/badges/issue_count.svg)][codeclimate]
14
+
15
+ Simple Sidekiq Batch Job implementation.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'sidekiq-batch'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install sidekiq-batch
32
+
33
+ ## Usage
34
+
35
+ Sidekiq Batch is MOSTLY a drop-in replacement for the API from Sidekiq PRO. See https://github.com/mperham/sidekiq/wiki/Batches for usage.
36
+
37
+ ## Caveats/Gotchas
38
+
39
+ Consider the following workflow:
40
+
41
+ * Batch Z created
42
+ * Worker A queued in batch Z
43
+ * Worker A starts Worker B in batch Z
44
+ * Worker B completes *before* worker A does
45
+ * Worker A completes
46
+
47
+ In the standard configuration, the `on(:success)` and `on(:complete)` callbacks will be triggered when Worker B completes.
48
+ This configuration is the default, simply for legacy reasons. This gem adds the following option to the sidekiq.yml options:
49
+
50
+ ```yaml
51
+ :batch_push_interval: 0
52
+ ```
53
+
54
+ When this value is *absent* (aka legacy), Worker A will only push the increment of batch jobs (aka Worker B) *when it completes*
55
+
56
+ When this value is set to `0`, Worker A will increment the count as soon as `WorkerB.perform_async` is called
57
+
58
+ When this value is a positive number, Worker A will wait a maximum of value-seconds before pushing the increment to redis, or until it's done, whichever comes first.
59
+
60
+ This comes into play if Worker A is queueing thousands of WorkerB jobs, or has some other reason for WorkerB to complete beforehand.
61
+
62
+ If you are queueing many WorkerB jobs, it is recommended to set this value to something like `3` to avoid thousands of calls to redis, and call WorkerB like so:
63
+ ```ruby
64
+ WorkerB.perform_in(4.seconds, some, args)
65
+ ```
66
+ this will ensure that the batch callback does not get triggered until WorkerA *and* the last WorkerB job complete.
67
+
68
+ If WorkerA is just slow for whatever reason, setting to `0` will update the batch status immediately so that the callbacks don't fire.
69
+
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/breamware/sidekiq-batch.
74
+
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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,104 @@
1
+ module Sidekiq
2
+ class Batch
3
+ module Callback
4
+ class Worker
5
+ include Sidekiq::Worker
6
+
7
+ def perform(clazz, event, opts, bid, parent_bid)
8
+ return unless %w(success complete).include?(event)
9
+ clazz, method = clazz.split("#") if (clazz && clazz.class == String && clazz.include?("#"))
10
+ method = "on_#{event}" if method.nil?
11
+ status = Sidekiq::Batch::Status.new(bid)
12
+
13
+ if clazz && object = Object.const_get(clazz)
14
+ instance = object.new
15
+ instance.send(method, status, opts) if instance.respond_to?(method)
16
+ end
17
+ end
18
+ end
19
+
20
+ class Finalize
21
+ def dispatch status, opts
22
+ bid = opts["bid"]
23
+ callback_bid = status.bid
24
+ event = opts["event"].to_sym
25
+ callback_batch = bid != callback_bid
26
+
27
+ Sidekiq.logger.debug {"Finalize #{event} batch id: #{opts["bid"]}, callback batch id: #{callback_bid} callback_batch #{callback_batch}"}
28
+
29
+ batch_status = Status.new bid
30
+ send(event, bid, batch_status, batch_status.parent_bid)
31
+
32
+
33
+ # Different events are run in different callback batches
34
+ Sidekiq::Batch.cleanup_redis callback_bid if callback_batch
35
+ Sidekiq::Batch.cleanup_redis bid if event == :success
36
+ end
37
+
38
+ def success(bid, status, parent_bid)
39
+ return unless parent_bid
40
+
41
+ _, _, success, _, complete, pending, children, failure = Sidekiq.redis do |r|
42
+ r.multi do |pipeline|
43
+ pipeline.sadd("BID-#{parent_bid}-success", [bid])
44
+ pipeline.expire("BID-#{parent_bid}-success", Sidekiq::Batch::BID_EXPIRE_TTL)
45
+ pipeline.scard("BID-#{parent_bid}-success")
46
+ pipeline.sadd("BID-#{parent_bid}-complete", [bid])
47
+ pipeline.scard("BID-#{parent_bid}-complete")
48
+ pipeline.hincrby("BID-#{parent_bid}", "pending", 0)
49
+ pipeline.hincrby("BID-#{parent_bid}", "children", 0)
50
+ pipeline.scard("BID-#{parent_bid}-failed")
51
+ end
52
+ end
53
+ # if job finished successfully and parent batch completed call parent complete callback
54
+ # Success callback is called after complete callback
55
+ if complete == children && pending == failure
56
+ Sidekiq.logger.debug {"Finalize parent complete bid: #{parent_bid}"}
57
+ Batch.enqueue_callbacks(:complete, parent_bid)
58
+ end
59
+
60
+ end
61
+
62
+ def complete(bid, status, parent_bid)
63
+ pending, children, success = Sidekiq.redis do |r|
64
+ r.multi do |pipeline|
65
+ pipeline.hincrby("BID-#{bid}", "pending", 0)
66
+ pipeline.hincrby("BID-#{bid}", "children", 0)
67
+ pipeline.scard("BID-#{bid}-success")
68
+ end
69
+ end
70
+
71
+ # if we batch was successful run success callback
72
+ if pending.to_i.zero? && children == success
73
+ Batch.enqueue_callbacks(:success, bid)
74
+
75
+ elsif parent_bid
76
+ # if batch was not successfull check and see if its parent is complete
77
+ # if the parent is complete we trigger the complete callback
78
+ # We don't want to run this if the batch was successfull because the success
79
+ # callback may add more jobs to the parent batch
80
+
81
+ Sidekiq.logger.debug {"Finalize parent complete bid: #{parent_bid}"}
82
+ _, complete, pending, children, failure = Sidekiq.redis do |r|
83
+ r.multi do |pipeline|
84
+ pipeline.sadd("BID-#{parent_bid}-complete", [bid])
85
+ pipeline.scard("BID-#{parent_bid}-complete")
86
+ pipeline.hincrby("BID-#{parent_bid}", "pending", 0)
87
+ pipeline.hincrby("BID-#{parent_bid}", "children", 0)
88
+ pipeline.scard("BID-#{parent_bid}-failed")
89
+ end
90
+ end
91
+ if complete == children && pending == failure
92
+ Batch.enqueue_callbacks(:complete, parent_bid)
93
+ end
94
+ end
95
+ end
96
+
97
+ def cleanup_redis bid, callback_bid=nil
98
+ Sidekiq::Batch.cleanup_redis bid
99
+ Sidekiq::Batch.cleanup_redis callback_bid if callback_bid
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,15 @@
1
+ module Sidekiq::Batch::Extension
2
+ module Worker
3
+ def bid
4
+ Thread.current[:batch].bid
5
+ end
6
+
7
+ def batch
8
+ Thread.current[:batch]
9
+ end
10
+
11
+ def valid_within_batch?
12
+ batch.valid?
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,55 @@
1
+ require_relative 'extension/worker'
2
+
3
+ module Sidekiq
4
+ class Batch
5
+ module Middleware
6
+ class ClientMiddleware
7
+ def call(_worker, msg, _queue, _redis_pool = nil)
8
+ if (batch = Thread.current[:batch])
9
+ batch.increment_job_queue(msg['jid']) if (msg[:bid] = batch.bid)
10
+ end
11
+ yield
12
+ end
13
+ end
14
+
15
+ class ServerMiddleware
16
+ def call(_worker, msg, _queue)
17
+ if (bid = msg['bid'])
18
+ begin
19
+ Thread.current[:batch] = Sidekiq::Batch.new(bid)
20
+ yield
21
+ Thread.current[:batch] = nil
22
+ Batch.process_successful_job(bid, msg['jid'])
23
+ rescue
24
+ Batch.process_failed_job(bid, msg['jid'])
25
+ raise
26
+ ensure
27
+ Thread.current[:batch] = nil
28
+ end
29
+ else
30
+ yield
31
+ end
32
+ end
33
+ end
34
+
35
+ def self.configure
36
+ Sidekiq.configure_client do |config|
37
+ config.client_middleware do |chain|
38
+ chain.add Sidekiq::Batch::Middleware::ClientMiddleware
39
+ end
40
+ end
41
+ Sidekiq.configure_server do |config|
42
+ config.client_middleware do |chain|
43
+ chain.add Sidekiq::Batch::Middleware::ClientMiddleware
44
+ end
45
+ config.server_middleware do |chain|
46
+ chain.add Sidekiq::Batch::Middleware::ServerMiddleware
47
+ end
48
+ end
49
+ Sidekiq::Worker.send(:include, Sidekiq::Batch::Extension::Worker)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ Sidekiq::Batch::Middleware.configure
@@ -0,0 +1,61 @@
1
+ module Sidekiq
2
+ class Batch
3
+ class Status
4
+ attr_reader :bid
5
+
6
+ def initialize(bid)
7
+ @bid = bid
8
+ end
9
+
10
+ def join
11
+ raise "Not supported"
12
+ end
13
+
14
+ def pending
15
+ Sidekiq.redis { |r| r.hget("BID-#{bid}", 'pending') }.to_i
16
+ end
17
+
18
+ def failures
19
+ Sidekiq.redis { |r| r.scard("BID-#{bid}-failed") }.to_i
20
+ end
21
+
22
+ def created_at
23
+ Sidekiq.redis { |r| r.hget("BID-#{bid}", 'created_at') }
24
+ end
25
+
26
+ def total
27
+ Sidekiq.redis { |r| r.hget("BID-#{bid}", 'total') }.to_i
28
+ end
29
+
30
+ def parent_bid
31
+ Sidekiq.redis { |r| r.hget("BID-#{bid}", "parent_bid") }
32
+ end
33
+
34
+ def failure_info
35
+ Sidekiq.redis { |r| r.smembers("BID-#{bid}-failed") } || []
36
+ end
37
+
38
+ def complete?
39
+ 'true' == Sidekiq.redis { |r| r.hget("BID-#{bid}", 'complete') }
40
+ end
41
+
42
+ def child_count
43
+ Sidekiq.redis { |r| r.hget("BID-#{bid}", 'children') }.to_i
44
+ end
45
+
46
+ def data
47
+ {
48
+ bid: bid,
49
+ total: total,
50
+ failures: failures,
51
+ pending: pending,
52
+ created_at: created_at,
53
+ complete: complete?,
54
+ failure_info: failure_info,
55
+ parent_bid: parent_bid,
56
+ child_count: child_count
57
+ }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ class Batch
3
+ VERSION = '0.3.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,331 @@
1
+ require 'securerandom'
2
+ require 'sidekiq'
3
+
4
+ require 'sidekiq/batch/callback'
5
+ require 'sidekiq/batch/middleware'
6
+ require 'sidekiq/batch/status'
7
+ require 'sidekiq/batch/version'
8
+
9
+ module Sidekiq
10
+ class Batch
11
+ class NoBlockGivenError < StandardError; end
12
+
13
+ BID_EXPIRE_TTL = 2_592_000
14
+
15
+ attr_reader :bid, :description, :callback_queue, :created_at
16
+
17
+ def initialize(existing_bid = nil)
18
+ @bid = existing_bid || SecureRandom.urlsafe_base64(10)
19
+ @existing = !(!existing_bid || existing_bid.empty?) # Basically existing_bid.present?
20
+ @initialized = false
21
+ @created_at = Time.now.utc.to_f
22
+ @bidkey = "BID-" + @bid.to_s
23
+ @queued_jids = []
24
+ @pending_jids = []
25
+
26
+ @incremental_push = !Sidekiq.default_configuration[:batch_push_interval].nil?
27
+ @batch_push_interval = Sidekiq.default_configuration[:batch_push_interval]
28
+ end
29
+
30
+ def description=(description)
31
+ @description = description
32
+ persist_bid_attr('description', description)
33
+ end
34
+
35
+ def callback_queue=(callback_queue)
36
+ @callback_queue = callback_queue
37
+ persist_bid_attr('callback_queue', callback_queue)
38
+ end
39
+
40
+ def callback_batch=(callback_batch)
41
+ @callback_batch = callback_batch
42
+ persist_bid_attr('callback_batch', callback_batch)
43
+ end
44
+
45
+ def on(event, callback, options = {})
46
+ return unless %w(success complete).include?(event.to_s)
47
+ callback_key = "#{@bidkey}-callbacks-#{event}"
48
+ Sidekiq.redis do |r|
49
+ r.multi do |pipeline|
50
+ pipeline.sadd(callback_key, [JSON.unparse({
51
+ callback: callback,
52
+ opts: options
53
+ })])
54
+ pipeline.expire(callback_key, BID_EXPIRE_TTL)
55
+ end
56
+ end
57
+ end
58
+
59
+ def jobs
60
+ raise NoBlockGivenError unless block_given?
61
+
62
+ bid_data, Thread.current[:bid_data] = Thread.current[:bid_data], []
63
+
64
+ begin
65
+ if !@existing && !@initialized
66
+ parent_bid = Thread.current[:batch].bid if Thread.current[:batch]
67
+
68
+ Sidekiq.redis do |r|
69
+ r.multi do |pipeline|
70
+ pipeline.hset(@bidkey, "created_at", @created_at)
71
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
72
+ if parent_bid
73
+ pipeline.hset(@bidkey, "parent_bid", parent_bid.to_s)
74
+ pipeline.hincrby("BID-#{parent_bid}", "children", 1)
75
+ end
76
+ end
77
+ end
78
+
79
+ @initialized = true
80
+ end
81
+
82
+ @queued_jids = []
83
+ @pending_jids = []
84
+
85
+ begin
86
+ parent = Thread.current[:batch]
87
+ Thread.current[:batch] = self
88
+ Thread.current[:parent_bid] = parent_bid
89
+ yield
90
+ ensure
91
+ Thread.current[:batch] = parent
92
+ Thread.current[:parent_bid] = nil
93
+ end
94
+
95
+ return [] if @queued_jids.size == 0
96
+ conditional_redis_increment!(true)
97
+
98
+ Sidekiq.redis do |r|
99
+ r.multi do |pipeline|
100
+ if parent_bid
101
+ pipeline.expire("BID-#{parent_bid}", BID_EXPIRE_TTL)
102
+ end
103
+
104
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
105
+
106
+ pipeline.sadd(@bidkey + "-jids", @queued_jids)
107
+ pipeline.expire(@bidkey + "-jids", BID_EXPIRE_TTL)
108
+ end
109
+ end
110
+
111
+ @queued_jids
112
+ ensure
113
+ Thread.current[:bid_data] = bid_data
114
+ end
115
+ end
116
+
117
+ def increment_job_queue(jid)
118
+ @queued_jids << jid
119
+ @pending_jids << jid
120
+ conditional_redis_increment!
121
+ end
122
+
123
+ def conditional_redis_increment!(force=false)
124
+ if should_increment? || force
125
+ parent_bid = Thread.current[:parent_bid]
126
+ Sidekiq.redis do |r|
127
+ r.multi do |pipeline|
128
+ if parent_bid
129
+ pipeline.hincrby("BID-#{parent_bid}", "total", @pending_jids.length)
130
+ pipeline.expire("BID-#{parent_bid}", BID_EXPIRE_TTL)
131
+ end
132
+
133
+ pipeline.hincrby(@bidkey, "pending", @pending_jids.length)
134
+ pipeline.hincrby(@bidkey, "total", @pending_jids.length)
135
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
136
+ end
137
+ end
138
+ @pending_jids = []
139
+ end
140
+ end
141
+
142
+ def should_increment?
143
+ return false unless @incremental_push
144
+ return true if @batch_push_interval == 0 || @queued_jids.length == 1
145
+ now = Time.now.to_f
146
+ @last_increment ||= now
147
+ if @last_increment + @batch_push_interval > now
148
+ @last_increment = now
149
+ return true
150
+ end
151
+ end
152
+
153
+ def invalidate_all
154
+ Sidekiq.redis do |r|
155
+ r.setex("invalidated-bid-#{bid}", BID_EXPIRE_TTL, 1)
156
+ end
157
+ end
158
+
159
+ def parent_bid
160
+ Sidekiq.redis do |r|
161
+ r.hget(@bidkey, "parent_bid")
162
+ end
163
+ end
164
+
165
+ def parent
166
+ if parent_bid
167
+ Sidekiq::Batch.new(parent_bid)
168
+ end
169
+ end
170
+
171
+ def valid?(batch = self)
172
+ valid = Sidekiq.redis { |r| r.exists("invalidated-bid-#{batch.bid}") }.zero?
173
+ batch.parent ? valid && valid?(batch.parent) : valid
174
+ end
175
+
176
+ private
177
+
178
+ def persist_bid_attr(attribute, value)
179
+ Sidekiq.redis do |r|
180
+ r.multi do |pipeline|
181
+ pipeline.hset(@bidkey, attribute, value)
182
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
183
+ end
184
+ end
185
+ end
186
+
187
+ class << self
188
+ def process_failed_job(bid, jid)
189
+ _, pending, failed, children, complete, parent_bid = Sidekiq.redis do |r|
190
+ r.multi do |pipeline|
191
+ pipeline.sadd("BID-#{bid}-failed", [jid])
192
+
193
+ pipeline.hincrby("BID-#{bid}", "pending", 0)
194
+ pipeline.scard("BID-#{bid}-failed")
195
+ pipeline.hincrby("BID-#{bid}", "children", 0)
196
+ pipeline.scard("BID-#{bid}-complete")
197
+ pipeline.hget("BID-#{bid}", "parent_bid")
198
+
199
+ pipeline.expire("BID-#{bid}-failed", BID_EXPIRE_TTL)
200
+ end
201
+ end
202
+
203
+ # if the batch failed, and has a parent, update the parent to show one pending and failed job
204
+ if parent_bid
205
+ Sidekiq.redis do |r|
206
+ r.multi do |pipeline|
207
+ pipeline.hincrby("BID-#{parent_bid}", "pending", 1)
208
+ pipeline.sadd("BID-#{parent_bid}-failed", [jid])
209
+ pipeline.expire("BID-#{parent_bid}-failed", BID_EXPIRE_TTL)
210
+ end
211
+ end
212
+ end
213
+
214
+ if pending.to_i == failed.to_i && children == complete
215
+ enqueue_callbacks(:complete, bid)
216
+ end
217
+ end
218
+
219
+ def process_successful_job(bid, jid)
220
+ failed, pending, children, complete, success, total, parent_bid = Sidekiq.redis do |r|
221
+ r.multi do |pipeline|
222
+ pipeline.scard("BID-#{bid}-failed")
223
+ pipeline.hincrby("BID-#{bid}", "pending", -1)
224
+ pipeline.hincrby("BID-#{bid}", "children", 0)
225
+ pipeline.scard("BID-#{bid}-complete")
226
+ pipeline.scard("BID-#{bid}-success")
227
+ pipeline.hget("BID-#{bid}", "total")
228
+ pipeline.hget("BID-#{bid}", "parent_bid")
229
+
230
+ pipeline.srem("BID-#{bid}-failed", [jid])
231
+ pipeline.srem("BID-#{bid}-jids", [jid])
232
+ pipeline.expire("BID-#{bid}", BID_EXPIRE_TTL)
233
+ end
234
+ end
235
+
236
+ all_success = pending.to_i.zero? && children == success
237
+ # if complete or successfull call complete callback (the complete callback may then call successful)
238
+ if (pending.to_i == failed.to_i && children == complete) || all_success
239
+ enqueue_callbacks(:complete, bid)
240
+ enqueue_callbacks(:success, bid) if all_success
241
+ end
242
+ end
243
+
244
+ def enqueue_callbacks(event, bid)
245
+ event_name = event.to_s
246
+ batch_key = "BID-#{bid}"
247
+ callback_key = "#{batch_key}-callbacks-#{event_name}"
248
+ already_processed, _, callbacks, queue, parent_bid, callback_batch = Sidekiq.redis do |r|
249
+ r.multi do |pipeline|
250
+ pipeline.hget(batch_key, event_name)
251
+ pipeline.hset(batch_key, event_name, 'true')
252
+ pipeline.smembers(callback_key)
253
+ pipeline.hget(batch_key, "callback_queue")
254
+ pipeline.hget(batch_key, "parent_bid")
255
+ pipeline.hget(batch_key, "callback_batch")
256
+ end
257
+ end
258
+
259
+ return if already_processed == 'true'
260
+
261
+ queue ||= "default"
262
+ parent_bid = !parent_bid || parent_bid.empty? ? nil : parent_bid # Basically parent_bid.blank?
263
+ callback_args = callbacks.reduce([]) do |memo, jcb|
264
+ cb = Sidekiq.load_json(jcb)
265
+ memo << [cb['callback'], event_name, cb['opts'], bid, parent_bid]
266
+ end
267
+
268
+ opts = {"bid" => bid, "event" => event_name}
269
+
270
+ # Run callback batch finalize synchronously
271
+ if callback_batch
272
+ # Extract opts from cb_args or use current
273
+ # Pass in stored event as callback finalize is processed on complete event
274
+ cb_opts = callback_args.first&.at(2) || opts
275
+
276
+ Sidekiq.logger.debug {"Run callback batch bid: #{bid} event: #{event_name} args: #{callback_args.inspect}"}
277
+ # Finalize now
278
+ finalizer = Sidekiq::Batch::Callback::Finalize.new
279
+ status = Status.new bid
280
+ finalizer.dispatch(status, cb_opts)
281
+
282
+ return
283
+ end
284
+
285
+ Sidekiq.logger.debug {"Enqueue callback bid: #{bid} event: #{event_name} args: #{callback_args.inspect}"}
286
+
287
+ if callback_args.empty?
288
+ # Finalize now
289
+ finalizer = Sidekiq::Batch::Callback::Finalize.new
290
+ status = Status.new bid
291
+ finalizer.dispatch(status, opts)
292
+ else
293
+ # Otherwise finalize in sub batch complete callback
294
+ cb_batch = self.new
295
+ cb_batch.callback_batch = 'true'
296
+ Sidekiq.logger.debug {"Adding callback batch: #{cb_batch.bid} for batch: #{bid}"}
297
+ cb_batch.on(:complete, "Sidekiq::Batch::Callback::Finalize#dispatch", opts)
298
+ cb_batch.jobs do
299
+ push_callbacks callback_args, queue
300
+ end
301
+ end
302
+ end
303
+
304
+ def cleanup_redis(bid)
305
+ Sidekiq.logger.debug {"Cleaning redis of batch #{bid}"}
306
+ Sidekiq.redis do |r|
307
+ r.del(
308
+ "BID-#{bid}",
309
+ "BID-#{bid}-callbacks-complete",
310
+ "BID-#{bid}-callbacks-success",
311
+ "BID-#{bid}-failed",
312
+
313
+ "BID-#{bid}-success",
314
+ "BID-#{bid}-complete",
315
+ "BID-#{bid}-jids",
316
+ )
317
+ end
318
+ end
319
+
320
+ private
321
+
322
+ def push_callbacks args, queue
323
+ Sidekiq::Client.push_bulk(
324
+ 'class' => Sidekiq::Batch::Callback::Worker,
325
+ 'args' => args,
326
+ 'queue' => queue
327
+ ) unless args.empty?
328
+ end
329
+ end
330
+ end
331
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq/batch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-batchs"
8
+ spec.version = Sidekiq::Batch::VERSION
9
+ spec.authors = ["Marcin Naglik"]
10
+ spec.email = ["tianlu1677@gmail.com"]
11
+
12
+ spec.summary = "Sidekiq Batch Jobs"
13
+ spec.description = "Sidekiq Batch Jobs Implementation"
14
+ spec.homepage = "http://github.com/breamware/sidekiq-batch"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "sidekiq", ">= 7", "<8"
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.1"
25
+ spec.add_development_dependency "rake", "~> 13.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-batchs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Naglik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-08 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: '7'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '7'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.1'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '13.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '13.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ description: Sidekiq Batch Jobs Implementation
76
+ email:
77
+ - tianlu1677@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".github/FUNDING.yml"
83
+ - ".github/dependabot.yml"
84
+ - ".github/workflows/ci.yml"
85
+ - ".github/workflows/codeql-analysis.yml"
86
+ - ".github/workflows/stale.yml"
87
+ - ".gitignore"
88
+ - ".rspec"
89
+ - ".travis.yml"
90
+ - Gemfile
91
+ - Guardfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/sidekiq/batch.rb
96
+ - lib/sidekiq/batch/callback.rb
97
+ - lib/sidekiq/batch/extension/worker.rb
98
+ - lib/sidekiq/batch/middleware.rb
99
+ - lib/sidekiq/batch/status.rb
100
+ - lib/sidekiq/batch/version.rb
101
+ - sidekiq-batch.gemspec
102
+ homepage: http://github.com/breamware/sidekiq-batch
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubygems_version: 3.1.6
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Sidekiq Batch Jobs
125
+ test_files: []