sidekiq-cron 1.11.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38f49b844ee1ade391bc47b8acd2e98b02cb9d05041cad364fca427c51d3f65d
4
- data.tar.gz: 6328039e5648a59057202fec118775e8e1ed7ffc2d5a38229c62e4c7af7ebfaf
3
+ metadata.gz: f6e739f09f2271ea9904b73771ac03701be57e5eae64e3dd3963e772cda7bdb2
4
+ data.tar.gz: 2ab0776fd2dad66b0db9ba38904a3edfbc540e854187f7a59ca205a748b4fd74
5
5
  SHA512:
6
- metadata.gz: 80d176888e61a86fabac69a160fc778965b7900b8971979158a7e192289874fea61c18072dd6a306f6a3b8e456e68ea74df57a6e9ede9cb5f64d721228757ce5
7
- data.tar.gz: 27febbff9109ca10a5bfc7b6e4414f903e9021ca598ef94992b1445dd383f60d8d48f86f3bd0af93d60b6593a4e2adda26308c8cae42aabdcca0803be64005b1
6
+ metadata.gz: 9231cf42e072213083e19c924a09838185faf20d240db195cba87db8f406443ac83172622e4ed89652d09f669e6ce04b7bae3a7836755b28043350c1e2ca056a
7
+ data.tar.gz: 9b63c398a1e129701a20330c5e801f8f36a04651366ed3f38d01061d86033774eb9fe845097af0041f465083d433f4ef783e1be8abf1d083a43f6e3e714c3057
data/CHANGELOG.md CHANGED
@@ -2,6 +2,44 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## 2.0.0
6
+
7
+ Sidekiq-Cron v2 is here! In this release we refactored some internals, plus:
8
+
9
+ - Review web UI translations for all available locales (https://github.com/sidekiq-cron/sidekiq-cron/pull/506)
10
+ - Fix detection of ActiveJob in Sidekiq v7.3.3+ (https://github.com/sidekiq-cron/sidekiq-cron/pull/510)
11
+ - Add retry job configuration option to set Sidekiq retry job option (https://github.com/sidekiq-cron/sidekiq-cron/pull/509)
12
+
13
+ Please take a look to the RC1 and RC2 changes too if you are coming from the v1.X series.
14
+
15
+ ## 2.0.0.rc2
16
+
17
+ - Remove support for Sidekiq < 6.5 (https://github.com/sidekiq-cron/sidekiq-cron/pull/480)
18
+ - Require at least Fugit >= 1.11.1 (https://github.com/sidekiq-cron/sidekiq-cron/pull/475)
19
+ - Update how Redis values are stored on save (https://github.com/sidekiq-cron/sidekiq-cron/pull/479)
20
+ - Web extension: Add compatibility with Sidekiq 7.3+ and remove inline styles (https://github.com/sidekiq-cron/sidekiq-cron/pull/480)
21
+ - Remove support for old Redis (< 4.2) (https://github.com/sidekiq-cron/sidekiq-cron/pull/490)
22
+ - 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)
23
+ - Rename `enque!` to `enqueue!` (https://github.com/sidekiq-cron/sidekiq-cron/pull/494)
24
+ - Refactor gem configuration module (https://github.com/sidekiq-cron/sidekiq-cron/pull/495)
25
+
26
+ ## 2.0.0.rc1
27
+
28
+ - Introduce `Namespacing` (https://github.com/sidekiq-cron/sidekiq-cron/pull/268)
29
+ - Human readable cron format in UI (https://github.com/sidekiq-cron/sidekiq-cron/pull/445)
30
+ - Add Bahasa Indonesia locale (https://github.com/sidekiq-cron/sidekiq-cron/pull/446)
31
+ - Fetch queue name from ActiveJob class (https://github.com/sidekiq-cron/sidekiq-cron/pull/448)
32
+ - Allows symbol keys in `.load_from_array!` (https://github.com/sidekiq-cron/sidekiq-cron/pull/458)
33
+ - Add natural language parsing mode (https://github.com/sidekiq-cron/sidekiq-cron/pull/459)
34
+ - Add ability to configure the past scheduled time threshold (https://github.com/sidekiq-cron/sidekiq-cron/pull/465)
35
+ - Docs: clarify worker vs process terminology (https://github.com/sidekiq-cron/sidekiq-cron/issues/453)
36
+
37
+ ## 1.12.0
38
+
39
+ - Remove Sidekiq.server? check from schedule loader (https://github.com/sidekiq-cron/sidekiq-cron/pull/436)
40
+ - Parse arguments on `args=` method (https://github.com/sidekiq-cron/sidekiq-cron/pull/442)
41
+ - Only check out a Redis connection if necessary (https://github.com/sidekiq-cron/sidekiq-cron/pull/438)
42
+
5
43
  ## 1.11.0
6
44
 
7
45
  - Differentiates b/w "schedule" vs "dynamic" jobs (https://github.com/sidekiq-cron/sidekiq-cron/pull/431)
@@ -55,7 +93,7 @@ All notable changes to this project will be documented in this file.
55
93
 
56
94
  ## 1.5.1
57
95
 
58
- - Fixes an issue that prevented the gem to work in previous Sidekiq versions (https://github.com/sidekiq-cron/sidekiq-cron/pull/335)
96
+ - Fixes an issue that prevented the gem to work in previous Sidekiq versions (https://github.com/sidekiq-cron/sidekiq-cron/pull/335)
59
97
 
60
98
  ## 1.5.0
61
99
 
@@ -150,6 +188,6 @@ All notable changes to this project will be documented in this file.
150
188
 
151
189
  ## 0.3.0
152
190
 
153
- - Suport for Active Job
191
+ - Support for Active Job
154
192
  - Sidekiq cron web ui needs to be loaded by: require 'sidekiq/cron/web'
155
193
  - Add load_from_hash! and load_from_array! which cleanup jobs before adding new ones
data/README.md CHANGED
@@ -8,18 +8,14 @@
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).
@@ -51,18 +47,39 @@ 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'),
54
51
  'source' => 'dynamic', # source of the job, `schedule`/`dynamic` (default: `dynamic`)
55
52
  'queue' => 'name of queue',
53
+ 'retry' => '5', # Sidekiq (not supported by ActiveJob) number of retries, or false to discard on first failure
56
54
  'args' => '[Array or Hash] of arguments which will be passed to perform method',
57
55
  'date_as_argument' => true, # add the time of execution as last argument of the perform method
58
- 'active_job' => true, # enqueue job through Rails 4.2+ Active Job interface
59
- 'queue_name_prefix' => 'prefix', # Rails 4.2+ Active Job queue with prefix
60
- 'queue_name_delimiter' => '.', # Rails 4.2+ Active Job queue with custom delimiter (default: '_')
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: '_')
61
59
  'description' => 'A sentence describing what work this job performs'
62
60
  'status' => 'disabled' # default: enabled
63
61
  }
64
62
  ```
65
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.natural_cron_parsing_mode = :strict # Default is :single
77
+ config.reschedule_grace_period = 300 # Default is 60
78
+ end
79
+ ```
80
+
81
+ If you are using Rails, you should add the above block inside an initializer (`config/initializers/sidekiq-cron.rb`).
82
+
66
83
  ### Time, cron and Sidekiq-Cron
67
84
 
68
85
  For testing your cron notation you can use [crontab.guru](https://crontab.guru).
@@ -76,18 +93,50 @@ like this `'0 22 * * 1-5 America/Chicago'`.
76
93
 
77
94
  #### Natural-language formats
78
95
 
79
- Since sidekiq-cron `v1.7.0`, you can use the natural-language formats supported by Fugit, such as:
96
+ Since Sidekiq-Cron `v1.7.0`, you can use the natural-language formats supported by Fugit, such as:
80
97
 
81
- ```rb
98
+ ```ruby
82
99
  "every day at five" # => '0 5 * * *'
83
100
  "every 3 hours" # => '0 */3 * * *'
84
101
  ```
85
102
 
86
103
  See [the relevant part of Fugit documentation](https://github.com/floraison/fugit#fugitnat) for details.
87
104
 
105
+ There are multiple modes that determine how natural-language cron strings will be parsed.
106
+
107
+ 1. `:single` (default)
108
+
109
+ ```ruby
110
+ Sidekiq::Cron.configure do |config|
111
+ # Note: This doesn't need to be specified since it's the default.
112
+ config.natural_cron_parsing_mode = :single
113
+ end
114
+ ```
115
+
116
+ This parses the first possible cron line from the given string and then ignores any additional cron lines.
117
+
118
+ Ex. `every day at 3:15 and 4:30`
119
+
120
+ - Equivalent to `15 3 * * *`.
121
+ - `30 4 * * *` gets ignored.
122
+
123
+ 2. `:strict`
124
+
125
+ ```ruby
126
+ Sidekiq::Cron.configure do |config|
127
+ config.natural_cron_parsing_mode = :strict
128
+ end
129
+ ```
130
+
131
+ This throws an error if the given string would be parsed into multiple cron lines.
132
+
133
+ Ex. `every day at 3:15 and 4:30`
134
+
135
+ - Would throw an error and the associated cron job would be invalid
136
+
88
137
  #### Second-precision (sub-minute) cronlines
89
138
 
90
- In addition to the standard 5-parameter cronline format, sidekiq-cron supports scheduling jobs with second-precision using a modified 6-parameter cronline format:
139
+ In addition to the standard 5-parameter cronline format, Sidekiq-Cron supports scheduling jobs with second-precision using a modified 6-parameter cronline format:
91
140
 
92
141
  `Seconds Minutes Hours Days Months DayOfWeek`
93
142
 
@@ -96,11 +145,65 @@ For example: `"*/30 * * * * *"` would schedule a job to run every 30 seconds.
96
145
  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:
97
146
 
98
147
  ```ruby
99
- Sidekiq::Options[:cron_poll_interval] = 10
148
+ Sidekiq::Cron.configure do |config|
149
+ config.cron_poll_interval = 10
150
+ end
100
151
  ```
101
152
 
102
153
  The default value at time of writing is 30 seconds. See [under the hood](#under-the-hood) for more details.
103
154
 
155
+ ### Namespacing
156
+
157
+ #### Default namespace
158
+
159
+ When not giving a namespace, the `default` one will be used.
160
+
161
+ In the case you'd like to change this value, you can change it via the following configuration flag:
162
+
163
+ ```ruby
164
+ Sidekiq::Cron.configure do |config|
165
+ config.default_namespace = 'statics'
166
+ end
167
+ ```
168
+
169
+ #### Usage
170
+
171
+ 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.
172
+
173
+ ```ruby
174
+ Sidekiq::Cron::Job.create(
175
+ name: 'Hard worker - every 5min',
176
+ namespace: 'Foo',
177
+ cron: '*/5 * * * *',
178
+ class: 'HardWorker'
179
+ )
180
+ # INFO: Cron Jobs - add job with name Hard worker - every 5min in the namespace Foo
181
+
182
+ # Without specifying the namespace, Sidekiq::Cron use the `default` one, therefore `count` return 0.
183
+ Sidekiq::Cron::Job.count
184
+ #=> 0
185
+
186
+ # Searching in the job's namespace returns 1.
187
+ Sidekiq::Cron::Job.count 'Foo'
188
+ #=> 1
189
+
190
+ # Same applies to `all`. Without a namespace, no jobs found.
191
+ Sidekiq::Cron::Job.all
192
+
193
+ # But giving the job's namespace returns it.
194
+ Sidekiq::Cron::Job.all 'Foo'
195
+ #=> [#<Sidekiq::Cron::Job:0x00007f7848a326a0 ... @name="Hard worker - every 5min", @namespace="Foo", @cron="*/5 * * * *", @klass="HardWorker", @status="enabled" ... >]
196
+
197
+ # If you'd like to get all the jobs across all the namespaces then pass an asterisk:
198
+ Sidekiq::Cron::Job.all '*'
199
+ #=> [#<Sidekiq::Cron::Job ...>]
200
+
201
+ job = Sidekiq::Cron::Job.find('Hard worker - every 5min', 'Foo').first
202
+ job.destroy
203
+ # INFO: Cron Jobs - deleted job with name Hard worker - every 5min from namespace Foo
204
+ #=> true
205
+ ```
206
+
104
207
  ### What objects/classes can be scheduled
105
208
 
106
209
  #### Sidekiq Worker
@@ -117,7 +220,9 @@ class HardWorker
117
220
  end
118
221
  ```
119
222
 
120
- #### Active Job Worker
223
+ 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.).
224
+
225
+ #### Active Job
121
226
 
122
227
  You can schedule `ExampleJob` which looks like:
123
228
 
@@ -131,10 +236,10 @@ class ExampleJob < ActiveJob::Base
131
236
  end
132
237
  ```
133
238
 
134
- For Active jobs you can use `symbolize_args: true` in `Sidekiq::Cron::Job.create` or in Hash configuration,
239
+ For Active Job you can use `symbolize_args: true` in `Sidekiq::Cron::Job.create` or in Hash configuration,
135
240
  which will ensure that arguments you are passing to it will be symbolized when passed back to `perform` method in worker.
136
241
 
137
- #### Adding Cron job
242
+ ### Adding Cron jobs
138
243
 
139
244
  Refer to [Schedule vs Dynamic jobs](#schedule-vs-dynamic-jobs) to understand the difference.
140
245
 
@@ -168,9 +273,9 @@ unless job.save
168
273
  end
169
274
  ```
170
275
 
171
- Use ActiveRecord models as arguments
276
+ Use ActiveRecord models as arguments:
172
277
 
173
- ```rb
278
+ ```ruby
174
279
  class Person < ApplicationRecord
175
280
  end
176
281
 
@@ -182,7 +287,6 @@ class HardWorker < ActiveJob::Base
182
287
  end
183
288
  end
184
289
 
185
-
186
290
  person = Person.create(id: 1)
187
291
  Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker', args: person)
188
292
  # => true
@@ -233,7 +337,9 @@ Sidekiq::Cron::Job.load_from_hash! hash
233
337
  Sidekiq::Cron::Job.load_from_array! array
234
338
  ```
235
339
 
236
- Or from YAML (same notation as Resque-scheduler):
340
+ ### Loading jobs from schedule file
341
+
342
+ You can also load multiple jobs from a YAML file:
237
343
 
238
344
  ```yaml
239
345
  # config/schedule.yml
@@ -254,24 +360,31 @@ second_job:
254
360
  There are multiple ways to load the jobs from a YAML file
255
361
 
256
362
  1. The gem will automatically load the jobs mentioned in `config/schedule.yml` file (it supports ERB)
257
- 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"`
363
+ 2. When you want to load jobs from a different filename, mention the filename in Sidekiq configuration as follows:
364
+
365
+ ```ruby
366
+ Sidekiq::Cron.configure do |config|
367
+ config.cron_schedule_file = "config/users_schedule.yml"
368
+ end
369
+ ```
370
+
258
371
  3. Load the file manually as follows:
259
372
 
260
- ```ruby
261
- # config/initializers/sidekiq.rb
373
+ ```ruby
374
+ # config/initializers/sidekiq.rb
262
375
 
263
- Sidekiq.configure_server do |config|
264
- config.on(:startup) do
265
- schedule_file = "config/users_schedule.yml"
376
+ Sidekiq.configure_server do |config|
377
+ config.on(:startup) do
378
+ schedule_file = "config/users_schedule.yml"
266
379
 
267
- if File.exist?(schedule_file)
268
- schedule = YAML.load_file(schedule_file)
380
+ if File.exist?(schedule_file)
381
+ schedule = YAML.load_file(schedule_file)
269
382
 
270
- Sidekiq::Cron::Job.load_from_hash!(schedule, source: "schedule")
383
+ Sidekiq::Cron::Job.load_from_hash!(schedule, source: "schedule")
384
+ end
385
+ end
271
386
  end
272
- end
273
- end
274
- ```
387
+ ```
275
388
 
276
389
  ### Finding jobs
277
390
 
@@ -317,7 +430,7 @@ job.status
317
430
  # => enabled/disabled
318
431
 
319
432
  # enqueue job right now!
320
- job.enque!
433
+ job.enqueue!
321
434
  ```
322
435
 
323
436
  ### Schedule vs Dynamic jobs
@@ -356,12 +469,28 @@ Sidekiq-Cron adds itself into this start procedure and starts another thread wit
356
469
  Sidekiq-Cron is checking jobs to be enqueued every 30s by default, you can change it by setting:
357
470
 
358
471
  ```ruby
359
- Sidekiq::Options[:cron_poll_interval] = 10
472
+ Sidekiq::Cron.configure do |config|
473
+ config.cron_poll_interval = 10
474
+ end
475
+ ```
476
+
477
+ 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:
478
+
479
+ ```ruby
480
+ Sidekiq::Cron.configure do |config|
481
+ config.reschedule_grace_period = 600 # 10 minutes in seconds
482
+ end
360
483
  ```
361
484
 
362
485
  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.
363
486
 
364
- 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.
487
+ When running with many Sidekiq processes, the polling can add significant load to Redis. You can disable polling on some processes by setting:
488
+
489
+ ```ruby
490
+ Sidekiq::Cron.configure do |config|
491
+ config.cron_poll_interval = 0
492
+ end
493
+ ```
365
494
 
366
495
  ## Contributing
367
496
 
@@ -383,6 +512,39 @@ You can execute the test suite by running:
383
512
  $ bundle exec rake test
384
513
  ```
385
514
 
515
+ ### Using Docker
516
+
517
+ 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:
518
+
519
+ 1. Build the Docker image (only the first time):
520
+ ```
521
+ docker compose -f docker/docker-compose.yml build
522
+ ```
523
+
524
+ 2. Run the test suite:
525
+ ```
526
+ docker compose -f docker/docker-compose.yml run --rm tests
527
+ ```
528
+
529
+ _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._
530
+
531
+ **Running other commands**
532
+
533
+ In the case you need to run a command in the gem's container, you would do it like so:
534
+
535
+ ```
536
+ docker compose -f docker/docker-compose.yml run --rm tests <HERE IS YOUR COMMAND>
537
+ ```
538
+ _Note that `tests` is the Docker Compose service name defined in the `docker/docker-compose.yml` file._
539
+
540
+ **Running a single test file**
541
+
542
+ 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:
543
+
544
+ ```
545
+ docker compose -f docker/docker-compose.yml run --rm --env TEST=test/unit/web_extension_test.rb tests
546
+ ```
547
+
386
548
  ## License
387
549
 
388
550
  Copyright (c) 2013 Ondrej Bartas. See [LICENSE](LICENSE.txt) for further details.