sidekiq-batch-temp 0.2.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: 0bddc7bf37f62645d9c7d5ef4d4668eeacf219945a8500c5730b7d872b65ef0c
4
+ data.tar.gz: 3f9ce84c57a3ecb13faf4555f563214cac97d2cc295ca65057e832e21aa60ffd
5
+ SHA512:
6
+ metadata.gz: 73773ffcbd75bb8fe3b9a4e05976366a3a4fbb1a94acc49bfe562d480b09f83cf555121ddf327ec99738c79561042ca95a3432b11b081d940a6944f3c9a2d51d
7
+ data.tar.gz: 1337f9f9f7f8f4b9f1bfc257d16051655124ff3a1b95125d8ea67737abb6423e4a1164a3899b4396891f99e1e90574374cdb5d1ca1ab9a9e83a63ec03dcf791f
@@ -0,0 +1,3 @@
1
+ # These are supported funding model platforms
2
+
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,23 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ strategy:
11
+ fail-fast: false
12
+ matrix:
13
+ ruby: ["2.5", "2.6", "2.7", "3.0", "3.1", ruby-head]
14
+
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ bundler-cache: true # 'bundle install' and cache gems
21
+ ruby-version: ${{ matrix.ruby }}
22
+ - name: Run tests
23
+ 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.2.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,330 @@
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
+ @incremental_push = Sidekiq.options.keys.include?(:batch_push_interval)
26
+ @batch_push_interval = Sidekiq.options[:batch_push_interval]
27
+ end
28
+
29
+ def description=(description)
30
+ @description = description
31
+ persist_bid_attr('description', description)
32
+ end
33
+
34
+ def callback_queue=(callback_queue)
35
+ @callback_queue = callback_queue
36
+ persist_bid_attr('callback_queue', callback_queue)
37
+ end
38
+
39
+ def callback_batch=(callback_batch)
40
+ @callback_batch = callback_batch
41
+ persist_bid_attr('callback_batch', callback_batch)
42
+ end
43
+
44
+ def on(event, callback, options = {})
45
+ return unless %w(success complete).include?(event.to_s)
46
+ callback_key = "#{@bidkey}-callbacks-#{event}"
47
+ Sidekiq.redis do |r|
48
+ r.multi do |pipeline|
49
+ pipeline.sadd(callback_key, [JSON.unparse({
50
+ callback: callback,
51
+ opts: options
52
+ })])
53
+ pipeline.expire(callback_key, BID_EXPIRE_TTL)
54
+ end
55
+ end
56
+ end
57
+
58
+ def jobs
59
+ raise NoBlockGivenError unless block_given?
60
+
61
+ bid_data, Thread.current[:bid_data] = Thread.current[:bid_data], []
62
+
63
+ begin
64
+ if !@existing && !@initialized
65
+ parent_bid = Thread.current[:batch].bid if Thread.current[:batch]
66
+
67
+ Sidekiq.redis do |r|
68
+ r.multi do |pipeline|
69
+ pipeline.hset(@bidkey, "created_at", @created_at)
70
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
71
+ if parent_bid
72
+ pipeline.hset(@bidkey, "parent_bid", parent_bid.to_s)
73
+ pipeline.hincrby("BID-#{parent_bid}", "children", 1)
74
+ end
75
+ end
76
+ end
77
+
78
+ @initialized = true
79
+ end
80
+
81
+ @queued_jids = []
82
+ @pending_jids = []
83
+
84
+ begin
85
+ parent = Thread.current[:batch]
86
+ Thread.current[:batch] = self
87
+ Thread.current[:parent_bid] = parent_bid
88
+ yield
89
+ ensure
90
+ Thread.current[:batch] = parent
91
+ Thread.current[:parent_bid] = nil
92
+ end
93
+
94
+ return [] if @queued_jids.size == 0
95
+ conditional_redis_increment!(true)
96
+
97
+ Sidekiq.redis do |r|
98
+ r.multi do |pipeline|
99
+ if parent_bid
100
+ pipeline.expire("BID-#{parent_bid}", BID_EXPIRE_TTL)
101
+ end
102
+
103
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
104
+
105
+ pipeline.sadd(@bidkey + "-jids", [@queued_jids])
106
+ pipeline.expire(@bidkey + "-jids", BID_EXPIRE_TTL)
107
+ end
108
+ end
109
+
110
+ @queued_jids
111
+ ensure
112
+ Thread.current[:bid_data] = bid_data
113
+ end
114
+ end
115
+
116
+ def increment_job_queue(jid)
117
+ @queued_jids << jid
118
+ @pending_jids << jid
119
+ conditional_redis_increment!
120
+ end
121
+
122
+ def conditional_redis_increment!(force=false)
123
+ if should_increment? || force
124
+ parent_bid = Thread.current[:parent_bid]
125
+ Sidekiq.redis do |r|
126
+ r.multi do |pipeline|
127
+ if parent_bid
128
+ pipeline.hincrby("BID-#{parent_bid}", "total", @pending_jids.length)
129
+ pipeline.expire("BID-#{parent_bid}", BID_EXPIRE_TTL)
130
+ end
131
+
132
+ pipeline.hincrby(@bidkey, "pending", @pending_jids.length)
133
+ pipeline.hincrby(@bidkey, "total", @pending_jids.length)
134
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
135
+ end
136
+ end
137
+ @pending_jids = []
138
+ end
139
+ end
140
+
141
+ def should_increment?
142
+ return false unless @incremental_push
143
+ return true if @batch_push_interval == 0 || @queued_jids.length == 1
144
+ now = Time.now.to_f
145
+ @last_increment ||= now
146
+ if @last_increment + @batch_push_interval > now
147
+ @last_increment = now
148
+ return true
149
+ end
150
+ end
151
+
152
+ def invalidate_all
153
+ Sidekiq.redis do |r|
154
+ r.setex("invalidated-bid-#{bid}", BID_EXPIRE_TTL, 1)
155
+ end
156
+ end
157
+
158
+ def parent_bid
159
+ Sidekiq.redis do |r|
160
+ r.hget(@bidkey, "parent_bid")
161
+ end
162
+ end
163
+
164
+ def parent
165
+ if parent_bid
166
+ Sidekiq::Batch.new(parent_bid)
167
+ end
168
+ end
169
+
170
+ def valid?(batch = self)
171
+ valid = !Sidekiq.redis { |r| r.exists("invalidated-bid-#{batch.bid}") }
172
+ batch.parent ? valid && valid?(batch.parent) : valid
173
+ end
174
+
175
+ private
176
+
177
+ def persist_bid_attr(attribute, value)
178
+ Sidekiq.redis do |r|
179
+ r.multi do |pipeline|
180
+ pipeline.hset(@bidkey, attribute, value)
181
+ pipeline.expire(@bidkey, BID_EXPIRE_TTL)
182
+ end
183
+ end
184
+ end
185
+
186
+ class << self
187
+ def process_failed_job(bid, jid)
188
+ _, pending, failed, children, complete, parent_bid = Sidekiq.redis do |r|
189
+ r.multi do |pipeline|
190
+ pipeline.sadd("BID-#{bid}-failed", [jid])
191
+
192
+ pipeline.hincrby("BID-#{bid}", "pending", 0)
193
+ pipeline.scard("BID-#{bid}-failed")
194
+ pipeline.hincrby("BID-#{bid}", "children", 0)
195
+ pipeline.scard("BID-#{bid}-complete")
196
+ pipeline.hget("BID-#{bid}", "parent_bid")
197
+
198
+ pipeline.expire("BID-#{bid}-failed", BID_EXPIRE_TTL)
199
+ end
200
+ end
201
+
202
+ # if the batch failed, and has a parent, update the parent to show one pending and failed job
203
+ if parent_bid
204
+ Sidekiq.redis do |r|
205
+ r.multi do |pipeline|
206
+ pipeline.hincrby("BID-#{parent_bid}", "pending", 1)
207
+ pipeline.sadd("BID-#{parent_bid}-failed", [jid])
208
+ pipeline.expire("BID-#{parent_bid}-failed", BID_EXPIRE_TTL)
209
+ end
210
+ end
211
+ end
212
+
213
+ if pending.to_i == failed.to_i && children == complete
214
+ enqueue_callbacks(:complete, bid)
215
+ end
216
+ end
217
+
218
+ def process_successful_job(bid, jid)
219
+ failed, pending, children, complete, success, total, parent_bid = Sidekiq.redis do |r|
220
+ r.multi do |pipeline|
221
+ pipeline.scard("BID-#{bid}-failed")
222
+ pipeline.hincrby("BID-#{bid}", "pending", -1)
223
+ pipeline.hincrby("BID-#{bid}", "children", 0)
224
+ pipeline.scard("BID-#{bid}-complete")
225
+ pipeline.scard("BID-#{bid}-success")
226
+ pipeline.hget("BID-#{bid}", "total")
227
+ pipeline.hget("BID-#{bid}", "parent_bid")
228
+
229
+ pipeline.srem("BID-#{bid}-failed", [jid])
230
+ pipeline.srem("BID-#{bid}-jids", [jid])
231
+ pipeline.expire("BID-#{bid}", BID_EXPIRE_TTL)
232
+ end
233
+ end
234
+
235
+ all_success = pending.to_i.zero? && children == success
236
+ # if complete or successfull call complete callback (the complete callback may then call successful)
237
+ if (pending.to_i == failed.to_i && children == complete) || all_success
238
+ enqueue_callbacks(:complete, bid)
239
+ enqueue_callbacks(:success, bid) if all_success
240
+ end
241
+ end
242
+
243
+ def enqueue_callbacks(event, bid)
244
+ event_name = event.to_s
245
+ batch_key = "BID-#{bid}"
246
+ callback_key = "#{batch_key}-callbacks-#{event_name}"
247
+ already_processed, _, callbacks, queue, parent_bid, callback_batch = Sidekiq.redis do |r|
248
+ r.multi do |pipeline|
249
+ pipeline.hget(batch_key, event_name)
250
+ pipeline.hset(batch_key, event_name, true)
251
+ pipeline.smembers(callback_key)
252
+ pipeline.hget(batch_key, "callback_queue")
253
+ pipeline.hget(batch_key, "parent_bid")
254
+ pipeline.hget(batch_key, "callback_batch")
255
+ end
256
+ end
257
+
258
+ return if already_processed == 'true'
259
+
260
+ queue ||= "default"
261
+ parent_bid = !parent_bid || parent_bid.empty? ? nil : parent_bid # Basically parent_bid.blank?
262
+ callback_args = callbacks.reduce([]) do |memo, jcb|
263
+ cb = Sidekiq.load_json(jcb)
264
+ memo << [cb['callback'], event_name, cb['opts'], bid, parent_bid]
265
+ end
266
+
267
+ opts = {"bid" => bid, "event" => event_name}
268
+
269
+ # Run callback batch finalize synchronously
270
+ if callback_batch
271
+ # Extract opts from cb_args or use current
272
+ # Pass in stored event as callback finalize is processed on complete event
273
+ cb_opts = callback_args.first&.at(2) || opts
274
+
275
+ Sidekiq.logger.debug {"Run callback batch bid: #{bid} event: #{event_name} args: #{callback_args.inspect}"}
276
+ # Finalize now
277
+ finalizer = Sidekiq::Batch::Callback::Finalize.new
278
+ status = Status.new bid
279
+ finalizer.dispatch(status, cb_opts)
280
+
281
+ return
282
+ end
283
+
284
+ Sidekiq.logger.debug {"Enqueue callback bid: #{bid} event: #{event_name} args: #{callback_args.inspect}"}
285
+
286
+ if callback_args.empty?
287
+ # Finalize now
288
+ finalizer = Sidekiq::Batch::Callback::Finalize.new
289
+ status = Status.new bid
290
+ finalizer.dispatch(status, opts)
291
+ else
292
+ # Otherwise finalize in sub batch complete callback
293
+ cb_batch = self.new
294
+ cb_batch.callback_batch = true
295
+ Sidekiq.logger.debug {"Adding callback batch: #{cb_batch.bid} for batch: #{bid}"}
296
+ cb_batch.on(:complete, "Sidekiq::Batch::Callback::Finalize#dispatch", opts)
297
+ cb_batch.jobs do
298
+ push_callbacks callback_args, queue
299
+ end
300
+ end
301
+ end
302
+
303
+ def cleanup_redis(bid)
304
+ Sidekiq.logger.debug {"Cleaning redis of batch #{bid}"}
305
+ Sidekiq.redis do |r|
306
+ r.del(
307
+ "BID-#{bid}",
308
+ "BID-#{bid}-callbacks-complete",
309
+ "BID-#{bid}-callbacks-success",
310
+ "BID-#{bid}-failed",
311
+
312
+ "BID-#{bid}-success",
313
+ "BID-#{bid}-complete",
314
+ "BID-#{bid}-jids",
315
+ )
316
+ end
317
+ end
318
+
319
+ private
320
+
321
+ def push_callbacks args, queue
322
+ Sidekiq::Client.push_bulk(
323
+ 'class' => Sidekiq::Batch::Callback::Worker,
324
+ 'args' => args,
325
+ 'queue' => queue
326
+ ) unless args.empty?
327
+ end
328
+ end
329
+ end
330
+ end
@@ -0,0 +1,28 @@
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-batch-temp"
8
+ spec.version = Sidekiq::Batch::VERSION
9
+ spec.authors = ["Marcin Naglik"]
10
+ spec.email = ["marcin.naglik@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", ">= 3", "<7"
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
+ spec.add_development_dependency "fakeredis", "~> 0.8.0"
28
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-batch-temp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Naglik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-30 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
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
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
+ - !ruby/object:Gem::Dependency
76
+ name: fakeredis
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.8.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.8.0
89
+ description: Sidekiq Batch Jobs Implementation
90
+ email:
91
+ - marcin.naglik@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".github/FUNDING.yml"
97
+ - ".github/dependabot.yml"
98
+ - ".github/workflows/ci.yml"
99
+ - ".github/workflows/codeql-analysis.yml"
100
+ - ".github/workflows/stale.yml"
101
+ - ".gitignore"
102
+ - ".rspec"
103
+ - ".travis.yml"
104
+ - Gemfile
105
+ - Guardfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/sidekiq/batch.rb
110
+ - lib/sidekiq/batch/callback.rb
111
+ - lib/sidekiq/batch/extension/worker.rb
112
+ - lib/sidekiq/batch/middleware.rb
113
+ - lib/sidekiq/batch/status.rb
114
+ - lib/sidekiq/batch/version.rb
115
+ - sidekiq-batch.gemspec
116
+ homepage: http://github.com/breamware/sidekiq-batch
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubygems_version: 3.4.6
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Sidekiq Batch Jobs
139
+ test_files: []