sidekiq-cron 0.6.3 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +145 -0
  3. data/Gemfile +3 -29
  4. data/README.md +175 -121
  5. data/Rakefile +3 -42
  6. data/lib/sidekiq/cron/job.rb +273 -144
  7. data/lib/sidekiq/cron/launcher.rb +39 -43
  8. data/lib/sidekiq/cron/locales/de.yml +2 -2
  9. data/lib/sidekiq/cron/locales/en.yml +6 -2
  10. data/lib/sidekiq/cron/locales/it.yml +23 -0
  11. data/lib/sidekiq/cron/locales/ja.yml +18 -0
  12. data/lib/sidekiq/cron/locales/pt.yml +22 -0
  13. data/lib/sidekiq/cron/locales/ru.yml +2 -2
  14. data/lib/sidekiq/cron/locales/zh-CN.yml +19 -0
  15. data/lib/sidekiq/cron/poller.rb +22 -12
  16. data/lib/sidekiq/cron/schedule_loader.rb +22 -0
  17. data/lib/sidekiq/cron/support.rb +8 -1
  18. data/lib/sidekiq/cron/version.rb +7 -0
  19. data/lib/sidekiq/cron/views/cron.erb +38 -28
  20. data/lib/sidekiq/cron/views/cron_show.erb +88 -0
  21. data/lib/sidekiq/cron/web.rb +1 -7
  22. data/lib/sidekiq/cron/web_extension.rb +19 -15
  23. data/lib/sidekiq/cron.rb +1 -0
  24. data/lib/sidekiq/options.rb +25 -0
  25. data/sidekiq-cron.gemspec +23 -108
  26. data/test/integration/performance_test.rb +13 -19
  27. data/test/models/person.rb +21 -0
  28. data/test/test_helper.rb +37 -38
  29. data/test/unit/fixtures/schedule_array.yml +13 -0
  30. data/test/unit/fixtures/schedule_erb.yml +6 -0
  31. data/test/unit/fixtures/schedule_hash.yml +12 -0
  32. data/test/unit/fixtures/schedule_string.yml +1 -0
  33. data/test/unit/job_test.rb +450 -35
  34. data/test/unit/launcher_test.rb +33 -0
  35. data/test/unit/poller_test.rb +28 -37
  36. data/test/unit/schedule_loader_test.rb +58 -0
  37. data/test/unit/web_extension_test.rb +59 -41
  38. metadata +72 -191
  39. data/.document +0 -5
  40. data/.travis.yml +0 -19
  41. data/Changes.md +0 -50
  42. data/Dockerfile +0 -32
  43. data/VERSION +0 -1
  44. data/config.ru +0 -14
  45. data/docker-compose.yml +0 -21
  46. data/examples/web-cron-ui.png +0 -0
  47. data/lib/sidekiq/cron/views/cron.slim +0 -69
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1c9490a07af91904810a5289c0899691f70bb56b
4
- data.tar.gz: 4d79c982c22844a7f7fa887dde61925aaa26e48e
2
+ SHA256:
3
+ metadata.gz: f484cd18cea7b3b00d83b82c5ae6ca7e2c831b22ed4d00cd77a19cfc9491b49d
4
+ data.tar.gz: efe62165d5c2b02b4ee2ce7f1ac877cda1bd06d550f4554fbb340b491f8d1319
5
5
  SHA512:
6
- metadata.gz: c9717f01b61187ffb9cc2024ac3de6a01a1641159ef5f0013f742858a932b01c5a89dcb37abb254484cc1ca9edb6da10cf96fa7d0b2b068199945db18e855c3b
7
- data.tar.gz: a06735e8a699441b6b9a0218e99949710b44dc2a1fdf92725fe865594687e428cd7509efd04023a9a09d5f4580466ab117ad6ccc471ab2797fae804fef9a3ea8
6
+ metadata.gz: a945824e3d39d8f54a33c68eec458f3dee68be2cd43ddf697b7fdd568afff291ff62ee1ec8cf2bbc05e26ddfa6e8230c78a953499b59b6844ca60b37f55ffc99
7
+ data.tar.gz: 2aa57d5d1e2b4ba98199bf79e13d49b7726478650ef46efa92871e0c13b51608829bc66159d8ed5a2cd58f4b6d5dcb3a6e87329795e604309fc60ba85b583758
data/CHANGELOG.md ADDED
@@ -0,0 +1,145 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## 1.10.0
6
+
7
+ - Remove EOL Ruby 2.6 support (https://github.com/sidekiq-cron/sidekiq-cron/pull/399)
8
+ - Add a logo for the project! (https://github.com/sidekiq-cron/sidekiq-cron/pull/402)
9
+ - Added support for ActiveRecord serialize/deserialize using GlobalID (https://github.com/sidekiq-cron/sidekiq-cron/pull/395)
10
+ - Allow for keyword args (`embedded: true`) in Poller (https://github.com/sidekiq-cron/sidekiq-cron/pull/398)
11
+ - Make last_enqueue_time be always an instance of Time (https://github.com/sidekiq-cron/sidekiq-cron/pull/354)
12
+ - Fix argument error problem update from 1.6.0 to newer (https://github.com/sidekiq-cron/sidekiq-cron/pull/392)
13
+ - Clear old jobs while loading the jobs from schedule via the schedule loader (https://github.com/sidekiq-cron/sidekiq-cron/pull/405)
14
+
15
+ ## 1.9.1
16
+
17
+ - Always enqueue via Active Job interface when defined in cron job config (https://github.com/sidekiq-cron/sidekiq-cron/pull/381)
18
+ - Fix schedule.yml YAML load errors on Ruby 3.1 (https://github.com/sidekiq-cron/sidekiq-cron/pull/386)
19
+ - Require Fugit v1.8 to refactor internals (https://github.com/sidekiq-cron/sidekiq-cron/pull/385)
20
+
21
+ ## 1.9.0
22
+
23
+ - Sidekiq v7 support (https://github.com/sidekiq-cron/sidekiq-cron/pull/369)
24
+ - Add support for ERB templates in the auto schedule loader (https://github.com/sidekiq-cron/sidekiq-cron/pull/373)
25
+
26
+ ## 1.8.0
27
+
28
+ - Fix deprecation warnings with redis-rb v4.8.0 (https://github.com/sidekiq-cron/sidekiq-cron/pull/356)
29
+ - Fix poller affecting Sidekiq scheduled set poller (https://github.com/sidekiq-cron/sidekiq-cron/pull/359)
30
+ - Fix default polling interval (https://github.com/sidekiq-cron/sidekiq-cron/pull/362)
31
+ - Add italian locale (https://github.com/sidekiq-cron/sidekiq-cron/pull/367)
32
+ - Allow disabling of cron polling (https://github.com/sidekiq-cron/sidekiq-cron/pull/368)
33
+
34
+ ## 1.7.0
35
+
36
+ - Enable to use cron notation in natural language (ie `every 30 minutes`) (https://github.com/sidekiq-cron/sidekiq-cron/pull/312)
37
+ - Fix `date_as_argument` feature to add timestamp argument at every cron job execution (https://github.com/sidekiq-cron/sidekiq-cron/pull/329)
38
+ - Introduce `Sidekiq::Options` to centralize reading/writing options from different Sidekiq versions (https://github.com/sidekiq-cron/sidekiq-cron/pull/341)
39
+ - Make auto schedule loading compatible with Array format (https://github.com/sidekiq-cron/sidekiq-cron/pull/345)
40
+
41
+ ## 1.6.0
42
+
43
+ - Adds support for auto-loading the `config/schedule.yml` file (https://github.com/sidekiq-cron/sidekiq-cron/pull/337)
44
+ - Fix `Sidekiq.options` deprecation warning (https://github.com/sidekiq-cron/sidekiq-cron/pull/338)
45
+
46
+ ## 1.5.1
47
+
48
+ - Fixes an issue that prevented the gem to work in previous Sidekiq versions (https://github.com/sidekiq-cron/sidekiq-cron/pull/335)
49
+
50
+ ## 1.5.0
51
+
52
+ - Integrate Sidekiq v6.5 breaking changes (https://github.com/sidekiq-cron/sidekiq-cron/pull/331)
53
+ - Add portuguese translations (https://github.com/sidekiq-cron/sidekiq-cron/pull/332)
54
+
55
+ ## 1.4.0
56
+
57
+ - 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)
59
+ - Remove invocation of deprecated Redis functionality (https://github.com/sidekiq-cron/sidekiq-cron/pull/318)
60
+ - Internal code cleanup (https://github.com/sidekiq-cron/sidekiq-cron/pull/317)
61
+ - Optimize gem size (https://github.com/sidekiq-cron/sidekiq-cron/pull/322)
62
+ - Fix "Show All" button on cron jobs view with Sidekiq 6.3.0+ (https://github.com/sidekiq-cron/sidekiq-cron/pull/321)
63
+ - Documentation updates
64
+
65
+ ## 1.3.0
66
+
67
+ - Add confirmation dialog when enquing jobs from UI
68
+ - Start to support Sidekiq `average_scheduled_poll_interval` option (replaced `poll_interval`)
69
+ - Fix deprecation warning for Redis 4.6.x
70
+ - Fix different response from Redis#exists in different Redis versions
71
+ - All PRs:
72
+ - https://github.com/sidekiq-cron/sidekiq-cron/pull/275
73
+ - https://github.com/sidekiq-cron/sidekiq-cron/pull/287
74
+ - https://github.com/sidekiq-cron/sidekiq-cron/pull/309
75
+ - https://github.com/sidekiq-cron/sidekiq-cron/pull/299
76
+ - https://github.com/sidekiq-cron/sidekiq-cron/pull/314
77
+ - https://github.com/sidekiq-cron/sidekiq-cron/pull/288
78
+
79
+ ## 1.2.0
80
+
81
+ - Updated readme
82
+ - Fix problem with Sidekiq::Launcher and requiring it when not needed
83
+ - Better patching of Sidekiq::Launcher
84
+ - Fixed Dockerfile
85
+
86
+ ## 1.1.0
87
+
88
+ - Updated readme
89
+ - Fix unit tests - changed argument error when getting invalid cron format
90
+ - When fallbacking old job enqueued time use `Time.parse` without format (so Ruby can decide best method to parse it)
91
+ - Add option `date_as_argument` which will add to your job arguments on last place `Time.now.to_f` when it was eneuqued
92
+ - Add option `description` which will allow you to add notes to your jobs so in web view you can see it
93
+ - Fixed translations
94
+
95
+ ## 1.0.4
96
+
97
+ - Fix problem with upgrading to 1.0.x - parsing last enqued time didn't count with old time format stored in Redis
98
+
99
+ ## 1.0.0
100
+
101
+ - Use [fugit](https://github.com/floraison/fugit) instead of [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) - API of cron didn't change (rufus scheduler is using fugit)
102
+ - Better working with Timezones
103
+ - Translations for JA, zh-CN
104
+ - Cron without timezone are considered as UTC, to add Timezone to cron use format `* * * * * Europe/Berlin`
105
+ - Be aware that this release can change when your jobs are enqueued (for me it didn't change but it is in one project, in other it can shift by different timezone setup)
106
+
107
+ ## 0.6.0
108
+
109
+ - Set poller to check jobs every 30s by default (possible to override by `Sidekiq.options[:poll_interval] = 10`)
110
+ - Add group actions (enqueue, enable, disable, delete) all in web view
111
+ - Fix poller to enqueu all jobs in poll start time
112
+ - Add performance test for enqueue of jobs (10 000 jobs in less than 19s)
113
+ - Fix problem with default queue
114
+ - Remove `redis-namespace` from dependencies
115
+ - Update Ruby versions in Travis
116
+
117
+ ## 0.5.0
118
+
119
+ - Add Docker support
120
+ - All crons are now evaluated in UTC
121
+ - Fix rufus scheduler & timezones problems
122
+ - Add support for Sidekiq 4.2.1
123
+ - Fix readme
124
+ - Add Russian locale
125
+ - User Rack.env in tests
126
+ - Faster enqueue of jobs
127
+ - Permit to use `ActiveJob::Base.queue_name_delimiter`
128
+ - Fix problem with multiple times enqueue #84
129
+ - Fix problem with enqueue of unknown class
130
+
131
+ ## 0.4.0
132
+
133
+ - Enable to work with Sidekiq >= 4.0.0
134
+ - Fix readme
135
+
136
+ ## 0.3.1
137
+
138
+ - Add CSRF tags to forms so it will work with Sidekiq >= 3.4.2
139
+ - Remove Tilt dependency
140
+
141
+ ## 0.3.0
142
+
143
+ - Suport for Active Job
144
+ - Sidekiq cron web ui needs to be loaded by: require 'sidekiq/cron/web'
145
+ - Add load_from_hash! and load_from_array! which cleanup jobs before adding new ones
data/Gemfile CHANGED
@@ -1,32 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'sidekiq', '>= 4.2.1'
4
- gem 'rufus-scheduler', '>= 3.3.0'
3
+ gemspec
5
4
 
6
- group :development do
7
- gem 'bundler'
8
- gem 'simplecov'
9
-
10
- gem 'redis-namespace', '>= 1.5.2'
11
- gem 'shoulda-context'
12
-
13
- gem 'rack'
14
- gem 'rack-test'
15
-
16
- gem 'jeweler'
17
-
18
- gem 'minitest'
19
- gem 'test-unit'
20
- gem 'sdoc' # sdoc -N .
21
-
22
- gem 'slim'
23
- gem 'sinatra'
24
-
25
- gem 'mocha'
26
- gem 'coveralls'
27
-
28
- gem 'shotgun'
29
-
30
- gem 'guard'
31
- gem 'guard-minitest'
32
- end
5
+ # To test different Sidekiq versions
6
+ gem "sidekiq", ENV.fetch("SIDEKIQ_VERSION", ">= 6")
data/README.md CHANGED
@@ -1,81 +1,115 @@
1
- Sidekiq-Cron [![Gem Version](https://badge.fury.io/rb/sidekiq-cron.svg)](http://badge.fury.io/rb/sidekiq-cron) [![Build Status](https://travis-ci.org/ondrejbartas/sidekiq-cron.svg?branch=master)](https://travis-ci.org/ondrejbartas/sidekiq-cron) [![Coverage Status](https://coveralls.io/repos/ondrejbartas/sidekiq-cron/badge.svg?branch=master)](https://coveralls.io/r/ondrejbartas/sidekiq-cron?branch=master) [![Dependency Status](https://dependencyci.com/github/ondrejbartas/sidekiq-cron/badge)](https://dependencyci.com/github/ondrejbartas/sidekiq-cron)
2
- ================================================================================================================================================================================================================================================================================================================================================================================================================================================
1
+ ![Sidekiq-Cron](logos/cover.png)
3
2
 
4
- [![Join the chat at https://gitter.im/ondrejbartas/sidekiq-cron](https://badges.gitter.im/ondrejbartas/sidekiq-cron.svg)](https://gitter.im/ondrejbartas/sidekiq-cron?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
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)
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)
5
6
 
6
- [Introduction video about Sidekiq-Cron by Drifting Ruby](https://www.driftingruby.com/episodes/periodic-tasks-with-sidekiq-cron)
7
+ > A scheduling add-on for [Sidekiq](https://sidekiq.org/)
7
8
 
8
- A scheduling add-on for [Sidekiq](http://sidekiq.org).
9
+ 🎬 [Introduction video about Sidekiq-Cron by Drifting Ruby](https://www.driftingruby.com/episodes/periodic-tasks-with-sidekiq-cron)
9
10
 
10
- Runs a thread alongside Sidekiq workers to schedule jobs at specified times (using cron notation `* * * * *` parsed by [Rufus-Scheduler](https://github.com/jmettraux/rufus-scheduler), more about [cron notation](http://www.nncron.ru/help/EN/working/cron-format.htm).
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
12
 
12
- Checks for new jobs to schedule every 10 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 worker is running.
13
14
 
14
- Scheduling jobs are added only when at least one Sidekiq process is running.
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.
15
16
 
16
- If you want to know how scheduling work, check out [under the hood](#under-the-hood)
17
+ If you want to know how scheduling work, check out [under the hood](#under-the-hood).
17
18
 
18
- Works with ActiveJob (Rails 4.2+)
19
+ Works with ActiveJob (Rails 4.2+).
19
20
 
20
- You don't need Sidekiq PRO, you can use this gem with plain __Sidekiq__.
21
+ You don't need Sidekiq PRO, you can use this gem with plain Sidekiq.
21
22
 
22
- Requirements
23
- -----------------
23
+ ## Changelog
24
24
 
25
- - Redis 2.8 or greater is required. (Redis 3.0.3 or greater is recommended for large scale use)
26
- - Sidekiq 5, or 4, or 3 and greater is required (for Sidekiq < 4 use version sidekiq-cron 0.3.1)
25
+ Before upgrading to a new version, please read our [Changelog](CHANGELOG.md).
27
26
 
28
- Change Log
29
- ----------
30
- before upgrading to new version, please read:
31
- [Change Log](https://github.com/ondrejbartas/sidekiq-cron/blob/master/Changes.md)
27
+ ## Installation
32
28
 
33
- Installation
34
- ------------
29
+ Install the gem:
35
30
 
36
- $ gem install sidekiq-cron
37
-
38
- or add to your `Gemfile`
39
-
40
- gem "sidekiq-cron", "~> 0.4.0"
31
+ ```
32
+ $ gem install sidekiq-cron
33
+ ```
41
34
 
35
+ Or add to your `Gemfile` and run `bundle install`:
42
36
 
43
- Getting Started
44
- -----------------
37
+ ```ruby
38
+ gem "sidekiq-cron"
39
+ ```
45
40
 
41
+ **NOTE** If you are not using Rails, you need to add `require 'sidekiq-cron'` somewhere after `require 'sidekiq'`.
46
42
 
47
- If you are not using Rails, you need to add `require 'sidekiq-cron'` somewhere after `require 'sidekiq'`.
43
+ ## Getting Started
48
44
 
49
- _Job properties_:
45
+ ### Job properties
50
46
 
51
47
  ```ruby
52
48
  {
53
- 'name' => 'name_of_job', #must be uniq!
54
- 'cron' => '1 * * * *', # execute at 1 minute of every hour, ex: 12:01, 13:01, 14:01, 15:01...etc(HH:MM)
55
- 'class' => 'MyClass',
56
- #OPTIONAL
57
- 'queue' => 'name of queue',
58
- 'args' => '[Array or Hash] of arguments which will be passed to perform method',
59
- 'active_job' => true, # enqueue job through rails 4.2+ active job interface
60
- 'queue_name_prefix' => 'prefix', # rails 4.2+ active job queue with prefix
61
- 'queue_name_delimiter' => '.' # rails 4.2+ active job queue with custom delimiter
49
+ # MANDATORY
50
+ 'name' => 'name_of_job', # must be uniq!
51
+ 'cron' => '1 * * * *', # execute at 1 minute of every hour, ex: 12:01, 13:01, 14:01, ...
52
+ 'class' => 'MyClass',
53
+ # OPTIONAL
54
+ 'queue' => 'name of queue',
55
+ 'args' => '[Array or Hash] of arguments which will be passed to perform method',
56
+ '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'
61
+ 'status' => 'disabled' # default: enabled
62
62
  }
63
63
  ```
64
64
 
65
- ### Time, cron and sidekiq-cron
65
+ ### Time, cron and Sidekiq-Cron
66
+
67
+ For testing your cron notation you can use [crontab.guru](https://crontab.guru).
68
+
69
+ Sidekiq-Cron uses [Fugit](https://github.com/floraison/fugit) to parse the cronline. So please, check Fugit documentation for further information about allowed formats.
70
+
71
+ If using Rails, this is evaluated against the timezone configured in Rails, otherwise the default is UTC.
66
72
 
67
- sidekiq-cron uses [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) to parse the cronline.
68
- By default, the timezone this is evaluated against UTC.
69
73
  If you want to have your jobs enqueued based on a different time zone you can specify a timezone in the cronline,
70
74
  like this `'0 22 * * 1-5 America/Chicago'`.
71
- See [rufus-scheduler documentation](https://github.com/jmettraux/rufus-scheduler#a-note-about-timezones) for more information.
75
+
76
+ #### Natural-language formats
77
+
78
+ Since sidekiq-cron `v1.7.0`, you can use the natural-language formats supported by Fugit, such as:
79
+
80
+ ```rb
81
+ "every day at five" # => '0 5 * * *'
82
+ "every 3 hours" # => '0 */3 * * *'
83
+ ```
84
+
85
+ See [the relevant part of Fugit documentation](https://github.com/floraison/fugit#fugitnat) for details.
86
+
87
+ #### Second-precision (sub-minute) cronlines
88
+
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:
90
+
91
+ `Seconds Minutes Hours Days Months DayOfWeek`
92
+
93
+ For example: `"*/30 * * * * *"` would schedule a job to run every 30 seconds.
94
+
95
+ 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
+
97
+ ```ruby
98
+ Sidekiq::Options[:cron_poll_interval] = 10
99
+ ```
100
+
101
+ The default value at time of writing is 30 seconds. See [under the hood](#under-the-hood) for more details.
72
102
 
73
103
  ### What objects/classes can be scheduled
104
+
74
105
  #### Sidekiq Worker
106
+
75
107
  In this example, we are using `HardWorker` which looks like:
108
+
76
109
  ```ruby
77
110
  class HardWorker
78
111
  include Sidekiq::Worker
112
+
79
113
  def perform(*args)
80
114
  # do something
81
115
  end
@@ -83,7 +117,9 @@ end
83
117
  ```
84
118
 
85
119
  #### Active Job Worker
86
- You can schedule: `ExampleJob` which looks like:
120
+
121
+ You can schedule `ExampleJob` which looks like:
122
+
87
123
  ```ruby
88
124
  class ExampleJob < ActiveJob::Base
89
125
  queue_as :default
@@ -94,17 +130,21 @@ class ExampleJob < ActiveJob::Base
94
130
  end
95
131
  ```
96
132
 
97
- #### Adding Cron job:
98
- ```ruby
133
+ For Active jobs you can use `symbolize_args: true` in `Sidekiq::Cron::Job.create` or in Hash configuration,
134
+ which will ensure that arguments you are passing to it will be symbolized when passed back to `perform` method in worker.
99
135
 
136
+ #### Adding Cron job
137
+
138
+ ```ruby
100
139
  class HardWorker
101
140
  include Sidekiq::Worker
141
+
102
142
  def perform(name, count)
103
143
  # do something
104
144
  end
105
145
  end
106
146
 
107
- Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker') # execute at every 5 minutes, ex: 12:05, 12:10, 12:15...etc
147
+ Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker') # execute at every 5 minutes
108
148
  # => true
109
149
  ```
110
150
 
@@ -119,16 +159,35 @@ else
119
159
  puts job.errors
120
160
  end
121
161
 
122
- #or simple
123
-
162
+ # or simple
124
163
  unless job.save
125
- puts job.errors #will return array of errors
164
+ puts job.errors # will return array of errors
165
+ end
166
+ ```
167
+
168
+ Use ActiveRecord models as arguments
169
+
170
+ ```rb
171
+ class Person < ApplicationRecord
172
+ end
173
+
174
+ class HardWorker < ActiveJob::Base
175
+ queue_as :default
176
+
177
+ def perform(person)
178
+ puts "person: #{person}"
179
+ end
126
180
  end
181
+
182
+
183
+ person = Person.create(id: 1)
184
+ Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker', args: person)
185
+ # => true
127
186
  ```
128
187
 
129
188
  Load more jobs from hash:
130
- ```ruby
131
189
 
190
+ ```ruby
132
191
  hash = {
133
192
  'name_of_job' => {
134
193
  'class' => 'MyClass',
@@ -145,6 +204,7 @@ Sidekiq::Cron::Job.load_from_hash hash
145
204
  ```
146
205
 
147
206
  Load more jobs from array:
207
+
148
208
  ```ruby
149
209
  array = [
150
210
  {
@@ -163,17 +223,17 @@ array = [
163
223
  Sidekiq::Cron::Job.load_from_array array
164
224
  ```
165
225
 
166
- Bang-suffixed methods will remove jobs that are not present in the given hash/array,
167
- update jobs that have the same names, and create new ones when the names are previously unknown.
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.
168
227
 
169
228
  ```ruby
170
- Sidekiq::Cron::Job#load_from_hash! hash
171
- Sidekiq::Cron::Job#load_from_array! array
229
+ Sidekiq::Cron::Job.load_from_hash! hash
230
+ Sidekiq::Cron::Job.load_from_array! array
172
231
  ```
173
232
 
174
- or from YML (same notation as Resque-scheduler)
233
+ Or from YAML (same notation as Resque-scheduler):
234
+
175
235
  ```yaml
176
- #config/schedule.yml
236
+ # config/schedule.yml
177
237
 
178
238
  my_first_job:
179
239
  cron: "*/5 * * * *"
@@ -188,65 +248,80 @@ second_job:
188
248
  hard: "stuff"
189
249
  ```
190
250
 
251
+ There are multiple ways to load the jobs from a YAML file
252
+
253
+ 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"`
255
+ 3. Load the file manually as follows:
256
+
191
257
  ```ruby
192
- #initializers/sidekiq.rb
193
- schedule_file = "config/schedule.yml"
258
+ # config/initializers/sidekiq.rb
194
259
 
195
- if File.exists?(schedule_file) && Sidekiq.server?
196
- Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
260
+ Sidekiq.configure_server do |config|
261
+ config.on(:startup) do
262
+ schedule_file = "config/users_schedule.yml"
263
+
264
+ if File.exist?(schedule_file)
265
+ Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
266
+ end
267
+ end
197
268
  end
198
269
  ```
199
270
 
200
- or you can use for loading jobs from yml file [sidekiq-cron-tasks](https://github.com/coverhound/sidekiq-cron-tasks) which will add rake task `bundle exec rake sidekiq_cron:load` to your rails application.
271
+ ### Finding jobs
201
272
 
202
- #### Finding jobs
203
273
  ```ruby
204
- #return array of all jobs
274
+ # return array of all jobs
205
275
  Sidekiq::Cron::Job.all
206
276
 
207
- #return one job by its unique name - case sensitive
277
+ # return one job by its unique name - case sensitive
208
278
  Sidekiq::Cron::Job.find "Job Name"
209
279
 
210
- #return one job by its unique name - you can use hash with 'name' key
280
+ # return one job by its unique name - you can use hash with 'name' key
211
281
  Sidekiq::Cron::Job.find name: "Job Name"
212
282
 
213
- #if job can't be found nil is returned
283
+ # if job can't be found nil is returned
214
284
  ```
215
285
 
216
- #### Destroy jobs:
286
+ ### Destroy jobs
287
+
217
288
  ```ruby
218
- #destroys all jobs
289
+ # destroy all jobs
219
290
  Sidekiq::Cron::Job.destroy_all!
220
291
 
221
- #destroy job by its name
292
+ # destroy job by its name
222
293
  Sidekiq::Cron::Job.destroy "Job Name"
223
294
 
224
- #destroy found job
295
+ # destroy found job
225
296
  Sidekiq::Cron::Job.find('Job name').destroy
226
297
  ```
227
298
 
228
- #### Work with job:
299
+ ### Work with job
300
+
229
301
  ```ruby
230
302
  job = Sidekiq::Cron::Job.find('Job name')
231
303
 
232
- #disable cron scheduling
304
+ # disable cron scheduling
233
305
  job.disable!
234
306
 
235
- #enable cron scheduling
307
+ # enable cron scheduling
236
308
  job.enable!
237
309
 
238
- #get status of job:
310
+ # get status of job:
239
311
  job.status
240
312
  # => enabled/disabled
241
313
 
242
- #enqueue job right now!
314
+ # enqueue job right now!
243
315
  job.enque!
244
316
  ```
245
317
 
246
- How to start scheduling?
318
+ ### How to start scheduling?
319
+
247
320
  Just start Sidekiq workers by running:
248
321
 
249
- sidekiq
322
+ ```
323
+ $ bundle exec sidekiq
324
+ ```
250
325
 
251
326
  ### Web UI for Cron Jobs
252
327
 
@@ -254,66 +329,45 @@ If you are using Sidekiq's web UI and you would like to add cron jobs too to thi
254
329
  add `require 'sidekiq/cron/web'` after `require 'sidekiq/web'`.
255
330
 
256
331
  With this, you will get:
257
- ![Web UI](https://github.com/ondrejbartas/sidekiq-cron/raw/master/examples/web-cron-ui.png)
258
332
 
259
- ### Forking Processes
260
-
261
- If you're using a forking web server like Unicorn you may run into an issue where the Redis connection is used
262
- before the process forks, causing the following exception
263
-
264
- Redis::InheritedError: Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.
265
-
266
- to occcur. To avoid this, wrap your job creation in the call to `Sidekiq.configure_server`:
267
-
268
- ```ruby
269
- Sidekiq.configure_server do |config|
270
- schedule_file = "config/schedule.yml"
271
-
272
- if File.exists?(schedule_file)
273
- Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
274
- end
275
- end
276
- ```
277
-
278
- Note that this API is only available in Sidekiq 3.x.x.
333
+ ![Web UI](docs/images/web-cron-ui.jpeg)
279
334
 
280
335
  ## Under the hood
281
336
 
282
337
  When you start the Sidekiq process, it starts one thread with `Sidekiq::Poller` instance, which perform the adding of scheduled jobs to queues, retries etc.
283
338
 
284
- Sidekiq-Cron adds itself into this start procedure and starts another thread with `Sidekiq::Cron::Poller` which checks all enabled Sidekiq cron jobs every 10 seconds, if they should be added to queue (their cronline matches time of check).
339
+ Sidekiq-Cron adds itself into this start procedure and starts another thread with `Sidekiq::Cron::Poller` which checks all enabled Sidekiq cron jobs every 30 seconds, if they should be added to queue (their cronline matches time of check).
285
340
 
286
341
  Sidekiq-Cron is checking jobs to be enqueued every 30s by default, you can change it by setting:
342
+
343
+ ```ruby
344
+ Sidekiq::Options[:cron_poll_interval] = 10
287
345
  ```
288
- Sidekiq.options[:poll_interval] = 10
289
- ```
290
346
 
291
- ## Thanks to
292
- * [@7korobi](https://github.com/7korobi)
293
- * [@antulik](https://github.com/antulik)
294
- * [@felixbuenemann](https://github.com/felixbuenemann)
295
- * [@gstark](https://github.com/gstark)
296
- * [@RajRoR](https://github.com/RajRoR)
297
- * [@romeuhcf](https://github.com/romeuhcf)
298
- * [@siruguri](https://github.com/siruguri)
299
- * [@Soliah](https://github.com/Soliah)
300
- * [@stephankaag](https://github.com/stephankaag)
301
- * [@sue445](https://github.com/sue445)
302
- * [@sylg](https://github.com/sylg)
303
- * [@tmeinlschmidt](https://github.com/tmeinlschmidt)
304
- * [@zerobearing2](https://github.com/zerobearing2)
347
+ 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
+
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.
305
350
 
351
+ ## Contributing
306
352
 
307
- ## Contributing to sidekiq-cron
353
+ **Thanks to all [contributors](https://github.com/sidekiq-cron/sidekiq-cron/graphs/contributors), you’re awesome and this wouldn’t be possible without you!**
308
354
 
309
355
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
310
356
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
311
357
  * Fork the project.
312
358
  * Start a feature/bugfix branch.
313
359
  * Commit and push until you are happy with your contribution.
314
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
315
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
360
+ * Make sure to add tests for it. This is important so we don't break it in a future version unintentionally.
361
+ * Open a pull request!
362
+
363
+ ### Testing
364
+
365
+ You can execute the test suite by running:
366
+
367
+ ```
368
+ $ bundle exec rake test
369
+ ```
316
370
 
317
- ## Copyright
371
+ ## License
318
372
 
319
- Copyright (c) 2013 Ondrej Bartas. See LICENSE.txt for further details.
373
+ Copyright (c) 2013 Ondrej Bartas. See [LICENSE](LICENSE.txt) for further details.