sidekiq-cron 1.12.0 → 2.0.0.rc2

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