sidekiq-rerouting 0.2.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ebea5ac549b2661e67c1aa3cbc6c387021704376fc169f6578daef26cbf30d64
4
- data.tar.gz: 42cbc382bac80ac07156d982cc395588da2357914d0a6a256de0b1acf1d128a4
3
+ metadata.gz: a6b33ce67524b43eeb69f383ac34528f0d31f7d26df43fa5ab1bd84f8b5d5d52
4
+ data.tar.gz: 80f81ed20b959d9cdb00bb32553e482becc753d834e071f9d3de5ec22f4fd692
5
5
  SHA512:
6
- metadata.gz: 4be3c9613717b52d8593d5df299b02fca2d2cd195b1870f31965ce513add925b76f44ee5a7fb0e33464bb7a9af24b11a2c58fffd6882f67ccc17dd1560a563c0
7
- data.tar.gz: 6154bbd5d5055ead93650ebbd5e79f39b929a15d10defba1f9f998895d6881060f755eb527f15af72090d45166381df5f23776734633a90918a8341b2eb3bc2f
6
+ metadata.gz: aa9a5eeb4e93350104159fc2e844fed05d26a28fc558328b4a11243e8759cf615509d2815a5f94ae21e9652ee9c7f84aaf0dc786d7ebe6c663a8d5640ad41fec
7
+ data.tar.gz: bce4f1c97fe84ee5234de594e6b044a30f2d5c0e9ba267d25d8e0980da20bdcda2d4d6dd7b7927fe8dd6b29c82e54a59f4850532909f49b5b39b9e477fcb233e
data/CHANGELOG.md CHANGED
@@ -1,9 +1,17 @@
1
- ## [Unreleased]
1
+ # Changelog
2
2
 
3
- ## [0.2.0] - 2025-09-22
3
+ ## [Unreleased]:
4
+
5
+ ## [0.3.0]: 2025-11-10
6
+
7
+ - Added support for Sidekiq Pro's Batch feature.
8
+ - **Breaking**: the `on_reroute` callback's `job` parameter is now the rerouted job instead of the original job.
9
+ Meaning the `job["queue"]` will be the rerouted queue, not the original queue.
10
+
11
+ ## [0.2.0]: 2025-09-22
4
12
 
5
13
  - **Breaking**: Change configuration of middleware to work with `Sidekiq::Middleware::Chain` API
6
14
 
7
- ## [0.1.0] - 2025-09-22
15
+ ## [0.1.0]: 2025-09-22
8
16
 
9
17
  - Initial release
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Sidekiq::Rerouting
2
2
 
3
- [![CI](https://github.com/hibachrach/sidekiq-disposal/actions/workflows/main.yml/badge.svg)](https://github.com/hibachrach/sidekiq-disposal/actions)
3
+ [![CI](https://github.com/hibachrach/sidekiq-rerouting/actions/workflows/main.yml/badge.svg)](https://github.com/hibachrach/sidekiq-rerouting/actions)
4
4
 
5
5
  A [Sidekiq][sidekiq] extension to set Sidekiq jobs to be rerouted to a different queue based on the job ID or job class.
6
6
 
@@ -23,7 +23,7 @@ gem install sidekiq-rerouting
23
23
  From a console (Rails console, or the like) you need a `Sidekiq::Rerouting::Client` instance, which is used to `#reroute` a job, or job class to be disposed.
24
24
 
25
25
  ```ruby
26
- client = Sidekiq::Disposal::Client.new
26
+ client = Sidekiq::Rerouting::Client.new
27
27
  ```
28
28
 
29
29
  ### Marking to reroute
@@ -38,14 +38,19 @@ client.reroute("different_queue", :jid, some_job_id)
38
38
  client.reroute("different_queue", :class, "SomeJobClass")
39
39
  ```
40
40
 
41
- A job or job class can also be removed from rerouting for disposal via a corresponding API.
41
+ A job or job class can also be removed from rerouting via a corresponding API. This only takes effects for jobs enqueued after the API call.
42
+ Reroute the jobs back to its original queue to affect the previously rerouted jobs still in the queue.
42
43
 
43
44
  ```ruby
44
- # Unmark a specific Job to be rerouted by specifying its job ID
45
- client.remove_rerouting("different_queue", :jid, some_job_id)
45
+ # Unmark a specific Job for rerouting by specifying its job ID
46
+ client.remove_rerouting(:jid, some_job_id)
46
47
 
47
- # Unmark an entire job class to be rerouted
48
- client.remove_rerouting("different_queue", :class, "SomeJobClass")
48
+ # Unmark an entire job class for rerouting
49
+ client.remove_rerouting(:class, "SomeJobClass")
50
+
51
+ # Force previously rerouted job class/jid to its original queue
52
+ client.reroute("original_queue", :jid, some_job_id)
53
+ client.reroute("original_queue", :class, "SomeJobClass")
49
54
  ```
50
55
 
51
56
  ### Clearing all rerouting
@@ -88,9 +93,23 @@ end
88
93
  ```
89
94
 
90
95
  It yields the following keyword arguments:
91
- * `job`: the serialized job that is being rerouted; see [Sidekiq's docs][job-format] for more details.
92
- * `old_queue`: the queue *from which* the job is being rerouted.
93
- * `new_queue`: the queue *to which* the job is being rerouted.
96
+
97
+ - `job`: the serialized job that is being rerouted; see [Sidekiq's docs][job-format] for more details.
98
+ - `old_queue`: the queue _from which_ the job is being rerouted.
99
+ - `new_queue`: the queue _to which_ the job is being rerouted.
100
+
101
+ ### Sidekiq Pro Batches
102
+
103
+ If you're using [Sidekiq Pro's Batch feature][sidekiq-batches] you need to be sure to insert the rerouting middleware _after_ the `Batch` middleware.
104
+ This will ensure the batch can be re-opened, and the rereouted job added to it.
105
+
106
+ ```ruby
107
+ Sidekiq.configure_server do |config|
108
+ config.server_middleware do |chain|
109
+ chain.insert_after(Sidekiq::Batch::Server, Sidekiq::Rerouting::ServerMiddleware)
110
+ end
111
+ end
112
+ ```
94
113
 
95
114
  ### Non-Reroutable Jobs
96
115
 
@@ -110,9 +129,9 @@ No extra Redis calls, no funny business.
110
129
 
111
130
  ## Development
112
131
 
113
- 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.
132
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
114
133
 
115
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
134
+ To install this gem onto your local machine, run `bin/rake install`. To release a new version, update the version number in `version.rb`, and then run `bin/rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
116
135
 
117
136
  ## Contributing
118
137
 
@@ -131,6 +150,7 @@ Everyone interacting in the Sidekiq::Rerouting project's codebases, issue tracke
131
150
  - [`sidekiq-disposal`][sidekiq-disposal]
132
151
 
133
152
  [sidekiq]: https://sidekiq.org "Simple, efficient background jobs for Ruby."
153
+ [sidekiq-batches]: https://github.com/sidekiq/sidekiq/wiki/Batches "Sidekiq Pro Batches"
134
154
  [sidekiq-disposal]: https://github.com/hibachrach/sidekiq-disposal "A Sidekiq extension to mark Sidekiq jobs to be disposed of."
135
155
  [sidekiq-register-middleware]: https://github.com/sidekiq/sidekiq/wiki/Middleware#registering-middleware "Registering Sidekiq Middleware"
136
156
  [job-format]: https://github.com/sidekiq/sidekiq/wiki/Job-Format "How Sidekiq jobs are serialized"
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rerouting"
4
+
5
+ module Sidekiq
6
+ module Rerouting
7
+ class BatchAPI
8
+ def jobs_in(batch:)
9
+ batch_api.new(batch).jobs do
10
+ yield
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def batch_api
17
+ @batch_api ||= defined?(::Sidekiq::Batch) ? ::Sidekiq::Batch : NullBatchAPI
18
+ end
19
+
20
+ class NullBatchAPI
21
+ def initialize(_batch_id)
22
+ end
23
+
24
+ def jobs
25
+ yield
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rerouting"
4
+ require_relative "batch_api"
5
+
6
+ module Sidekiq
7
+ module Rerouting
8
+ class Router
9
+ def initialize(job_instance:, job_payload:, client: Client.new, batch_api: BatchAPI.new)
10
+ @batch_api = batch_api
11
+ @client = client
12
+ @job_instance = job_instance
13
+ @job_payload = job_payload
14
+ @original_queue = job_payload["queue"]
15
+ end
16
+
17
+ attr_reader :original_queue
18
+
19
+ def reroutable?
20
+ job_instance && job_instance.class.get_sidekiq_options.fetch("reroutable", true)
21
+ end
22
+
23
+ def rerouted_from?(queue:)
24
+ rerouted_queue && rerouted_queue != queue
25
+ end
26
+
27
+ def rerouted_playload
28
+ job_payload.merge("queue" => rerouted_queue)
29
+ end
30
+
31
+ def rerouted_queue
32
+ return @rerouted_queue if defined?(@rerouted_queue)
33
+
34
+ @rerouted_queue = client.rerouting_destination(job_payload)
35
+ end
36
+
37
+ def within_batch_maybe
38
+ batch_id = job_payload["bid"]
39
+
40
+ return yield unless batch_id
41
+
42
+ batch_api.jobs_in(batch: batch_id) do
43
+ yield
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ attr_reader :batch_api, :client, :job_instance, :job_payload
50
+ end
51
+ end
52
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "sidekiq"
4
4
  require_relative "../rerouting"
5
+ require_relative "router"
5
6
 
6
7
  module Sidekiq
7
8
  module Rerouting
@@ -13,14 +14,13 @@ module Sidekiq
13
14
  @on_reroute = opts.fetch(:on_reroute, nil)
14
15
  end
15
16
 
16
- def call(job_instance, job, queue)
17
- if job_instance && !job_instance.class.get_sidekiq_options.fetch("reroutable", true)
18
- yield
19
- elsif job_instance &&
20
- (rerouting_destination = client.rerouting_destination(job)) &&
21
- rerouting_destination != queue
22
- job_instance.class.client_push(job.merge("queue" => rerouting_destination))
23
- on_reroute&.call(job:, old_queue: queue, new_queue: rerouting_destination)
17
+ def call(job_instance, job_payload, queue)
18
+ router = Router.new(job_instance:, job_payload:, client:)
19
+
20
+ if router.reroutable? && router.rerouted_from?(queue:)
21
+ router.within_batch_maybe do
22
+ reroute(sidekiq_client: job_instance.class, router: router)
23
+ end
24
24
  else
25
25
  yield
26
26
  end
@@ -29,6 +29,12 @@ module Sidekiq
29
29
  private
30
30
 
31
31
  attr_reader :client, :on_reroute
32
+
33
+ def reroute(sidekiq_client:, router:)
34
+ sidekiq_client.client_push(router.rerouted_playload)
35
+
36
+ on_reroute&.call(job: router.rerouted_playload, old_queue: router.original_queue, new_queue: router.rerouted_queue)
37
+ end
32
38
  end
33
39
  end
34
40
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sidekiq
4
4
  module Rerouting
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-rerouting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hazel Bachrach
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2025-09-22 00:00:00.000000000 Z
12
+ date: 2025-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sidekiq
@@ -41,7 +41,9 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - lib/sidekiq/rerouting.rb
44
+ - lib/sidekiq/rerouting/batch_api.rb
44
45
  - lib/sidekiq/rerouting/client.rb
46
+ - lib/sidekiq/rerouting/router.rb
45
47
  - lib/sidekiq/rerouting/server_middleware.rb
46
48
  - lib/sidekiq/rerouting/version.rb
47
49
  - sig/sidekiq/rerouting.rbs
@@ -68,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
70
  - !ruby/object:Gem::Version
69
71
  version: '0'
70
72
  requirements: []
71
- rubygems_version: 3.4.19
73
+ rubygems_version: 3.5.22
72
74
  signing_key:
73
75
  specification_version: 4
74
76
  summary: A mechanism to reroute queued jobs to a different queue on pickup