sidekiq-job-signal 0.1.1 → 0.1.2

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: e1b1269d2b8a1f085d4e4d401395d9782183816d914f743fae1a0ed53a49fe67
4
- data.tar.gz: 5b6c2f6a338cc074dae96217f46801fe8a388f8f89b115c07034be2b4abacffa
3
+ metadata.gz: aa69177da872fdf54f62d0834e9c91873e6b0d48482020eca1d47834bfcbe15f
4
+ data.tar.gz: 0ec3e0052920bc78933e024194624b17b771ecf5241cc890970e6dfdcbfdad1b
5
5
  SHA512:
6
- metadata.gz: 6c967793f23972ec8d9ed9ba9745156cf10602218e5cec2cecea0537a9320bfd8aefac1c03ce37a8658e06b22a0dcf8d83e611ac4133373a70a0e2262b79a17c
7
- data.tar.gz: 20ffd03cecc77dd267384c9d5e627cb726678cac7c00906325de05e7264f1e21a499253e490d314286b75607d3c31c8ba9ddf1827880aaa362378055e037bcdf
6
+ metadata.gz: 118bcc2de0547932c93a784345d45196034298e0181e3e3cfcbf638b914d74a80683b86f1e36ea46b9b760394adb07e44181664669663c3fc162b40aedd41fda
7
+ data.tar.gz: 8939c6978196a406867be42c898c6d7f03b31475213c8b1aacba892b12d5753577625fa9547c2c728a968108dd897b497d24b2731ea552bb1a08974ced89e1c5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sidekiq-job-signal (0.1.1)
4
+ sidekiq-job-signal (0.1.2)
5
5
  sidekiq (>= 6.5, < 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -8,6 +8,10 @@ Install the gem and add to the application's Gemfile by executing:
8
8
 
9
9
  $ bundle add sidekiq-job-signal
10
10
 
11
+ In your Gemfile, specify the gem as:
12
+
13
+ gem "sidekiq-job-signal", require: "sidekiq/job_signal"
14
+
11
15
  If bundler is not being used to manage dependencies, install the gem by executing:
12
16
 
13
17
  $ gem install sidekiq-job-signal
@@ -16,14 +20,32 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
20
 
17
21
  ```rb
18
22
  Sidekiq::JobSignal.quit(jid: "12345")
23
+ Sidekiq::JobSignal.quit(job_class: "ExampleJob")
19
24
 
20
25
  # log:
21
26
  # Turned #{12345}:#{JobWorkerClass} into a no-op: [1,2,3]"
22
27
 
28
+ # If you want to add the `quitting?` method to your job
29
+ class ExampleJob
30
+ include Sidekiq::Job
31
+ include Sidekiq::JobSignal::Receiver
32
+
33
+ def perform
34
+ if quitting?
35
+ # finish early...
36
+ end
37
+ end
38
+ end
39
+
23
40
  # middleware.rb
24
41
  Sidekiq.configure_server do |config|
25
42
  config.server_middleware do |chain|
43
+ # Defaults to by_class: false, by_jid: true
26
44
  chain.add ::Sidekiq::JobSignal::ServerMiddleware
45
+ # OR
46
+ chain.add ::Sidekiq::JobSignal::ServerMiddleware, by_class: true
47
+ # OR
48
+ chain.add ::Sidekiq::JobSignal::ServerMiddleware, by_jid: false, by_class: true
27
49
  end
28
50
 
29
51
  Sidekiq::JobSignal.on_quit do |job|
@@ -33,7 +55,7 @@ Sidekiq.configure_server do |config|
33
55
  end
34
56
  ```
35
57
 
36
- If you like to enable the Sidekiq Web UI for quitting jobs, you can include the following in some kind of initialization file. This will enable a new "Signals" tab.
58
+ If you'd like to enable the Sidekiq Web UI for quitting jobs, you can include the following in some kind of initialization file. This will enable a new "Signals" tab.
37
59
 
38
60
  ```rb
39
61
  Sidekiq::Web.register Sidekiq::JobSignal::Web
@@ -2,9 +2,9 @@
2
2
 
3
3
  module Sidekiq
4
4
  module JobSignal
5
- class Receiver
5
+ module Receiver
6
6
  def quitting?
7
- ::Sidekiq::JobSignal.quitting?(jid: jid, worker_class: self.class.name)
7
+ ::Sidekiq::JobSignal.quitting?(jid: jid, job_class: self.class.name)
8
8
  end
9
9
  end
10
10
  end
@@ -5,19 +5,36 @@ module Sidekiq
5
5
  class ServerMiddleware
6
6
  include ::Sidekiq::ServerMiddleware
7
7
 
8
- def call(worker, _job, _queue)
9
- signalled = ::Sidekiq::JobSignal.quitting?(worker_class: worker.class.name, jid: worker.jid)
10
- logger.info "Sidekiq::JobSignal::ServerMiddleware.call: signalled=#{signalled}"
11
- if signalled
12
- def worker.perform(*args)
13
- logger.info "Turned #{jid}:#{self.class} into a no-op: #{args.inspect}"
14
- end
15
- end
8
+ def initialize(options = {})
9
+ @by_jid = options.fetch(:by_jid, true)
10
+ @by_class = options.fetch(:by_class, false)
11
+ end
16
12
 
13
+ def call(job, job_payload, _queue)
14
+ signalled = ::Sidekiq::JobSignal.quitting?(**quit_options(job))
15
+ logger.debug "#{self.class}.call: signalled=#{signalled}"
16
+ noop_job(job) if signalled
17
17
  yield
18
18
  ensure
19
- logger.info "Sidekiq::JobSignal::ServerMiddleware.call: ensure signalled=#{signalled}"
20
- ::Sidekiq::JobSignal.handlers.each { |handler| handler.call(worker) } if signalled
19
+ logger.debug "#{self.class}.call: ensure signalled=#{signalled}"
20
+ ::Sidekiq::JobSignal.handlers.each { |handler| handler.call(job) } if signalled
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :by_jid, :by_class
26
+
27
+ def quit_options(job)
28
+ {}.tap do |options|
29
+ options[:job_class] = job.class.name if by_class
30
+ options[:jid] = job.jid if by_jid
31
+ end
32
+ end
33
+
34
+ def noop_job(job)
35
+ def job.perform(*args)
36
+ logger.info "Turned #{jid}:#{self.class} into a no-op: #{args.inspect}"
37
+ end
21
38
  end
22
39
  end
23
40
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sidekiq
4
4
  module JobSignal
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
@@ -18,4 +18,4 @@ module Sidekiq
18
18
  end
19
19
  end
20
20
  end
21
- end
21
+ end
@@ -17,29 +17,29 @@ module Sidekiq
17
17
  handlers << block
18
18
  end
19
19
 
20
- def quit(worker_class: "", jid: "")
20
+ def quit(job_class: "", jid: "")
21
21
  ::Sidekiq.redis do |r|
22
22
  r.pipelined do |pipeline|
23
23
  pipeline.set "jobsignal-#{jid}", "quit", ex: 86_400 if jid && !jid.empty?
24
- pipeline.set "jobsignal-#{worker_class}", "quit", ex: 86_400 if worker_class && !worker_class.empty?
24
+ pipeline.set "jobsignal-#{job_class}", "quit", ex: 86_400 if job_class && !job_class.empty?
25
25
  end
26
26
  end
27
27
  end
28
28
 
29
- def delete_signal(worker_class: "", jid: "")
29
+ def delete_signal(job_class: "", jid: "")
30
30
  ::Sidekiq.redis do |r|
31
31
  r.pipelined do |pipeline|
32
32
  pipeline.del("jobsignal-#{jid}") if jid && !jid.empty?
33
- pipeline.del("jobsignal-#{worker_class}") if worker_class && !worker_class.empty?
33
+ pipeline.del("jobsignal-#{job_class}") if job_class && !job_class.empty?
34
34
  end
35
35
  end
36
36
  end
37
37
 
38
- def quitting?(worker_class: "", jid: "")
38
+ def quitting?(job_class: "", jid: "")
39
39
  results = ::Sidekiq.redis do |r|
40
40
  r.pipelined do |pipeline|
41
41
  pipeline.get("jobsignal-#{jid}") if jid && !jid.empty?
42
- pipeline.get("jobsignal-#{worker_class}") if worker_class && !worker_class.empty?
42
+ pipeline.get("jobsignal-#{job_class}") if job_class && !job_class.empty?
43
43
  end
44
44
  end
45
45
  results.include?("quit")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-job-signal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Camara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-20 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  requirements: []
80
- rubygems_version: 3.1.6
80
+ rubygems_version: 3.4.10
81
81
  signing_key:
82
82
  specification_version: 4
83
83
  summary: Signal a job to quit, and block the job from executing through middleware.