robin-compat 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.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +28 -0
  3. data/COPYING +674 -0
  4. data/LICENSE +165 -0
  5. data/README.md +258 -0
  6. data/exe/sidekiq +5 -0
  7. data/exe/sidekiqswarm +6 -0
  8. data/lib/robin/compat/cron_line.rb +46 -0
  9. data/lib/robin/compat/version.rb +8 -0
  10. data/lib/robin-compat.rb +3 -0
  11. data/lib/sidekiq/active_job.rb +87 -0
  12. data/lib/sidekiq/api.rb +352 -0
  13. data/lib/sidekiq/batch.rb +175 -0
  14. data/lib/sidekiq/capsule.rb +38 -0
  15. data/lib/sidekiq/cli.rb +70 -0
  16. data/lib/sidekiq/client.rb +84 -0
  17. data/lib/sidekiq/config.rb +190 -0
  18. data/lib/sidekiq/cron/job.rb +555 -0
  19. data/lib/sidekiq/cron/poller.rb +47 -0
  20. data/lib/sidekiq/cron/schedule_loader.rb +41 -0
  21. data/lib/sidekiq/cron/web.rb +7 -0
  22. data/lib/sidekiq/cron.rb +146 -0
  23. data/lib/sidekiq/deploy.rb +62 -0
  24. data/lib/sidekiq/iterable_job.rb +55 -0
  25. data/lib/sidekiq/job.rb +207 -0
  26. data/lib/sidekiq/job_retry.rb +23 -0
  27. data/lib/sidekiq/limiter.rb +144 -0
  28. data/lib/sidekiq/logger.rb +25 -0
  29. data/lib/sidekiq/middleware/chain.rb +27 -0
  30. data/lib/sidekiq/middleware/current_attributes.rb +19 -0
  31. data/lib/sidekiq/middleware/modules.rb +15 -0
  32. data/lib/sidekiq/middleware/server/statsd.rb +3 -0
  33. data/lib/sidekiq/periodic.rb +94 -0
  34. data/lib/sidekiq/pro/metrics.rb +134 -0
  35. data/lib/sidekiq/pro/web.rb +5 -0
  36. data/lib/sidekiq/rails.rb +18 -0
  37. data/lib/sidekiq/redis_connection.rb +166 -0
  38. data/lib/sidekiq/scheduler.rb +4 -0
  39. data/lib/sidekiq/testing/inline.rb +5 -0
  40. data/lib/sidekiq/testing.rb +188 -0
  41. data/lib/sidekiq/web.rb +112 -0
  42. data/lib/sidekiq-cron.rb +4 -0
  43. data/lib/sidekiq-ent/periodic/testing.rb +4 -0
  44. data/lib/sidekiq-ent/web.rb +5 -0
  45. data/lib/sidekiq-ent.rb +90 -0
  46. data/lib/sidekiq-pro.rb +16 -0
  47. data/lib/sidekiq-scheduler/config.rb +65 -0
  48. data/lib/sidekiq-scheduler/manager.rb +34 -0
  49. data/lib/sidekiq-scheduler/poller.rb +46 -0
  50. data/lib/sidekiq-scheduler/redis_manager.rb +108 -0
  51. data/lib/sidekiq-scheduler/schedule.rb +65 -0
  52. data/lib/sidekiq-scheduler/scheduled_job.rb +80 -0
  53. data/lib/sidekiq-scheduler/scheduler.rb +218 -0
  54. data/lib/sidekiq-scheduler/utils.rb +120 -0
  55. data/lib/sidekiq-scheduler/version.rb +6 -0
  56. data/lib/sidekiq-scheduler/web.rb +7 -0
  57. data/lib/sidekiq-scheduler.rb +48 -0
  58. data/lib/sidekiq.rb +222 -0
  59. metadata +121 -0
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,258 @@
1
+ # robin-compat
2
+
3
+ A Sidekiq-compatible interface for [Robin](https://github.com/sferik/robin):
4
+ run your Sidekiq app on Robin without changing its code. robin-compat
5
+ provides Sidekiq's interface on top of Robin: job classes,
6
+ `sidekiq_options`, the client, configuration, `config/sidekiq.yml`, the
7
+ `sidekiq` command, the testing API, the data API, `Sidekiq::Web`, and
8
+ Sidekiq Pro's and Enterprise's APIs. Robin reads and writes Sidekiq's
9
+ Redis data where it is, so you switch without draining any queue.
10
+
11
+ Robin and robin-compat are free software under the LGPL-3.0, including
12
+ the features Sidekiq sells as Pro and Enterprise.
13
+
14
+ It matches Sidekiq 7.0 to 8.1, with Pro and Enterprise, and
15
+ `Sidekiq::VERSION` is 8.1.0. Apps written for Sidekiq 7 say so (see
16
+ [Coming from Sidekiq 7](#coming-from-sidekiq-7)). Where 8.0 and 8.1
17
+ differ, it follows 8.0,
18
+ whose rules are looser: a job with `retry_for` also stops at its retry
19
+ count, and `perform_inline` takes arguments JSON would change (a Symbol
20
+ becomes a String) where 8.1 raises. It also takes the forms of Sidekiq 7
21
+ that 8.0 dropped: error handlers with two parameters (`->(error,
22
+ context)`), `Sidekiq::Work` read as a Hash (`work["queue"]`), and testing
23
+ modes nested in each other's blocks.
24
+
25
+ > robin-compat is an independent project. It is not affiliated with,
26
+ > sponsored by or endorsed by Contributed Systems LLC or Mike Perham.
27
+ > Sidekiq, Sidekiq Pro and Sidekiq Enterprise are trademarks of
28
+ > Contributed Systems LLC, and are named here only to say what
29
+ > robin-compat is compatible with. It contains none of Sidekiq's code.
30
+
31
+ ## Upgrading from Sidekiq
32
+
33
+ ### 1. Change your Gemfile
34
+
35
+ ```diff
36
+ -gem "sidekiq"
37
+ +gem "robin-compat"
38
+ ```
39
+
40
+ If you use Sidekiq Pro or Enterprise, remove them and their gem source
41
+ too:
42
+
43
+ ```diff
44
+ -source "https://gems.contribsys.com/" do
45
+ - gem "sidekiq-pro"
46
+ - gem "sidekiq-ent"
47
+ -end
48
+ ```
49
+
50
+ If you use sidekiq-cron or sidekiq-scheduler, remove them too, since they
51
+ depend on the real Sidekiq:
52
+
53
+ ```diff
54
+ -gem "sidekiq-cron"
55
+ -gem "sidekiq-scheduler"
56
+ ```
57
+
58
+ robin-compat provides their interfaces, and runs the jobs they saved in
59
+ Redis where they are. If your schedules use natural language ("every day
60
+ at five", or durations like "2 hours and 30 minutes"), add the fugit gem
61
+ to your Gemfile, which both brought in.
62
+
63
+ Then `bundle install`. That's the only change to your app. Everything else
64
+ keeps working as written:
65
+
66
+ - `require "sidekiq"`, `"sidekiq/web"`, `"sidekiq/api"`,
67
+ `"sidekiq/testing"`, `"sidekiq-pro"`, `"sidekiq-ent"`
68
+ - job classes (`include Sidekiq::Job`, `Sidekiq::Worker`), `sidekiq_options`,
69
+ `sidekiq_retry_in`, `sidekiq_retries_exhausted`, `perform_async`,
70
+ `perform_in`, `perform_at`, `perform_bulk`, `set`, `perform_inline`
71
+ - `Sidekiq.configure_server` and `configure_client` blocks, server and
72
+ client middleware, error and death handlers, lifecycle events
73
+ - `config/sidekiq.yml` (queues and weights, concurrency, capsules,
74
+ environment sections, other settings) and `bundle exec sidekiq`
75
+ - Active Job's `:sidekiq` adapter, and `sidekiq_options` on Active Job
76
+ classes
77
+ - `Sidekiq::Testing` (fake, inline and disabled modes, `jobs`, `drain`)
78
+ - `Sidekiq::Stats`, `Queue`, `ScheduledSet`, `RetrySet`, `DeadSet`,
79
+ `ProcessSet`, `Workers`
80
+ - `Sidekiq::Deploy.mark!` (`require "sidekiq/deploy"`): the dashboard's
81
+ metrics charts show each deploy
82
+ - `mount Sidekiq::Web => "/sidekiq"`, with `Sidekiq::Web.use` middleware
83
+ (basic auth, say) and Enterprise's `Sidekiq::Web.authorize`
84
+ - `Thread.current[:sidekiq_capsule]` in jobs, and
85
+ `Sidekiq::Client#normalize_item`
86
+ - Pro: batches and their callbacks (with `invalidate_all` and
87
+ `invalidate_jids`), `reliable_push!`, `super_fetch!`, expiring jobs,
88
+ queue pausing, StatsD metrics (`config.dogstatsd`)
89
+ - Enterprise: rate limiters, unique jobs, periodic jobs, encryption,
90
+ leader election, `sidekiqswarm`
91
+ - sidekiq-cron: `Sidekiq::Cron::Job` (`create`, `find`, `all`, `destroy`,
92
+ `enable!`, `disable!`, `enqueue!`, `load_from_hash!`...), namespaces,
93
+ `Sidekiq::Cron.configure`, and `config/schedule.yml`, loaded as each
94
+ server starts. Jobs sidekiq-cron and Robin enqueue claim each tick in
95
+ Redis the same way, so the two can run side by side while you switch.
96
+ - sidekiq-scheduler: the `:scheduler:` section of `config/sidekiq.yml`
97
+ (`cron`, `every`, `interval`, `at` and `in` schedules, `enabled`,
98
+ `dynamic`, `listened_queues_only`), `Sidekiq.schedule=`,
99
+ `Sidekiq.set_schedule`, `Sidekiq.get_schedule`,
100
+ `Sidekiq::Scheduler.reload_schedule!` and its other methods, and the
101
+ enabled states its web UI toggles. Robin claims each cron tick in Redis
102
+ as sidekiq-scheduler does, so the two can run side by side too.
103
+
104
+ ### Coming from Sidekiq 7
105
+
106
+ Robin needs Ruby 3.4 or later, so upgrade Ruby first if your app runs an
107
+ older one. Then tell robin-compat which Sidekiq your app was written for,
108
+ in an initializer every process loads (web servers and consoles push
109
+ jobs too, not just the `sidekiq` processes):
110
+
111
+ ```ruby
112
+ # config/initializers/sidekiq.rb
113
+ Sidekiq.compatible_with = "7.2"
114
+ ```
115
+
116
+ It takes any version from 7.0 to 8.1 ("7.2.4" works too) and decides
117
+ what depends on it:
118
+
119
+ - Before 8.0, jobs record their times (`created_at`, `enqueued_at`,
120
+ `failed_at` and `retried_at`) in epoch seconds rather than
121
+ milliseconds, and Active Job jobs name Sidekiq 7's wrapper class
122
+ (`ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper`), so Sidekiq 7
123
+ can run and show Robin's jobs if you go back. Pro's StatsD metrics
124
+ have no `sidekiq.` prefix, as in Pro 7 (which leaves it to the client's
125
+ `namespace`).
126
+ - Before 7.3, a Hash of arguments in a sidekiq-scheduler schedule has its
127
+ keys symbolized, as sidekiq-scheduler 5 does with Active Support.
128
+ - Before 7.1, `push_bulk` returns only the jids of the jobs it pushed, as
129
+ Sidekiq 7.0 does, not a `nil` for each job middleware stopped.
130
+
131
+ The forms Sidekiq 8 dropped work whatever the version: error handlers
132
+ with two parameters, `Sidekiq::Work` read as a Hash, and nested testing
133
+ modes.
134
+
135
+ ### 2. Deploy
136
+
137
+ 1. Stop your Sidekiq processes as you normally do (`TERM`) and let them
138
+ exit: Sidekiq pushes the jobs they were running back to their queues.
139
+ 2. Deploy.
140
+ 3. Start Robin with the same command (`bundle exec sidekiq -C
141
+ config/sidekiq.yml`, or `sidekiqswarm` with Enterprise). Your Procfile,
142
+ systemd unit or Kubernetes manifest doesn't change.
143
+
144
+ Robin picks up where Sidekiq left off: enqueued jobs, scheduled jobs,
145
+ retries and dead jobs, paused queues, and the processed and failed
146
+ totals and their history. Run Sidekiq and Robin one after the other,
147
+ not side by side.
148
+
149
+ Robin keeps Sidekiq's data formats, so you can also go back: stop Robin,
150
+ deploy Sidekiq again, and Sidekiq picks up the same way (the interop tests
151
+ check this with the real Sidekiq). Active Job jobs name Sidekiq's wrapper
152
+ class for this reason. Features Sidekiq doesn't have, such as Robin's
153
+ encrypted or debounced jobs, are of course lost on Sidekiq.
154
+
155
+ ### 3. That's it
156
+
157
+ Your app runs on Robin. Its dashboard is where Sidekiq's was, and the
158
+ processes show up in it (and in Sidekiq's own API, which reads the same
159
+ records) as they did before.
160
+
161
+ ## What carries over, and what doesn't
162
+
163
+ These stay where Sidekiq keeps them, and Robin uses them as they are:
164
+ queues, the schedule, retry and dead sets, the paused queues, the job
165
+ totals, and process records. Each job keeps its arguments, retry count,
166
+ error details and backtrace.
167
+
168
+ State that Sidekiq Pro and Enterprise keep in their own formats doesn't
169
+ carry over:
170
+
171
+ - **Batches in progress.** Their jobs still run, but their callbacks won't
172
+ fire. Let running batches finish before switching.
173
+ - **Unique job locks.** A duplicate of a job pushed just before the switch
174
+ may be accepted once.
175
+ - **Rate limiter counts** start from zero.
176
+ - **Periodic jobs** are registered again from your configuration; their
177
+ past runs aren't shown.
178
+ - **Encrypted jobs** (Enterprise) can't be decrypted by Robin, which
179
+ encrypts differently. Let them finish before switching.
180
+ - **Historical metrics** charts, and the deploy marks on them, start
181
+ fresh.
182
+ - **Jobs held by a crashed Sidekiq Pro process** (super_fetch) aren't
183
+ recovered by Robin. Stop Sidekiq cleanly, or let Sidekiq Pro recover
184
+ them before switching.
185
+
186
+ ## Differences you might notice
187
+
188
+ - **Robin's dashboard** replaces Sidekiq's. Extensions written for
189
+ Sidekiq's web UI (`Sidekiq::Web.register`) don't load on it; Robin logs
190
+ a warning and carries on. Robin's own extension API is
191
+ `Robin::Web.register`.
192
+ - **Fetching is always reliable.** Jobs a crashed process was running are
193
+ put back on their queues, without Sidekiq Pro's `super_fetch!`.
194
+ - **`log_level`** sets the level of a job's start and done lines, not the
195
+ logger's level while the job runs.
196
+ - **Deploy marks.** A label marked in the last hour isn't marked again
197
+ (Sidekiq's window is a minute), and marks are kept as long as Robin's
198
+ hourly metrics rather than for 90 days.
199
+ - **Environment variables.** `RAILS_ENV` is checked before `APP_ENV`;
200
+ Sidekiq checks `APP_ENV` first.
201
+ - **sidekiq-cron jobs** aren't shown on Robin's dashboard, which has no
202
+ page for them (`require "sidekiq/cron/web"` logs a warning). Only Robin's
203
+ leader polls for them, where sidekiq-cron polls on every process, so
204
+ `cron_poll_process_count` is accepted and ignored. Schedules are read by
205
+ Robin's cron parser, which takes fugit's additions to cron syntax as
206
+ well (`MON#2`, `FRI#L`, `-2` and `TUE%2`).
207
+ - **sidekiq-scheduler jobs** aren't shown on Robin's dashboard either
208
+ (`require "sidekiq-scheduler/web"` logs a warning). Only Robin's leader
209
+ pushes them, so `every`, `interval` and `in` jobs run once across your
210
+ processes, where sidekiq-scheduler runs them once on each. `every` and
211
+ `interval` jobs run at the multiples of their interval since the Unix
212
+ epoch (`every: "15m"` at :00, :15, :30 and :45), not counting from when
213
+ a process started, so their `first_in` and `first_at` options are
214
+ ignored. `in` counts from when the leader started. `at` times are read
215
+ with `Time.parse` (Chronic's phrases aren't). `listened_queues_only`
216
+ pushes the jobs whose queue some live process takes.
217
+ `rufus_scheduler_options` are accepted and ignored.
218
+
219
+ Retries follow Sidekiq's rules: up to 25 by default, `count ** 4 + 15`
220
+ seconds apart plus jitter, and `sidekiq_retry_in` counts from 0.
221
+
222
+ ## What you get
223
+
224
+ Everything Robin does, including these built-in features: reliable fetch
225
+ with crash recovery, batches, rate limiters, unique, debounced, expiring
226
+ and encrypted jobs, periodic jobs, iterable jobs, progress and
227
+ cancellation, a live dashboard, metrics, OpenTelemetry tracing, and swarm
228
+ mode with rolling restarts. On the same machine, Robin runs about 5
229
+ times as many jobs per second as Sidekiq, and about 6 times as many with
230
+ a millisecond of network latency; scheduled jobs and retries coming due
231
+ reach their queues about 60 times as fast, and a crash loses no jobs
232
+ ([benchmarks](https://github.com/sferik/robin/blob/main/bench/RESULTS.md)).
233
+
234
+ Once you're on Robin, you can move to its own names whenever you like
235
+ (`include Robin::Job`, `robin_options`, `Robin.configure_server`): both
236
+ work side by side.
237
+
238
+ ## Testing robin-compat
239
+
240
+ ```sh
241
+ bundle exec rake test
242
+ ```
243
+
244
+ The interop tests run the real Sidekiq gem (from `test/interop/Gemfile`)
245
+ against the same Redis as Robin: Robin runs what Sidekiq pushed,
246
+ scheduled, retried and killed; Sidekiq's API reads what Robin wrote and
247
+ acts on it; and Sidekiq runs what Robin pushed. They need a database
248
+ they may flush:
249
+
250
+ ```sh
251
+ ROBIN_INTEROP_REDIS_URL=redis://127.0.0.1:6379/10 bundle exec ruby -Itest -Ilib test/interop_test.rb
252
+ ```
253
+
254
+ They run Sidekiq 8.1, or with `ROBIN_INTEROP_GEMFILE` set to
255
+ `test/interop/sidekiq-8.0/Gemfile` (or `sidekiq-7.3`, `sidekiq-7.2`,
256
+ `sidekiq-7.1` or `sidekiq-7.0`), that version, with robin-compat set to
257
+ follow it. Install those gems first, with `BUNDLE_GEMFILE` set to the
258
+ same Gemfile and `bundle install`.
data/exe/sidekiq ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "sidekiq/cli"
5
+ Sidekiq::CLI.instance.run(ARGV)
data/exe/sidekiqswarm ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Sidekiq Enterprise's multi-process command (see Sidekiq::CLI#swarm).
5
+ require "sidekiq/cli"
6
+ Sidekiq::CLI.instance.swarm(ARGV)
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "robin"
4
+
5
+ module Robin
6
+ module Compat
7
+ # Cron lines as sidekiq-cron and sidekiq-scheduler take them (through
8
+ # fugit): five fields, or six with seconds first, and an optional time
9
+ # zone at the end ("0 22 * * 1-5 America/Chicago"). Without one, a line
10
+ # is in Rails' Time.zone, else the system's.
11
+ module CronLine
12
+ # A time zone at the end of a cron line: an IANA name
13
+ # ("America/Chicago", "Etc/GMT+5"), UTC, GMT, Z or an offset ("+05:30").
14
+ ZONE = %r{\A([A-Za-z][\w+-]*(/[\w+-]+)+|UTC|GMT|Z|[+-]\d\d:\d\d)\z}
15
+ private_constant :ZONE
16
+
17
+ class << self
18
+ # The line as a Robin::Cron.
19
+ def parse(line)
20
+ *fields, last = line.split
21
+ return Cron.new(fields.join(" "), tz: last) if ZONE.match?(last)
22
+
23
+ Cron.new(line, tz: default_zone)
24
+ end
25
+
26
+ # Rails' Time.zone's name, or nil (the system's zone).
27
+ def default_zone = rails_time_zone&.tzinfo&.name
28
+
29
+ # Fugit, or nil when the app doesn't have the gem.
30
+ def fugit
31
+ require "fugit"
32
+ Fugit
33
+ rescue LoadError
34
+ nil
35
+ end
36
+
37
+ private
38
+
39
+ # Rails' Time.zone, or nil without Active Support's.
40
+ # mutant:disable -- loaded for every test at once under mutant (the
41
+ # tests stub this to cover both)
42
+ def rails_time_zone = (::Time.respond_to?(:zone) && ::Time.zone) || nil
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Robin
4
+ module Compat
5
+ # Released in lockstep with the robin gem.
6
+ VERSION = "0.1.2"
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+ require "robin/active_job"
5
+
6
+ module Sidekiq
7
+ module ActiveJob
8
+ # Active Job jobs Sidekiq pushed name this class: they run on Robin's
9
+ # wrapper.
10
+ Wrapper = Robin::ActiveJob::Wrapper
11
+ end
12
+
13
+ module Job
14
+ # sidekiq_options on Active Job classes.
15
+ module Options
16
+ def self.included(base)
17
+ base.include(Robin::ActiveJob::Options)
18
+ base.extend(ClassMethods)
19
+ end
20
+
21
+ # sidekiq_options, and the retry hooks (Robin runs an Active Job
22
+ # job's from the class it wraps).
23
+ module ClassMethods
24
+ def sidekiq_options(opts = {}) = robin_options(Job.stringify(opts))
25
+
26
+ def get_sidekiq_options = Job.default_options.merge(robin_options)
27
+
28
+ # The block gets (retry count, exception, job): the count is 0 the
29
+ # first time the job fails.
30
+ def sidekiq_retry_in(&block)
31
+ @sidekiq_retry_in_block = block
32
+ @robin_retry_in_block = ->(failures, exception, job) { Job.retry_delay(block.call(failures - 1, exception, job)) }
33
+ end
34
+
35
+ # The block gets (job, exception); returning :discard skips the
36
+ # dead set.
37
+ def sidekiq_retries_exhausted(&block)
38
+ @robin_retries_exhausted_block = block
39
+ end
40
+
41
+ def sidekiq_retry_in_block = inherited_hook(:@sidekiq_retry_in_block)
42
+
43
+ def robin_retry_in_block = inherited_hook(:@robin_retry_in_block)
44
+
45
+ def robin_retries_exhausted_block = inherited_hook(:@robin_retries_exhausted_block)
46
+
47
+ def sidekiq_retries_exhausted_block = robin_retries_exhausted_block
48
+
49
+ private
50
+
51
+ # The hook this class or the closest superclass set.
52
+ def inherited_hook(name) = ancestors.filter_map { |klass| klass.instance_variable_get(name) }.first
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ module ActiveJob
59
+ module QueueAdapters
60
+ # The adapter Rails ships under this name (registered for autoloading,
61
+ # or already loaded; Rails 8.2 has none) gives way to Robin's, without
62
+ # being loaded.
63
+ (constants(false) & [:SidekiqAdapter]).each { |name| remove_const(name) }
64
+
65
+ # The :sidekiq adapter, as Active Job names it, is Robin's adapter.
66
+ class SidekiqAdapter < RobinAdapter
67
+ # The wrapper class name Sidekiq used before it named its own.
68
+ JobWrapper = Robin::ActiveJob::Wrapper
69
+
70
+ # Jobs pushed inside a transaction are sent after it commits.
71
+ def enqueue_after_transaction_commit? = true
72
+
73
+ private
74
+
75
+ # Jobs name Sidekiq's wrapper class, as Sidekiq's adapter pushes
76
+ # them, so Sidekiq processes can run them too (Robin's run them on
77
+ # its own wrapper, an alias): Sidekiq 8's, or for apps written for
78
+ # Sidekiq 7, the one Sidekiq 7 knows (which Sidekiq 8 knows too).
79
+ def payload(job, timestamp) = super.merge({"class" => Sidekiq.compatible_before?("8.0") ? JOB_WRAPPER_7 : JOB_WRAPPER})
80
+
81
+ JOB_WRAPPER = "Sidekiq::ActiveJob::Wrapper"
82
+ JOB_WRAPPER_7 = "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
83
+ end
84
+ end
85
+ end
86
+
87
+ ActiveSupport.on_load(:active_job) { include Sidekiq::Job::Options }