sidekiq-cron 1.10.0 → 2.3.1

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +81 -3
  3. data/Gemfile +3 -0
  4. data/README.md +283 -38
  5. data/lib/sidekiq/cron/job.rb +277 -182
  6. data/lib/sidekiq/cron/launcher.rb +2 -5
  7. data/lib/sidekiq/cron/locales/de.yml +15 -6
  8. data/lib/sidekiq/cron/locales/en.yml +14 -14
  9. data/lib/sidekiq/cron/locales/es.yml +22 -0
  10. data/lib/sidekiq/cron/locales/id.yml +22 -0
  11. data/lib/sidekiq/cron/locales/it.yml +15 -16
  12. data/lib/sidekiq/cron/locales/ja.yml +14 -10
  13. data/lib/sidekiq/cron/locales/pt.yml +14 -14
  14. data/lib/sidekiq/cron/locales/ru.yml +15 -7
  15. data/lib/sidekiq/cron/locales/zh-CN.yml +14 -11
  16. data/lib/sidekiq/cron/namespace.rb +43 -0
  17. data/lib/sidekiq/cron/poller.rb +12 -13
  18. data/lib/sidekiq/cron/schedule_loader.rb +51 -16
  19. data/lib/sidekiq/cron/support.rb +4 -30
  20. data/lib/sidekiq/cron/version.rb +1 -1
  21. data/lib/sidekiq/cron/views/cron.erb +97 -92
  22. data/lib/sidekiq/cron/views/cron_show.erb +87 -82
  23. data/lib/sidekiq/cron/views/legacy/cron.erb +113 -0
  24. data/lib/sidekiq/cron/views/legacy/cron_show.erb +92 -0
  25. data/lib/sidekiq/cron/web.rb +21 -2
  26. data/lib/sidekiq/cron/web_extension.rb +99 -31
  27. data/lib/sidekiq/cron.rb +73 -5
  28. data/lib/sidekiq/options.rb +9 -7
  29. data/lib/sidekiq-cron.rb +6 -0
  30. data/sidekiq-cron.gemspec +7 -6
  31. metadata +40 -27
  32. data/test/integration/performance_test.rb +0 -46
  33. data/test/models/person.rb +0 -21
  34. data/test/test_helper.rb +0 -93
  35. data/test/unit/fixtures/schedule_array.yml +0 -13
  36. data/test/unit/fixtures/schedule_erb.yml +0 -6
  37. data/test/unit/fixtures/schedule_hash.yml +0 -12
  38. data/test/unit/fixtures/schedule_string.yml +0 -1
  39. data/test/unit/job_test.rb +0 -1386
  40. data/test/unit/launcher_test.rb +0 -33
  41. data/test/unit/poller_test.rb +0 -144
  42. data/test/unit/schedule_loader_test.rb +0 -58
  43. data/test/unit/web_extension_test.rb +0 -155
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f484cd18cea7b3b00d83b82c5ae6ca7e2c831b22ed4d00cd77a19cfc9491b49d
4
- data.tar.gz: efe62165d5c2b02b4ee2ce7f1ac877cda1bd06d550f4554fbb340b491f8d1319
3
+ metadata.gz: 8e088e2cce323ffd21798bdaf3818e6b49bb5dd7ae98e4005899df45603c9529
4
+ data.tar.gz: b3771467c144d7ac4de9f30f00f15fdff4fba7750ee3b0e74df09a3fd88215ba
5
5
  SHA512:
6
- metadata.gz: a945824e3d39d8f54a33c68eec458f3dee68be2cd43ddf697b7fdd568afff291ff62ee1ec8cf2bbc05e26ddfa6e8230c78a953499b59b6844ca60b37f55ffc99
7
- data.tar.gz: 2aa57d5d1e2b4ba98199bf79e13d49b7726478650ef46efa92871e0c13b51608829bc66159d8ed5a2cd58f4b6d5dcb3a6e87329795e604309fc60ba85b583758
6
+ metadata.gz: 25cf2dc9507fc3b8a8e65b9a0e14bbc52449be13c4e7083f059b3cd1d07b0bac8006a3db5c77d37a18372cce12241e99c46f8d370c39ed8bcfc3a9791429c013
7
+ data.tar.gz: 60126836a12d6d1df44f76f2fdf4c473b1ec4e5dc723582e092cb6ac5e47178cffa7bf048b2afa7bf2fcf7f1428dd9ea1f4a8eb5ffe1c9b868a733fed12d0d0a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,84 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## 2.3.1
6
+
7
+ - Fix manually launch enqueue job not working from web UI (https://github.com/sidekiq-cron/sidekiq-cron/pull/564)
8
+
9
+ ## 2.3.0
10
+
11
+ - **WARNING** The default value for `available_namespaces` has changed from `nil` to `[default_namespace]`. Please refer to the [migration guide](https://github.com/sidekiq-cron/sidekiq-cron/blob/master/README.md#migrating-to-23) for further details (https://github.com/sidekiq-cron/sidekiq-cron/pull/552)
12
+ - Fix deprecation warning for using raw params in WEB extension (https://github.com/sidekiq-cron/sidekiq-cron/pull/547)
13
+ - Allow for sidekiq embedded configuration (https://github.com/sidekiq-cron/sidekiq-cron/pull/549)
14
+ - Load schedule file within sidekiq callback (https://github.com/sidekiq-cron/sidekiq-cron/pull/550)
15
+ - Fix missing default namespace if namespaces are explicitly provided (https://github.com/sidekiq-cron/sidekiq-cron/pull/551)
16
+
17
+ ## 2.2.0
18
+
19
+ - Add support for Sidekiq v8 (https://github.com/sidekiq-cron/sidekiq-cron/pull/531, https://github.com/sidekiq-cron/sidekiq-cron/pull/538)
20
+ - Always show parsed cron expression in web UI (https://github.com/sidekiq-cron/sidekiq-cron/pull/535)
21
+ - Add fallback to .yaml extension (https://github.com/sidekiq-cron/sidekiq-cron/pull/534)
22
+ - Refactor constantize helper to use Object.const_get (https://github.com/sidekiq-cron/sidekiq-cron/pull/541)
23
+ - Allow testing of schedule by library consumers (https://github.com/sidekiq-cron/sidekiq-cron/pull/543)
24
+
25
+ ## 2.1.0
26
+
27
+ - Add `available_namespaces` configuration option (https://github.com/sidekiq-cron/sidekiq-cron/pull/524)
28
+ - I18n fixes and enhancements (https://github.com/sidekiq-cron/sidekiq-cron/pull/520, https://github.com/sidekiq-cron/sidekiq-cron/pull/522)
29
+
30
+ ## 2.0.1
31
+
32
+ - Fix usage of ActiveJob::Base.queue_name (https://github.com/sidekiq-cron/sidekiq-cron/pull/517)
33
+ - Fix: Add quotes to Japanese translations containing multi-byte symbols (https://github.com/sidekiq-cron/sidekiq-cron/pull/515)
34
+
35
+ ## 2.0.0
36
+
37
+ Sidekiq-Cron v2 is here! In this release we refactored some internals, plus:
38
+
39
+ - Review web UI translations for all available locales (https://github.com/sidekiq-cron/sidekiq-cron/pull/506)
40
+ - Fix detection of ActiveJob in Sidekiq v7.3.3+ (https://github.com/sidekiq-cron/sidekiq-cron/pull/510)
41
+ - Add retry job configuration option to set Sidekiq retry job option (https://github.com/sidekiq-cron/sidekiq-cron/pull/509)
42
+
43
+ Please take a look to the RC1 and RC2 changes too if you are coming from the v1.X series.
44
+
45
+ ## 2.0.0.rc2
46
+
47
+ - Remove support for Sidekiq < 6.5 (https://github.com/sidekiq-cron/sidekiq-cron/pull/480)
48
+ - Require at least Fugit >= 1.11.1 (https://github.com/sidekiq-cron/sidekiq-cron/pull/475)
49
+ - Update how Redis values are stored on save (https://github.com/sidekiq-cron/sidekiq-cron/pull/479)
50
+ - Web extension: Add compatibility with Sidekiq 7.3+ and remove inline styles (https://github.com/sidekiq-cron/sidekiq-cron/pull/480)
51
+ - Remove support for old Redis (< 4.2) (https://github.com/sidekiq-cron/sidekiq-cron/pull/490)
52
+ - Ensure date_as_argument option can be set from true to false in Sidekiq Cron jobs (https://github.com/sidekiq-cron/sidekiq-cron/pull/485)
53
+ - Rename `enque!` to `enqueue!` (https://github.com/sidekiq-cron/sidekiq-cron/pull/494)
54
+ - Refactor gem configuration module (https://github.com/sidekiq-cron/sidekiq-cron/pull/495)
55
+
56
+ ## 2.0.0.rc1
57
+
58
+ - Introduce `Namespacing` (https://github.com/sidekiq-cron/sidekiq-cron/pull/268)
59
+ - Human readable cron format in UI (https://github.com/sidekiq-cron/sidekiq-cron/pull/445)
60
+ - Add Bahasa Indonesia locale (https://github.com/sidekiq-cron/sidekiq-cron/pull/446)
61
+ - Fetch queue name from ActiveJob class (https://github.com/sidekiq-cron/sidekiq-cron/pull/448)
62
+ - Allows symbol keys in `.load_from_array!` (https://github.com/sidekiq-cron/sidekiq-cron/pull/458)
63
+ - Add natural language parsing mode (https://github.com/sidekiq-cron/sidekiq-cron/pull/459)
64
+ - Add ability to configure the past scheduled time threshold (https://github.com/sidekiq-cron/sidekiq-cron/pull/465)
65
+ - Docs: clarify worker vs process terminology (https://github.com/sidekiq-cron/sidekiq-cron/issues/453)
66
+
67
+ ## 1.12.0
68
+
69
+ - Remove Sidekiq.server? check from schedule loader (https://github.com/sidekiq-cron/sidekiq-cron/pull/436)
70
+ - Parse arguments on `args=` method (https://github.com/sidekiq-cron/sidekiq-cron/pull/442)
71
+ - Only check out a Redis connection if necessary (https://github.com/sidekiq-cron/sidekiq-cron/pull/438)
72
+
73
+ ## 1.11.0
74
+
75
+ - Differentiates b/w "schedule" vs "dynamic" jobs (https://github.com/sidekiq-cron/sidekiq-cron/pull/431)
76
+ - Clears scheduled jobs upon schedule load (https://github.com/sidekiq-cron/sidekiq-cron/pull/431)
77
+ - Reduce gem size by excluding test files (https://github.com/sidekiq-cron/sidekiq-cron/pull/414)
78
+
79
+ ## 1.10.1
80
+
81
+ - Use `hset` instead of deprecated `hmset` (https://github.com/sidekiq-cron/sidekiq-cron/pull/410)
82
+
5
83
  ## 1.10.0
6
84
 
7
85
  - Remove EOL Ruby 2.6 support (https://github.com/sidekiq-cron/sidekiq-cron/pull/399)
@@ -45,7 +123,7 @@ All notable changes to this project will be documented in this file.
45
123
 
46
124
  ## 1.5.1
47
125
 
48
- - Fixes an issue that prevented the gem to work in previous Sidekiq versions (https://github.com/sidekiq-cron/sidekiq-cron/pull/335)
126
+ - Fixes an issue that prevented the gem to work in previous Sidekiq versions (https://github.com/sidekiq-cron/sidekiq-cron/pull/335)
49
127
 
50
128
  ## 1.5.0
51
129
 
@@ -55,7 +133,7 @@ All notable changes to this project will be documented in this file.
55
133
  ## 1.4.0
56
134
 
57
135
  - Fix buttons order in job show view (https://github.com/sidekiq-cron/sidekiq-cron/pull/302)
58
- - Dark Mode support in UI (https://github.com/sidekiq-cron/sidekiq-cron/pull/317/282)
136
+ - Dark Mode support in UI (https://github.com/sidekiq-cron/sidekiq-cron/pull/282)
59
137
  - Remove invocation of deprecated Redis functionality (https://github.com/sidekiq-cron/sidekiq-cron/pull/318)
60
138
  - Internal code cleanup (https://github.com/sidekiq-cron/sidekiq-cron/pull/317)
61
139
  - Optimize gem size (https://github.com/sidekiq-cron/sidekiq-cron/pull/322)
@@ -140,6 +218,6 @@ All notable changes to this project will be documented in this file.
140
218
 
141
219
  ## 0.3.0
142
220
 
143
- - Suport for Active Job
221
+ - Support for Active Job
144
222
  - Sidekiq cron web ui needs to be loaded by: require 'sidekiq/cron/web'
145
223
  - Add load_from_hash! and load_from_array! which cleanup jobs before adding new ones
data/Gemfile CHANGED
@@ -4,3 +4,6 @@ gemspec
4
4
 
5
5
  # To test different Sidekiq versions
6
6
  gem "sidekiq", ENV.fetch("SIDEKIQ_VERSION", ">= 6")
7
+
8
+ # To test different Rails versions
9
+ gem "rails", ENV.fetch("RAILS_VERSION", "~> 7")
data/README.md CHANGED
@@ -1,25 +1,21 @@
1
1
  ![Sidekiq-Cron](logos/cover.png)
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/sidekiq-cron.svg)](https://badge.fury.io/rb/sidekiq-cron)
4
- [![Build Status](https://github.com/sidekiq-cron/sidekiq-cron/workflows/CI/badge.svg?branch=master)](https://github.com/sidekiq-cron/sidekiq-cron/actions)
4
+ [![CI](https://github.com/sidekiq-cron/sidekiq-cron/actions/workflows/ci.yml/badge.svg)](https://github.com/sidekiq-cron/sidekiq-cron/actions/workflows/ci.yml)
5
5
  [![codecov](https://codecov.io/gh/sidekiq-cron/sidekiq-cron/branch/master/graph/badge.svg?token=VK9IVLIaY8)](https://codecov.io/gh/sidekiq-cron/sidekiq-cron)
6
6
 
7
7
  > A scheduling add-on for [Sidekiq](https://sidekiq.org/)
8
8
 
9
9
  🎬 [Introduction video about Sidekiq-Cron by Drifting Ruby](https://www.driftingruby.com/episodes/periodic-tasks-with-sidekiq-cron)
10
10
 
11
- Sidekiq-Cron runs a thread alongside Sidekiq workers to schedule jobs at specified times (using cron notation `* * * * *` parsed by [Fugit](https://github.com/floraison/fugit)).
11
+ Sidekiq-Cron runs a thread alongside Sidekiq workers to schedule jobs at specified times (using cron notation `* * * * *` or natural language, powered by [Fugit](https://github.com/floraison/fugit)).
12
12
 
13
- Checks for new jobs to schedule every 30 seconds and doesn't schedule the same job multiple times when more than one Sidekiq worker is running.
13
+ Checks for new jobs to schedule every 30 seconds and doesn't schedule the same job multiple times when more than one Sidekiq process is running.
14
14
 
15
15
  Scheduling jobs are added only when at least one Sidekiq process is running, but it is safe to use Sidekiq-Cron in environments where multiple Sidekiq processes or nodes are running.
16
16
 
17
17
  If you want to know how scheduling work, check out [under the hood](#under-the-hood).
18
18
 
19
- Works with ActiveJob (Rails 4.2+).
20
-
21
- You don't need Sidekiq PRO, you can use this gem with plain Sidekiq.
22
-
23
19
  ## Changelog
24
20
 
25
21
  Before upgrading to a new version, please read our [Changelog](CHANGELOG.md).
@@ -42,7 +38,7 @@ gem "sidekiq-cron"
42
38
 
43
39
  ## Getting Started
44
40
 
45
- ### Job properties
41
+ ### Job properties
46
42
 
47
43
  ```ruby
48
44
  {
@@ -51,17 +47,40 @@ gem "sidekiq-cron"
51
47
  'cron' => '1 * * * *', # execute at 1 minute of every hour, ex: 12:01, 13:01, 14:01, ...
52
48
  'class' => 'MyClass',
53
49
  # OPTIONAL
50
+ 'namespace' => 'YourNamespace', # groups jobs together in a namespace (Default value is 'default'),
51
+ 'source' => 'dynamic', # source of the job, `schedule`/`dynamic` (default: `dynamic`)
54
52
  'queue' => 'name of queue',
53
+ 'retry' => '5', # Sidekiq (not supported by ActiveJob) number of retries, or false to discard on first failure
55
54
  'args' => '[Array or Hash] of arguments which will be passed to perform method',
56
55
  'date_as_argument' => true, # add the time of execution as last argument of the perform method
57
- 'active_job' => true, # enqueue job through Rails 4.2+ Active Job interface
58
- 'queue_name_prefix' => 'prefix', # Rails 4.2+ Active Job queue with prefix
59
- 'queue_name_delimiter' => '.', # Rails 4.2+ Active Job queue with custom delimiter
60
- 'description' => 'A sentence describing what work this job performs'
56
+ 'active_job' => true, # enqueue job through Active Job interface
57
+ 'queue_name_prefix' => 'prefix', # Active Job queue with prefix
58
+ 'queue_name_delimiter' => '.', # Active Job queue with custom delimiter (default: '_')
59
+ 'description' => 'A sentence describing what work this job performs',
61
60
  'status' => 'disabled' # default: enabled
62
61
  }
63
62
  ```
64
63
 
64
+ **NOTE** The `status` of a job does not get changed in Redis when a job gets reloaded unless the `status` property is explicitly set.
65
+
66
+ ### Configuration
67
+
68
+ All configuration options:
69
+
70
+ ```ruby
71
+ Sidekiq::Cron.configure do |config|
72
+ config.cron_poll_interval = 10 # Default is 30
73
+ config.cron_schedule_file = 'config/my_schedule.yml' # Default is 'config/schedule.yml'
74
+ config.cron_history_size = 20 # Default is 10
75
+ config.default_namespace = 'statistics' # Default is 'default'
76
+ config.available_namespaces = %w[statistics maintenance billing] # Default is `[config.default_namespace]`
77
+ config.natural_cron_parsing_mode = :strict # Default is :single
78
+ config.reschedule_grace_period = 300 # Default is 60
79
+ end
80
+ ```
81
+
82
+ If you are using Rails, you should add the above block inside an initializer (`config/initializers/sidekiq-cron.rb`).
83
+
65
84
  ### Time, cron and Sidekiq-Cron
66
85
 
67
86
  For testing your cron notation you can use [crontab.guru](https://crontab.guru).
@@ -75,18 +94,50 @@ like this `'0 22 * * 1-5 America/Chicago'`.
75
94
 
76
95
  #### Natural-language formats
77
96
 
78
- Since sidekiq-cron `v1.7.0`, you can use the natural-language formats supported by Fugit, such as:
97
+ Since Sidekiq-Cron `v1.7.0`, you can use the natural-language formats supported by Fugit, such as:
79
98
 
80
- ```rb
99
+ ```ruby
81
100
  "every day at five" # => '0 5 * * *'
82
101
  "every 3 hours" # => '0 */3 * * *'
83
102
  ```
84
103
 
85
104
  See [the relevant part of Fugit documentation](https://github.com/floraison/fugit#fugitnat) for details.
86
105
 
106
+ There are multiple modes that determine how natural-language cron strings will be parsed.
107
+
108
+ 1. `:single` (default)
109
+
110
+ ```ruby
111
+ Sidekiq::Cron.configure do |config|
112
+ # Note: This doesn't need to be specified since it's the default.
113
+ config.natural_cron_parsing_mode = :single
114
+ end
115
+ ```
116
+
117
+ This parses the first possible cron line from the given string and then ignores any additional cron lines.
118
+
119
+ Ex. `every day at 3:15 and 4:30`
120
+
121
+ - Equivalent to `15 3 * * *`.
122
+ - `30 4 * * *` gets ignored.
123
+
124
+ 2. `:strict`
125
+
126
+ ```ruby
127
+ Sidekiq::Cron.configure do |config|
128
+ config.natural_cron_parsing_mode = :strict
129
+ end
130
+ ```
131
+
132
+ This throws an error if the given string would be parsed into multiple cron lines.
133
+
134
+ Ex. `every day at 3:15 and 4:30`
135
+
136
+ - Would throw an error and the associated cron job would be invalid
137
+
87
138
  #### Second-precision (sub-minute) cronlines
88
139
 
89
- In addition to the standard 5-parameter cronline format, sidekiq-cron supports scheduling jobs with second-precision using a modified 6-parameter cronline format:
140
+ In addition to the standard 5-parameter cronline format, Sidekiq-Cron supports scheduling jobs with second-precision using a modified 6-parameter cronline format:
90
141
 
91
142
  `Seconds Minutes Hours Days Months DayOfWeek`
92
143
 
@@ -95,11 +146,93 @@ For example: `"*/30 * * * * *"` would schedule a job to run every 30 seconds.
95
146
  Note that if you plan to schedule jobs with second precision you may need to override the default schedule poll interval so it is lower than the interval of your jobs:
96
147
 
97
148
  ```ruby
98
- Sidekiq::Options[:cron_poll_interval] = 10
149
+ Sidekiq::Cron.configure do |config|
150
+ config.cron_poll_interval = 10
151
+ end
99
152
  ```
100
153
 
101
154
  The default value at time of writing is 30 seconds. See [under the hood](#under-the-hood) for more details.
102
155
 
156
+ ### Namespacing
157
+
158
+ #### Default namespace
159
+
160
+ When not giving a namespace, the `default` one will be used.
161
+
162
+ In the case you'd like to change this value, you can change it via the following configuration flag:
163
+
164
+ ```ruby
165
+ Sidekiq::Cron.configure do |config|
166
+ config.default_namespace = 'statistics'
167
+ end
168
+ ```
169
+
170
+ #### Renaming namespace
171
+
172
+ If you rename the namespace of a job that is already running, the gem will not automatically delete the cron job associated with the old namespace. This means you could end up with two cron jobs running simultaneously.
173
+
174
+ To avoid this, it is recommended to delete all existing cron jobs associated with the old namespace before making the change. You can achieve this with the following code:
175
+
176
+ ```ruby
177
+ Sidekiq::Cron::Job.all('YOUR_OLD_NAMESPACE_NAME').each { |job| job.destroy }
178
+ ```
179
+
180
+ #### Available namespaces
181
+
182
+ By default, Sidekiq Cron uses the available_namespaces configuration option to determine which namespaces your application utilizes. The default namespace (`"default"`, by default) is always included in the list of available namespaces.
183
+
184
+ If you want Sidekiq Cron to automatically detect existing namespaces from the Redis database, you can set `available_namespaces` to the special option `:auto`.
185
+
186
+ If available_namespaces is explicitly set and a job is created with an unexpected namespace, a warning will be printed, and the job will be assigned to the default namespace.
187
+
188
+ #### Migrating to 2.3
189
+
190
+ As discussed in [this issue](https://github.com/sidekiq-cron/sidekiq-cron/issues/516), the approach introduced in Sidekiq Cron 2.0 for determining available namespaces using the `KEYS` command is not acceptable. Therefore, starting from version 2.3, namespacing has been reworked:
191
+
192
+ - If you were not using the namespacing feature, no action is required. You can even remove `available_namespaces = %w[default]`, as it is now the default.
193
+
194
+ - If you were using the namespacing feature and explicitly specified available namespaces as a list, no changes are needed.
195
+
196
+ - If you were using the namespacing feature and relied on automatic namespace inference, you should either specify all used namespaces explicitly or set `available_namespaces` to `:auto` to maintain automatic detection. However, note that this approach does not scale well (see the referenced issue for details).
197
+
198
+ #### Usage
199
+
200
+ When creating a new job, you can optionally give a `namespace` attribute, and then you can pass it too in the `find` or `destroy` methods.
201
+
202
+ ```ruby
203
+ Sidekiq::Cron::Job.create(
204
+ name: 'Hard worker - every 5min',
205
+ namespace: 'Foo',
206
+ cron: '*/5 * * * *',
207
+ class: 'HardWorker'
208
+ )
209
+ # INFO: Cron Jobs - add job with name Hard worker - every 5min in the namespace Foo
210
+
211
+ # Without specifying the namespace, Sidekiq::Cron use the `default` one, therefore `count` return 0.
212
+ Sidekiq::Cron::Job.count
213
+ #=> 0
214
+
215
+ # Searching in the job's namespace returns 1.
216
+ Sidekiq::Cron::Job.count 'Foo'
217
+ #=> 1
218
+
219
+ # Same applies to `all`. Without a namespace, no jobs found.
220
+ Sidekiq::Cron::Job.all
221
+
222
+ # But giving the job's namespace returns it.
223
+ Sidekiq::Cron::Job.all 'Foo'
224
+ #=> [#<Sidekiq::Cron::Job:0x00007f7848a326a0 ... @name="Hard worker - every 5min", @namespace="Foo", @cron="*/5 * * * *", @klass="HardWorker", @status="enabled" ... >]
225
+
226
+ # If you'd like to get all the jobs across all the namespaces then pass an asterisk:
227
+ Sidekiq::Cron::Job.all '*'
228
+ #=> [#<Sidekiq::Cron::Job ...>]
229
+
230
+ job = Sidekiq::Cron::Job.find('Hard worker - every 5min', 'Foo').first
231
+ job.destroy
232
+ # INFO: Cron Jobs - deleted job with name Hard worker - every 5min from namespace Foo
233
+ #=> true
234
+ ```
235
+
103
236
  ### What objects/classes can be scheduled
104
237
 
105
238
  #### Sidekiq Worker
@@ -116,7 +249,9 @@ class HardWorker
116
249
  end
117
250
  ```
118
251
 
119
- #### Active Job Worker
252
+ For Sidekiq workers, `symbolize_args: true` in `Sidekiq::Cron::Job.create` or in Hash configuration is gonna be ignored as Sidekiq currently only allows for [simple JSON datatypes](https://github.com/sidekiq/sidekiq/wiki/The-Basics#:~:text=These%20two%20methods,not%20serialize%20properly.).
253
+
254
+ #### Active Job
120
255
 
121
256
  You can schedule `ExampleJob` which looks like:
122
257
 
@@ -130,10 +265,12 @@ class ExampleJob < ActiveJob::Base
130
265
  end
131
266
  ```
132
267
 
133
- For Active jobs you can use `symbolize_args: true` in `Sidekiq::Cron::Job.create` or in Hash configuration,
268
+ For Active Job you can use `symbolize_args: true` in `Sidekiq::Cron::Job.create` or in Hash configuration,
134
269
  which will ensure that arguments you are passing to it will be symbolized when passed back to `perform` method in worker.
135
270
 
136
- #### Adding Cron job
271
+ ### Adding Cron jobs
272
+
273
+ Refer to [Schedule vs Dynamic jobs](#schedule-vs-dynamic-jobs) to understand the difference.
137
274
 
138
275
  ```ruby
139
276
  class HardWorker
@@ -165,9 +302,9 @@ unless job.save
165
302
  end
166
303
  ```
167
304
 
168
- Use ActiveRecord models as arguments
305
+ Use ActiveRecord models as arguments:
169
306
 
170
- ```rb
307
+ ```ruby
171
308
  class Person < ApplicationRecord
172
309
  end
173
310
 
@@ -179,7 +316,6 @@ class HardWorker < ActiveJob::Base
179
316
  end
180
317
  end
181
318
 
182
-
183
319
  person = Person.create(id: 1)
184
320
  Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker', args: person)
185
321
  # => true
@@ -223,14 +359,16 @@ array = [
223
359
  Sidekiq::Cron::Job.load_from_array array
224
360
  ```
225
361
 
226
- Bang-suffixed methods will remove jobs that are not present in the given hash/array, update jobs that have the same names, and create new ones when the names are previously unknown.
362
+ Bang-suffixed methods will remove jobs where source is `schedule` and are not present in the given hash/array, update jobs that have the same names, and create new ones when the names are previously unknown.
227
363
 
228
364
  ```ruby
229
365
  Sidekiq::Cron::Job.load_from_hash! hash
230
366
  Sidekiq::Cron::Job.load_from_array! array
231
367
  ```
232
368
 
233
- Or from YAML (same notation as Resque-scheduler):
369
+ ### Loading jobs from schedule file
370
+
371
+ You can also load multiple jobs from a YAML file:
234
372
 
235
373
  ```yaml
236
374
  # config/schedule.yml
@@ -251,22 +389,31 @@ second_job:
251
389
  There are multiple ways to load the jobs from a YAML file
252
390
 
253
391
  1. The gem will automatically load the jobs mentioned in `config/schedule.yml` file (it supports ERB)
254
- 2. When you want to load jobs from a different filename, mention the filename in sidekiq configuration, i.e. `cron_schedule_file: "config/users_schedule.yml"`
392
+ 2. When you want to load jobs from a different filename, mention the filename in Sidekiq configuration as follows:
393
+
394
+ ```ruby
395
+ Sidekiq::Cron.configure do |config|
396
+ config.cron_schedule_file = "config/users_schedule.yml"
397
+ end
398
+ ```
399
+
255
400
  3. Load the file manually as follows:
256
401
 
257
- ```ruby
258
- # config/initializers/sidekiq.rb
402
+ ```ruby
403
+ # config/initializers/sidekiq.rb
404
+
405
+ Sidekiq.configure_server do |config|
406
+ config.on(:startup) do
407
+ schedule_file = "config/users_schedule.yml"
259
408
 
260
- Sidekiq.configure_server do |config|
261
- config.on(:startup) do
262
- schedule_file = "config/users_schedule.yml"
409
+ if File.exist?(schedule_file)
410
+ schedule = YAML.load_file(schedule_file)
263
411
 
264
- if File.exist?(schedule_file)
265
- Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
412
+ Sidekiq::Cron::Job.load_from_hash!(schedule, source: "schedule")
413
+ end
414
+ end
266
415
  end
267
- end
268
- end
269
- ```
416
+ ```
270
417
 
271
418
  ### Finding jobs
272
419
 
@@ -312,9 +459,19 @@ job.status
312
459
  # => enabled/disabled
313
460
 
314
461
  # enqueue job right now!
315
- job.enque!
462
+ job.enqueue!
316
463
  ```
317
464
 
465
+ ### Schedule vs Dynamic jobs
466
+
467
+ There are two potential job sources: `schedule` and `dynamic`.
468
+ Jobs associated with schedule files are labeled as `schedule` as their source,
469
+ whereas jobs created at runtime without the `source=schedule` argument are classified as `dynamic`.
470
+
471
+ The key distinction lies in how these jobs are managed.
472
+ When a schedule is loaded, any stale `schedule` jobs are automatically removed to ensure synchronization within the schedule.
473
+ The `dynamic` jobs remain unaffected by this process.
474
+
318
475
  ### How to start scheduling?
319
476
 
320
477
  Just start Sidekiq workers by running:
@@ -341,12 +498,67 @@ Sidekiq-Cron adds itself into this start procedure and starts another thread wit
341
498
  Sidekiq-Cron is checking jobs to be enqueued every 30s by default, you can change it by setting:
342
499
 
343
500
  ```ruby
344
- Sidekiq::Options[:cron_poll_interval] = 10
501
+ Sidekiq::Cron.configure do |config|
502
+ config.cron_poll_interval = 10
503
+ end
504
+ ```
505
+
506
+ When Sidekiq (and Sidekiq-Cron) is not used in zero-downtime deployments, after the deployment is done Sidekiq-Cron starts to catch up. It will consider older jobs that missed their schedules during that time. By default, only jobs that should have started less than 1 minute ago are considered. This is problematic for some jobs, e.g., jobs that run once a day. If on average Sidekiq is shut down for 10 minutes during deployments, you can configure Sidekiq-Cron to consider jobs that were about to be scheduled during that time:
507
+
508
+ ```ruby
509
+ Sidekiq::Cron.configure do |config|
510
+ config.reschedule_grace_period = 600 # 10 minutes in seconds
511
+ end
345
512
  ```
346
513
 
347
514
  Sidekiq-Cron is safe to use with multiple Sidekiq processes or nodes. It uses a Redis sorted set to determine that only the first process who asks can enqueue scheduled jobs into the queue.
348
515
 
349
- When running with many Sidekiq processes, the polling can add significant load to Redis. You can disable polling on some processes by setting `Sidekiq::Options[:cron_poll_interval] = 0` on these processes.
516
+ When running with many Sidekiq processes, the polling can add significant load to Redis. You can disable polling on some processes by setting:
517
+
518
+ ```ruby
519
+ Sidekiq::Cron.configure do |config|
520
+ config.cron_poll_interval = 0
521
+ end
522
+ ```
523
+
524
+ ## Testing your configuration
525
+
526
+ You can test your application's configuration by loading the schedule in your test suite. Below is an example using RSpec in a Rails project:
527
+
528
+ ```ruby
529
+ # spec/cron_schedule_spec.rb
530
+ require "rails_helper"
531
+
532
+ RSpec.describe "Cron Schedule" do
533
+ let(:schedule_loader) { Sidekiq::Cron::ScheduleLoader.new }
534
+ let(:all_jobs) { Sidekiq::Cron::Job.all }
535
+
536
+ # Confirms that `config.cron_schedule_file` points to a real file.
537
+ it "has a schedule file" do
538
+ expect(schedule_loader).to have_schedule_file
539
+ end
540
+
541
+ # Confirms that no jobs in the schedule have an invalid cron string.
542
+ it "does not return any errors" do
543
+ expect(schedule_loader.load_schedule).to be_empty
544
+ end
545
+
546
+ # May be subject to churn, but adds confidence.
547
+ it "adds the expected number of jobs" do
548
+ schedule_loader.load_schedule
549
+ expect(all_jobs.size).to eq 5
550
+ end
551
+
552
+ # Confirms that all job classes exist.
553
+ it "has a valid class for each added job" do
554
+ schedule_loader.load_schedule
555
+ # Shows that all classes exist (as we can constantize the names without raising).
556
+ job_classes = all_jobs.map { |job| job.klass.constantize }
557
+ # Naive check that classes are sidekiq jobs (as they all have `.perfrom_async`).
558
+ expect(job_classes).to all(respond_to(:perform_async))
559
+ end
560
+ end
561
+ ```
350
562
 
351
563
  ## Contributing
352
564
 
@@ -368,6 +580,39 @@ You can execute the test suite by running:
368
580
  $ bundle exec rake test
369
581
  ```
370
582
 
583
+ ### Using Docker
584
+
585
+ This project uses [Docker Compose](https://docs.docker.com/compose/) in order to orchestrate containers and get the test suite running on you local machine, and here you find the commands to run in order to get a complete environment to build and test this gem:
586
+
587
+ 1. Build the Docker image (only the first time):
588
+ ```
589
+ docker compose -f docker/docker-compose.yml build
590
+ ```
591
+
592
+ 2. Run the test suite:
593
+ ```
594
+ docker compose -f docker/docker-compose.yml run --rm tests
595
+ ```
596
+
597
+ _This command will download the first time the project's dependencies (Redis so far), create the containers and run the default command to run the tests._
598
+
599
+ **Running other commands**
600
+
601
+ In the case you need to run a command in the gem's container, you would do it like so:
602
+
603
+ ```
604
+ docker compose -f docker/docker-compose.yml run --rm tests <HERE IS YOUR COMMAND>
605
+ ```
606
+ _Note that `tests` is the Docker Compose service name defined in the `docker/docker-compose.yml` file._
607
+
608
+ **Running a single test file**
609
+
610
+ Given you only want to run the tests from the `test/unit/web_extension_test.rb` file, you need to pass its path with the `TEST` env variable, so here is the command:
611
+
612
+ ```
613
+ docker compose -f docker/docker-compose.yml run --rm --env TEST=test/unit/web_extension_test.rb tests
614
+ ```
615
+
371
616
  ## License
372
617
 
373
618
  Copyright (c) 2013 Ondrej Bartas. See [LICENSE](LICENSE.txt) for further details.