sidekiq-status 0.5.0 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yaml +53 -0
  3. data/.gitignore +4 -1
  4. data/.gitlab-ci.yml +17 -0
  5. data/Appraisals +11 -0
  6. data/CHANGELOG.md +41 -0
  7. data/README.md +148 -41
  8. data/Rakefile +2 -0
  9. data/gemfiles/sidekiq_6.1.gemfile +7 -0
  10. data/gemfiles/sidekiq_6.x.gemfile +7 -0
  11. data/gemfiles/sidekiq_7.x.gemfile +7 -0
  12. data/lib/sidekiq-status/client_middleware.rb +54 -1
  13. data/lib/sidekiq-status/redis_adapter.rb +18 -0
  14. data/lib/sidekiq-status/redis_client_adapter.rb +14 -0
  15. data/lib/sidekiq-status/server_middleware.rb +76 -17
  16. data/lib/sidekiq-status/sidekiq_extensions.rb +7 -0
  17. data/lib/sidekiq-status/storage.rb +26 -13
  18. data/lib/sidekiq-status/testing/inline.rb +10 -0
  19. data/lib/sidekiq-status/version.rb +1 -1
  20. data/lib/sidekiq-status/web.rb +158 -10
  21. data/lib/sidekiq-status/worker.rb +12 -5
  22. data/lib/sidekiq-status.rb +43 -10
  23. data/sidekiq-status.gemspec +9 -4
  24. data/spec/environment.rb +1 -0
  25. data/spec/lib/sidekiq-status/client_middleware_spec.rb +30 -13
  26. data/spec/lib/sidekiq-status/server_middleware_spec.rb +102 -30
  27. data/spec/lib/sidekiq-status/web_spec.rb +84 -0
  28. data/spec/lib/sidekiq-status/worker_spec.rb +21 -2
  29. data/spec/lib/sidekiq-status_spec.rb +158 -77
  30. data/spec/spec_helper.rb +104 -24
  31. data/spec/support/test_jobs.rb +84 -7
  32. data/web/sidekiq-status-single-web.png +0 -0
  33. data/web/sidekiq-status-web.png +0 -0
  34. data/web/views/status.erb +118 -0
  35. data/web/views/status_not_found.erb +6 -0
  36. data/web/views/statuses.erb +135 -17
  37. metadata +102 -16
  38. data/.travis.yml +0 -2
  39. data/CHANGELOG +0 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c4b871d99e1ab00e5786f91d61a3f0fc4c17087e
4
- data.tar.gz: 6909a5063a68811982f9e38580b1aeb146f5313f
2
+ SHA256:
3
+ metadata.gz: 7c4653623b20223d993b2c5f2ce82a1ee214083c1adb51530a76d82e4d2b3977
4
+ data.tar.gz: 6cee1ff677b1b964465e79facf5dd1e747c6747f9e99c7fd54e0330082a4b201
5
5
  SHA512:
6
- metadata.gz: 952fb2284e645179db9b5242bf4a9adcc6f8512b02335b60056b964711498c2c87d633e6f1b95116d576524ca4e3931db25fa51a22eab4d67cc41ac86e84099f
7
- data.tar.gz: 249eba2c142648813996c685d6bb5e03b08765580bd36139ce24f348ddc3a1cd19ec0569a9c398d9c08a2d948a2f64d7e537695df6d93b45869416a1546a16ef
6
+ metadata.gz: 99971994088169aa3ba0c21417557ce9d9fc072917b06c05a9cb7815f0405e331882f057d56079b41256f028c8248a780d9f74592a660a850959109a579519cc
7
+ data.tar.gz: aa7030ebbbb8161f5e9e81c3fac25357cac7bff62d810a3f785e9343c12c3a5839f94060d993aafc5381700b7803ca5e1de17dcb00762525eda6fa9a6bdb4725
@@ -0,0 +1,53 @@
1
+ name: Ruby CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+ branches:
9
+ - main
10
+
11
+ jobs:
12
+ test:
13
+
14
+ runs-on: ubuntu-latest
15
+
16
+ services:
17
+ redis:
18
+ image: redis
19
+ options: >-
20
+ --health-cmd "redis-cli ping"
21
+ --health-interval 10s
22
+ --health-timeout 5s
23
+ --health-retries 5
24
+ ports:
25
+ - 6379:6379
26
+
27
+ strategy:
28
+ fail-fast: false
29
+ matrix:
30
+ ruby-version:
31
+ - '2.7'
32
+ - '3.0'
33
+ - '3.1'
34
+ - '3.2'
35
+ gemfile:
36
+ - 'gemfiles/sidekiq_6.1.gemfile'
37
+ - 'gemfiles/sidekiq_6.x.gemfile'
38
+ - 'gemfiles/sidekiq_7.x.gemfile'
39
+
40
+ env:
41
+ BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
42
+
43
+ steps:
44
+ - uses: actions/checkout@v3
45
+ - name: Set up Ruby ${{ matrix.ruby-version }}
46
+ uses: ruby/setup-ruby@v1
47
+ with:
48
+ ruby-version: ${{ matrix.ruby-version }}
49
+ bundler-cache: true
50
+ rubygems: latest
51
+ - name: Run tests
52
+ run: |
53
+ bundle exec rake test
data/.gitignore CHANGED
@@ -1,7 +1,10 @@
1
+ gemfiles/*.lock
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
4
5
  .config
6
+ .rvmrc
7
+ .ruby-version
5
8
  .yardoc
6
9
  Gemfile.lock
7
10
  InstalledFiles
@@ -16,4 +19,4 @@ test/tmp
16
19
  test/version_tmp
17
20
  tmp
18
21
 
19
- .idea/
22
+ .idea/
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,17 @@
1
+ dependency_scanning:
2
+ image: docker:stable
3
+ variables:
4
+ DOCKER_DRIVER: overlay2
5
+ allow_failure: true
6
+ services:
7
+ - docker:stable-dind
8
+ script:
9
+ - export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
10
+ - docker run
11
+ --env DEP_SCAN_DISABLE_REMOTE_CHECKS="${DEP_SCAN_DISABLE_REMOTE_CHECKS:-false}"
12
+ --volume "$PWD:/code"
13
+ --volume /var/run/docker.sock:/var/run/docker.sock
14
+ "registry.gitlab.com/gitlab-org/security-products/dependency-scanning:$SP_VERSION" /code
15
+ artifacts:
16
+ reports:
17
+ dependency_scanning: gl-dependency-scanning-report.json
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise "sidekiq-5.x" do
2
+ gem "sidekiq", "~> 5"
3
+ end
4
+
5
+ appraise "sidekiq-6.1" do
6
+ gem "sidekiq", "~> 6.1"
7
+ end
8
+
9
+ appraise "sidekiq-6.x" do
10
+ gem "sidekiq", "~> 6"
11
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,41 @@
1
+ **Version 3.0.3**
2
+ - Fixes a Sidekiq warning about the deprecated `hmset` redis command (https://github.com/kenaniah/sidekiq-status/pull/37)
3
+
4
+ **Version 3.0.2**
5
+ - Avoids setting statuses for non-status jobs when an exception is thrown (https://github.com/kenaniah/sidekiq-status/pull/32)
6
+
7
+ **Version 3.0.1**
8
+ - Adds elapsed time and ETA to the job status page (https://github.com/kenaniah/sidekiq-status/pull/13)
9
+
10
+ **Version 3.0.0**
11
+ - Drops support for Sidekiq 5.x
12
+ - Adds support for Sidekiq 7.x
13
+ - Migrates from Travis CI to GitHub Actions
14
+
15
+ **Version 2.1.3**
16
+ - Fixes redis deprecation warnings (https://github.com/kenaniah/sidekiq-status/issues/11)
17
+
18
+ **Version 2.1.2**
19
+ - Casts values to strings when HTML-encoding
20
+
21
+ **Version 2.1.1**
22
+ - Ensures parameter outputs are properly HTML-encoded
23
+
24
+ **Version 2.1.0**
25
+ - Adds support for Sidekiq 6.2.2+ (https://github.com/mperham/sidekiq/issues/4955)
26
+
27
+ **Version 2.0.2**
28
+ - Fixes for dark mode theme
29
+
30
+ **Version 2.0.1**
31
+ - Adds support for dark mode to the job status page
32
+
33
+ **Version 2.0.0**
34
+ - Adds support for Ruby 2.7, 3.0
35
+ - Adds support for Sidekiq 6.x
36
+ - Removes support for Ruby 2.3, 2.4, 2.5
37
+ - Removes support for Sidekiq 3.x, 4.x
38
+
39
+ **Versions 1.1.4 and prior**
40
+
41
+ See https://github.com/utgarda/sidekiq-status/blob/master/CHANGELOG.md.
data/README.md CHANGED
@@ -1,47 +1,72 @@
1
1
  # Sidekiq::Status
2
- [![Gem Version](https://badge.fury.io/rb/sidekiq-status.png)](http://badge.fury.io/rb/sidekiq-status)
3
- [![Code Climate](https://codeclimate.com/github/utgarda/sidekiq-status.png)](https://codeclimate.com/github/utgarda/sidekiq-status)
4
- [![Build Status](https://secure.travis-ci.org/utgarda/sidekiq-status.png)](http://travis-ci.org/utgarda/sidekiq-status)
2
+ [![Gem Version](https://badge.fury.io/rb/sidekiq-status.svg)](https://badge.fury.io/rb/sidekiq-status)
3
+ [![Build Status](https://github.com/kenaniah/sidekiq-status/actions/workflows/ci.yaml/badge.svg)](https://github.com/kenaniah/sidekiq-status/actions/)
5
4
 
6
- An extension to [Sidekiq](http://github.com/mperham/sidekiq) message processing to track your jobs. Inspired
7
- by [resque-status](http://github.com/quirkey/resque-status) and mostly copying its features, using Sidekiq's middleware.
5
+ Sidekiq-status is an extension to [Sidekiq](https://github.com/mperham/sidekiq) that tracks information about your Sidekiq and provides a UI to that purpose. It was inspired by [resque-status](https://github.com/quirkey/resque-status).
6
+
7
+ Requires Ruby 2.6+ and Sidekiq 6.0+ or newer.
8
8
 
9
9
  ## Installation
10
10
 
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'sidekiq-status'
15
+ ```
16
+
17
+ Or install it yourself as:
18
+
19
+ ```bash
11
20
  gem install sidekiq-status
21
+ ```
22
+
23
+ ### Migrating to Version 3.x from 2.x
24
+
25
+ Version 3.0.0 adds support for Sidekiq 7.x, but drops support for Sidekiq 5.x. **You should be able to upgrade cleanly from version 2.x to 3.x provided you are running Sidekiq 6.x or newer.**
26
+
27
+ #### Migrating to Version 2.x from 1.x
28
+
29
+ Version 2.0.0 was published in order to add support for Ruby 3.0 and Sidekiq 6.x and to remove support for versions of both that are now end-of-life. **You should be able to upgrade cleanly from version 1.x to 2.x provided you are running Sidekiq 5.x or newer.**
30
+
31
+ Sidekiq-status version 1.1.4 provides support all the way back to Sidekiq 3.x and was maintained at https://github.com/utgarda/sidekiq-status/.
32
+
33
+ ## Setup Checklist
34
+
35
+ To get started:
12
36
 
13
- ## Usage
37
+ * [Configure](#configuration) the middleware
38
+ * (Optionally) add the [web interface](#adding-the-web-interface)
39
+ * (Optionally) enable support for [ActiveJob](#activejob-support)
14
40
 
15
41
  ### Configuration
16
42
 
17
- Configure your middleware chains, lookup [Middleware usage](https://github.com/mperham/sidekiq/wiki/Middleware)
18
- on Sidekiq wiki for more info.
43
+ To use, add sidekiq-status to the middleware chains. See [Middleware usage](https://github.com/mperham/sidekiq/wiki/Middleware)
44
+ on the Sidekiq wiki for more info.
19
45
 
20
46
  ``` ruby
21
47
  require 'sidekiq'
22
48
  require 'sidekiq-status'
23
49
 
24
50
  Sidekiq.configure_client do |config|
25
- config.client_middleware do |chain|
26
- chain.add Sidekiq::Status::ClientMiddleware
27
- end
51
+ # accepts :expiration (optional)
52
+ Sidekiq::Status.configure_client_middleware config, expiration: 30.minutes.to_i
28
53
  end
29
54
 
30
55
  Sidekiq.configure_server do |config|
31
- config.server_middleware do |chain|
32
- chain.add Sidekiq::Status::ServerMiddleware, expiration: 30.minutes # default
33
- end
34
- config.client_middleware do |chain|
35
- chain.add Sidekiq::Status::ClientMiddleware
36
- end
56
+ # accepts :expiration (optional)
57
+ Sidekiq::Status.configure_server_middleware config, expiration: 30.minutes.to_i
58
+
59
+ # accepts :expiration (optional)
60
+ Sidekiq::Status.configure_client_middleware config, expiration: 30.minutes.to_i
37
61
  end
38
62
  ```
39
63
 
40
- After that you can use your jobs as usual and only include `Sidekiq::Status::Worker` module if you want additional functionality of tracking progress and passing any data from job to client.
64
+ Include the `Sidekiq::Status::Worker` module in your jobs if you want the additional functionality of tracking progress and storing / retrieving job data.
41
65
 
42
66
  ``` ruby
43
67
  class MyJob
44
68
  include Sidekiq::Worker
69
+ include Sidekiq::Status::Worker # enables job status tracking
45
70
 
46
71
  def perform(*args)
47
72
  # your code goes here
@@ -49,14 +74,17 @@ class MyJob
49
74
  end
50
75
  ```
51
76
 
52
- To overwrite expiration on worker basis and don't use global expiration for all workers write a expiration method like this below:
77
+ Note: _only jobs that include `Sidekiq::Status::Worker`_ will have their statuses tracked.
78
+
79
+ To overwrite expiration on a per-worker basis, write an expiration method like the one below:
53
80
 
54
81
  ``` ruby
55
82
  class MyJob
56
83
  include Sidekiq::Worker
84
+ include Sidekiq::Status::Worker # enables job status tracking
57
85
 
58
86
  def expiration
59
- @expiration ||= 60*60*24*30 # 30 days
87
+ @expiration ||= 60 * 60 * 24 * 30 # 30 days
60
88
  end
61
89
 
62
90
  def perform(*args)
@@ -65,23 +93,61 @@ class MyJob
65
93
  end
66
94
  ```
67
95
 
68
- But keep in mind that such thing will store details of job as long as expiration is set, so it may charm your Redis storage/memory consumption. Because Redis stores all data in RAM.
96
+ The job status and any additional stored details will remain in Redis until the expiration time is reached. It is recommended that you find an expiration time that works best for your workload.
97
+
98
+ ### Expiration Times
99
+
100
+ As sidekiq-status stores information about jobs in Redis, it is necessary to set an expiration time for the data that gets stored. A default expiration time may be configured at the time the middleware is loaded via the `:expiration` parameter.
69
101
 
70
- ### Retrieving status
102
+ As explained above, the default expiration may also be overridden on a per-job basis by defining it within the job itself via a method called `#expiration`.
71
103
 
72
- Query for job status any time later:
104
+ The expiration time set will be used as the [Redis expire time](https://redis.io/commands/expire), which is also known as the TTL (time to live). Once the expiration time has passed, all information about the job's status and any custom data stored via sidekiq-status will disappear.
105
+
106
+ It is advised that you set the expiration time greater than the amount of time required to complete the job.
107
+
108
+ The default expiration time is 30 minutes.
109
+
110
+ ### Retrieving Status
111
+
112
+ You may query for job status any time up to expiration:
73
113
 
74
114
  ``` ruby
75
115
  job_id = MyJob.perform_async(*args)
76
- # :queued, :working, :complete or :failed , nil after expiry (30 minutes)
116
+ # :queued, :working, :complete, :failed or :interrupted, nil after expiry (30 minutes)
77
117
  status = Sidekiq::Status::status(job_id)
78
- Sidekiq::Status::queued? job_id
79
- Sidekiq::Status::working? job_id
80
- Sidekiq::Status::complete? job_id
81
- Sidekiq::Status::failed? job_id
118
+ Sidekiq::Status::queued? job_id
119
+ Sidekiq::Status::working? job_id
120
+ Sidekiq::Status::retrying? job_id
121
+ Sidekiq::Status::complete? job_id
122
+ Sidekiq::Status::failed? job_id
123
+ Sidekiq::Status::interrupted? job_id
124
+
82
125
  ```
126
+ Important: If you try any of the above status method after the expiration time, the result will be `nil` or `false`.
127
+
128
+ ### ActiveJob Support
83
129
 
84
- ### Tracking progress, saving and retrieveing data associated with job
130
+ This gem also supports ActiveJob jobs. Their status will be tracked automatically.
131
+
132
+ To also enable job progress tracking and data storage features, simply add the `Sidekiq::Status::Worker` module to your base class, like below:
133
+
134
+ ```ruby
135
+ # app/jobs/application_job.rb
136
+ class ApplicationJob < ActiveJob::Base
137
+ include Sidekiq::Status::Worker
138
+ end
139
+
140
+ # app/jobs/my_job.rb
141
+ class MyJob < ApplicationJob
142
+ def perform(*args)
143
+ # your code goes here
144
+ end
145
+ end
146
+ ```
147
+
148
+ ### Tracking Progress and Storing Data
149
+
150
+ sidekiq-status comes with a feature that allows you to track the progress of a job, as well as store and retrieve any custom data related to a job.
85
151
 
86
152
  ``` ruby
87
153
  class MyJob
@@ -93,13 +159,13 @@ class MyJob
93
159
 
94
160
  # the common idiom to track progress of your task
95
161
  total 100 # by default
96
- at 5, "Almost done"
162
+ at 5, "Almost done" # 5/100 = 5 % completion
97
163
 
98
164
  # a way to associate data with your job
99
165
  store vino: 'veritas'
100
166
 
101
- # a way of retrieving said data
102
- # remember that retrieved data is always is String|nil
167
+ # a way of retrieving stored data
168
+ # remember that retrieved data is always String|nil
103
169
  vino = retrieve :vino
104
170
  end
105
171
  end
@@ -112,28 +178,57 @@ Sidekiq::Status::at job_id #=> 5
112
178
  Sidekiq::Status::total job_id #=> 100
113
179
  Sidekiq::Status::message job_id #=> "Almost done"
114
180
  Sidekiq::Status::pct_complete job_id #=> 5
181
+ Sidekiq::Status::working_at job_id #=> 2718
182
+ Sidekiq::Status::update_time job_id #=> 2819
115
183
  ```
184
+
116
185
  ### Unscheduling
117
186
 
118
187
  ```ruby
119
188
  scheduled_job_id = MyJob.perform_in 3600
120
189
  Sidekiq::Status.cancel scheduled_job_id #=> true
121
- #doesn't cancel running jobs, this is more like unscheduling, therefore an alias:
190
+ # doesn't cancel running jobs, this is more like unscheduling, therefore an alias:
122
191
  Sidekiq::Status.unschedule scheduled_job_id #=> true
192
+
193
+ # returns false if invalid or wrong scheduled_job_id is provided
194
+ Sidekiq::Status.unschedule some_other_unschedule_job_id #=> false
195
+ Sidekiq::Status.unschedule nil #=> false
196
+ Sidekiq::Status.unschedule '' #=> false
197
+ # Note: cancel and unschedule are alias methods.
123
198
  ```
199
+ Important: If you schedule a job and then try any of the status methods after the expiration time, the result will be either `nil` or `false`. The job itself will still be in Sidekiq's scheduled queue and will execute normally. Once the job is started at its scheduled time, sidekiq-status' job metadata will once again be added back to Redis and you will be able to get status info for the job until the expiration time.
200
+
201
+ ### Deleting Job Status by Job ID
202
+
203
+ Job status and metadata will automatically be removed from Redis once the expiration time is reached. But if you would like to remove job information from Redis prior to the TTL expiration, `Sidekiq::Status#delete` will do just that. Note that this will also remove any metadata that was stored with the job.
204
+
205
+ ```ruby
206
+ # returns number of keys/jobs that were removed
207
+ Sidekiq::Status.delete(job_id) #=> 1
208
+ Sidekiq::Status.delete(bad_job_id) #=> 0
209
+ ```
210
+
211
+ ### Sidekiq Web Integration
124
212
 
125
- ### Sidekiq web integration
213
+ This gem provides an extension to Sidekiq's web interface with an index at `/statuses`.
126
214
 
127
- Sidekiq::Status also provides an extension to Sidekiq web interface with a `/statuses` page.
215
+ ![Sidekiq Status Web](web/sidekiq-status-web.png)
128
216
 
129
- Setup Sidekiq web interface according to Sidekiq documentation and add the Sidekiq::Status::Web require:
217
+ Information for an individual job may be found at `/statuses/:job_id`.
218
+
219
+ ![Sidekiq Status Web](web/sidekiq-status-single-web.png)
220
+
221
+ Note: _only jobs that include `Sidekiq::Status::Worker`_ will be reported in the web interface.
222
+
223
+ #### Adding the Web Interface
224
+
225
+ To use, setup the Sidekiq Web interface according to Sidekiq documentation and add the `Sidekiq::Status::Web` require:
130
226
 
131
227
  ``` ruby
132
228
  require 'sidekiq/web'
133
229
  require 'sidekiq-status/web'
134
230
  ```
135
231
 
136
-
137
232
  ### Testing
138
233
 
139
234
  Drawing analogy from [sidekiq testing by inlining](https://github.com/mperham/sidekiq/wiki/Testing#testing-workers-inline),
@@ -153,17 +248,29 @@ To use `sidekiq-status` inlining, require it too in your `{test,spec}_helper.rb`
153
248
 
154
249
  `require 'sidekiq-status/testing/inline'`
155
250
 
251
+ ## Contributing
252
+
253
+ Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
156
254
 
157
- ### Features coming
158
- * Stopping jobs by id
255
+ 1. Fork it
256
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
257
+ 3. Commit your changes along with test cases (`git commit -am 'Add some feature'`)
258
+ 4. If possible squash your commits to one commit if they all belong to same feature.
259
+ 5. Push to the branch (`git push origin my-new-feature`)
260
+ 6. Create new Pull Request.
159
261
 
160
262
  ## Thanks
263
+ * Pramod Shinde
264
+ * Kenaniah Cerny
161
265
  * Clay Allsopp
162
266
  * Andrew Korzhuev
163
267
  * Jon Moses
164
268
  * Wayne Hoover
165
269
  * Dylan Robinson
270
+ * Dmitry Novotochinov
271
+ * Mohammed Elalj
272
+ * Ben Sharpe
166
273
 
167
274
  ## License
168
- MIT License , see LICENSE for more details.
169
- © 2012 - 2014 Evgeniy Tsvigun
275
+ MIT License, see LICENSE for more details.
276
+ © 2012 - 2016 Evgeniy Tsvigun
data/Rakefile CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env rake
2
+
3
+ require "bundler/setup"
2
4
  require "bundler/gem_tasks"
3
5
 
4
6
  require 'rspec/core/rake_task'
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sidekiq", "~> 6.1"
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", "~> 6"
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", "~> 7"
6
+
7
+ gemspec path: "../"
@@ -1,15 +1,68 @@
1
+ require 'sidekiq/api'
2
+ JOB_CLASS = Sidekiq.constants.include?(:JobRecord) ? Sidekiq::JobRecord : Sidekiq::Job
3
+
1
4
  module Sidekiq::Status
2
5
  # Should be in the client middleware chain
3
6
  class ClientMiddleware
4
7
  include Storage
8
+
9
+ # Parameterized initialization, use it when adding middleware to client chain
10
+ # chain.add Sidekiq::Status::ClientMiddleware, :expiration => 60 * 5
11
+ # @param [Hash] opts middleware initialization options
12
+ # @option opts [Fixnum] :expiration ttl for complete jobs
13
+ def initialize(opts = {})
14
+ @expiration = opts[:expiration]
15
+ end
16
+
5
17
  # Uses msg['jid'] id and puts :queued status in the job's Redis hash
6
18
  # @param [Class] worker_class if includes Sidekiq::Status::Worker, the job gets processed with the plugin
7
19
  # @param [Array] msg job arguments
8
20
  # @param [String] queue the queue's name
9
21
  # @param [ConnectionPool] redis_pool optional redis connection pool
10
22
  def call(worker_class, msg, queue, redis_pool=nil)
11
- store_status msg['jid'], :queued, nil, redis_pool
23
+
24
+ # Determine the actual job class
25
+ klass = msg["args"][0]["job_class"] || worker_class rescue worker_class
26
+ job_class = if klass.is_a?(Class)
27
+ klass
28
+ elsif Module.const_defined?(klass)
29
+ Module.const_get(klass)
30
+ else
31
+ nil
32
+ end
33
+
34
+ # Store data if the job is a Sidekiq::Status::Worker
35
+ if job_class && job_class.ancestors.include?(Sidekiq::Status::Worker)
36
+ initial_metadata = {
37
+ jid: msg['jid'],
38
+ status: :queued,
39
+ worker: JOB_CLASS.new(msg, queue).display_class,
40
+ args: display_args(msg, queue)
41
+ }
42
+ store_for_id msg['jid'], initial_metadata, job_class.new.expiration || @expiration, redis_pool
43
+ end
44
+
12
45
  yield
46
+
47
+ end
48
+
49
+ def display_args(msg, queue)
50
+ job = JOB_CLASS.new(msg, queue)
51
+ return job.display_args.to_a.empty? ? "{}" : job.display_args.to_json
52
+ rescue Exception => e
53
+ # For Sidekiq ~> 2.7
54
+ return msg['args'].to_a.empty? ? nil : msg['args'].to_json
55
+ end
56
+ end
57
+
58
+ # Helper method to easily configure sidekiq-status client middleware
59
+ # whatever the Sidekiq version is.
60
+ # @param [Sidekiq] sidekiq_config the Sidekiq config
61
+ # @param [Hash] client_middleware_options client middleware initialization options
62
+ # @option client_middleware_options [Fixnum] :expiration ttl for complete jobs
63
+ def self.configure_client_middleware(sidekiq_config, client_middleware_options = {})
64
+ sidekiq_config.client_middleware do |chain|
65
+ chain.add Sidekiq::Status::ClientMiddleware, client_middleware_options
13
66
  end
14
67
  end
15
68
  end
@@ -0,0 +1,18 @@
1
+ # adapter for redis-rb client
2
+ class Sidekiq::Status::RedisAdapter
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def scan(**options, &block)
8
+ @client.scan_each(**options, &block)
9
+ end
10
+
11
+ def schedule_batch(key, options)
12
+ @client.zrangebyscore key, options[:start], options[:end], limit: [options[:offset], options[:limit]]
13
+ end
14
+
15
+ def method_missing(method, *args)
16
+ @client.send(method, *args)
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ # adapter for redis-client client
2
+ class Sidekiq::Status::RedisClientAdapter
3
+ def initialize(client)
4
+ @client = client
5
+ end
6
+
7
+ def schedule_batch(key, options)
8
+ @client.zrange(key, options[:start], options[:end], :byscore, :limit, options[:offset], options[:limit])
9
+ end
10
+
11
+ def method_missing(method, *args)
12
+ @client.send(method, *args)
13
+ end
14
+ end