cron_swanson 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d117286d60628186a24607e387fb3b51eb4b915d8e75a96d92ebfce8ea51a9ed
4
- data.tar.gz: 7d8fb576c1aead5cbe16c2ccd81f1acda7f96149fb725bd780359a4ba8d2360c
3
+ metadata.gz: 9c4d87324b0351bbf60e5cc586833282618dba613694fd57b22e0e9a7afce9a5
4
+ data.tar.gz: f64d0020a6be996393b40040d506d880ad5d2a0315a530205ec8d834abdeb273
5
5
  SHA512:
6
- metadata.gz: 99a782ff24a871fe8f5ea77f5a4e26c06b0af27519f03873dd62d4e2a5e9a2cd7da478230d3283fcb6b9af28ae03b69d7c3ea64f650683bba7d5f6786017abed
7
- data.tar.gz: e054da7a55fd73c7a0f8c00e4912f13572467840984b45cb7a1d3041a4c13efdbc9ab965fd13748f26a16eea94afcdad86dba227cf1e2c487d52b97dacd199c8
6
+ metadata.gz: 13473ea070dfe8f19fde8f9e0439ab571abbc0eb8274499df442fa21c43da86e5bd26942510156250ac8d474aed73725f1ac510c437da92e9cf855ccf95a6a94
7
+ data.tar.gz: 990e1e3eb6fb5027111646583ecda830c45a38117b1035f748dd553161b8f3bdfacee1bad1e9e5b21d68baf69bc39db4d47fbe4b6599a664f1e90c1711b4c607
data/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
  # Changelog
2
2
 
3
- ## 0.2.0 : "Under my utelage, you will grow from boys to men." : November 3, 2019
3
+ ## 0.3.0 : "Any dog under 50 pounds is a cat and cats are useless." : November 4, 2019
4
+
5
+ New feature:
6
+
7
+ * Support for seed strings, to allow the same job to have distinct schedules
8
+ in various apps.
9
+
10
+ Breaking changes:
11
+
12
+ * `whenever` integration changed from class methods to instance methods.
13
+ * Some methods were renamed.
14
+
15
+ ## 0.2.0 : "Under my tutelage, you will grow from boys to men." : November 3, 2019
4
16
 
5
17
  Improvements in whenever integration.
6
18
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # CronSwanson
2
2
 
3
- `CronSwanson` helps schedule cron jobs.
3
+ `CronSwanson` helps distribute schedule cron jobs.
4
4
 
5
5
  ![Never half-ass two things.](whole-ass.jpg)
6
6
 
@@ -32,24 +32,24 @@ Or install it yourself as:
32
32
 
33
33
  ## Usage
34
34
 
35
- ### schedule
35
+ ### build_schedule
36
36
 
37
- Use `schedule` to build a cron schedule. The supplied string is hashed to determine
38
- the run time of the job.
37
+ Use `build_schedule` to build a cron schedule string. The supplied string is hashed
38
+ to determine the run time of the job.
39
39
 
40
40
  ```ruby
41
- CronSwanson.schedule 'whiskey'
41
+ CronSwanson.build_schedule 'whiskey'
42
42
  #=> "33 18 * * *"
43
43
  ```
44
44
 
45
45
  **To keep two applications running the same job from executing at once**, make the
46
- application name part of the schedule key.
46
+ application name part of the build_schedule key.
47
47
 
48
48
  ```ruby
49
- CronSwanson.schedule 'application-a whiskey'
49
+ CronSwanson.build_schedule 'application-a whiskey'
50
50
  #=> "4 19 * * *"
51
51
 
52
- CronSwanson.schedule 'application-b whiskey'
52
+ CronSwanson.build_schedule 'application-b whiskey'
53
53
  #=> "11 7 * * *"
54
54
  ```
55
55
 
@@ -57,14 +57,14 @@ An `interval` (in seconds) can be supplied if you want a job to be run more than
57
57
  once/day. This `interval` must be a factor of 24 hours.
58
58
 
59
59
  ```ruby
60
- CronSwanson.schedule 'bacon', interval: 60 * 60 * 4
60
+ CronSwanson.build_schedule 'bacon', interval: 60 * 60 * 4
61
61
  #=> "26 2,6,10,14,18,22 * * *"
62
62
  ```
63
63
 
64
64
  You can also use `ActiveSupport::Duration` instances.
65
65
 
66
66
  ```ruby
67
- CronSwanson.schedule 'bacon', interval: 4.hours
67
+ CronSwanson.build_schedule 'bacon', interval: 4.hours
68
68
  #=> "26 2,6,10,14,18,22 * * *"
69
69
  ```
70
70
 
@@ -72,17 +72,18 @@ CronSwanson.schedule 'bacon', interval: 4.hours
72
72
 
73
73
  `CronSwanson` is built to integrate with the fantastic [whenever](https://github.com/javan/whenever) gem.
74
74
 
75
- `CronSwanson::Whenever.add` will calculate a run time for jobs by hashing the text
76
- of the job definitions in the given block.
75
+ `#schedule` will calculate a run time for jobs by hashing the text of the job
76
+ definitions in the given block. (Plus an optional seed string if one is supplied.)
77
77
 
78
- **NOTE**: This means that if you change the jobs in the block, you will also change the schedule time
79
- for these jobs.
78
+ **NOTE**: This means that if you change the jobs in the block, or the seed, you will also
79
+ change the schedule time for these jobs.
80
80
 
81
81
  #### Daily
82
82
 
83
83
  ```ruby
84
- # in config/schedule.rb
85
- CronSwanson::Whenever.add(self) do
84
+ # in config/build_schedule.rb
85
+ swanson = CronSwanson::Whenever.new(self, seed: 'application-name')
86
+ swanson.schedule do
86
87
  rake 'sample:job'
87
88
  end
88
89
  ```
@@ -93,15 +94,17 @@ determined by `CronSwanson`.
93
94
  #### Multiple times/day
94
95
 
95
96
  ```ruby
96
- # in config/schedule.rb
97
+ # in config/build_schedule.rb
97
98
 
98
99
  # with ActiveSupport
99
- CronSwanson::Whenever.add(self, interval: 4.hours) do
100
+ swanson = CronSwanson::Whenever.new(self, seed: 'application-name')
101
+ swanson.schedule(interval: 4.hours) do
100
102
  rake 'sample:job'
101
103
  end
102
104
 
103
105
  # without ActiveSupport
104
- CronSwanson::Whenever.add(self, interval: 60 * 60 * 4) do
106
+ swanson = CronSwanson::Whenever.new(self, seed: 'application-name')
107
+ swanson.schedule(interval: 60 * 60 * 4) do
105
108
  rake 'sample:job'
106
109
  end
107
110
  ```
@@ -111,10 +114,11 @@ end
111
114
  Any custom job types which have been defined will work.
112
115
 
113
116
  ```ruby
114
- # in config/schedule.rb
117
+ # in config/build_schedule.rb
115
118
  job_type :ron, '/usr/bin/ron :task'
116
119
 
117
- CronSwanson::Whenever.add(self) do
120
+ swanson = CronSwanson::Whenever.new(self, seed: 'application-name')
121
+ swanson.schedule do
118
122
  ron 'bacon whiskey'
119
123
  end
120
124
  ```
@@ -125,16 +129,36 @@ Roles are supported. See the [whenever documentation](https://github.com/javan/w
125
129
  for more information on this.
126
130
 
127
131
  ```ruby
128
- CronSwanson::Whenever.add(self) do
132
+ swanson = CronSwanson::Whenever.new(self, seed: 'application-name')
133
+ swanson.schedule do
129
134
  rake 'will_run_on_all_roles'
130
135
  end
131
136
 
132
- # will only be added to servers with the :restricted role
133
- CronSwanson::Whenever.add(self, roles: [:restricted]) do
137
+ # Added to servers with the :restricted role
138
+ swanson.schedule(roles: [:restricted]) do
134
139
  rake 'restricted_only'
135
140
  end
136
141
  ```
137
142
 
143
+ #### seeds
144
+
145
+ Varying the seed string affects all jobs scheduled by that instance.
146
+
147
+ ```ruby
148
+ swanson = CronSwanson::Whenever.new(self, seed: 'application-a')
149
+ swanson.schedule do
150
+ rake 'job'
151
+ end
152
+
153
+ swanson = CronSwanson::Whenever.new(self, seed: 'application-b')
154
+ swanson.schedule do
155
+ rake 'job'
156
+ end
157
+ ```
158
+
159
+ In this example, the job being scheduled is the same, but the schedule times will
160
+ be different because the seed string is different.
161
+
138
162
  ## Development
139
163
 
140
164
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/cron_swanson.rb CHANGED
@@ -36,7 +36,15 @@ module CronSwanson
36
36
  offset_seconds
37
37
  end
38
38
 
39
- def self.schedule(job_identifier, interval: default_interval)
39
+ # generate a cron schedule string
40
+ #
41
+ # the same input will always produce the same output.
42
+ #
43
+ # @param [String] job_identifier a job to generate a schedule for
44
+ # @param [Integer, ActiveSupport::Duration] interval how often should the job
45
+ # be scheduled to run?
46
+ # @return [String] a schedule string like '38 4 * * *'
47
+ def self.build_schedule(job_identifier, interval: default_interval)
40
48
  if interval > SECONDS_PER_DAY
41
49
  raise ArgumentError, "interval must be less than 1 day (#{SECONDS_PER_DAY} seconds)."
42
50
  end
@@ -1,3 +1,3 @@
1
1
  module CronSwanson
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,13 +1,30 @@
1
1
  module CronSwanson
2
2
  # integration for the whenever gem: https://github.com/javan/whenever
3
3
  class Whenever
4
+ # a seed string which should be unique per application
5
+ #
6
+ # causes unique hashes (ie: schedules) to be generated by multiple apps all
7
+ # scheduling the same job.
8
+ attr_reader :seed
9
+
10
+ def initialize(whenever_job_list, seed: '')
11
+ if !whenever_job_list.is_a?(::Whenever::JobList)
12
+ raise ArgumentError, "supply a Whenever::JobList. (In schedule.rb code, use `self`.)"
13
+ end
14
+
15
+ @whenever_job_list = whenever_job_list
16
+ @seed = seed
17
+ @in_schedule_method = false
18
+ @whenever_jobs = []
19
+ end
20
+
4
21
  # CronSwanson integration for whenever
5
22
  #
6
23
  # The given block can use any job types understood by your whenever configuration.
7
24
  # See https://github.com/javan/whenever#define-your-own-job-types.
8
25
  #
9
26
  # CronSwanson currently uses the location it is invoked from in schedule.rb
10
- # to calculate a job time. This means that moving the `.add` invocation to
27
+ # to calculate a job time. This means that moving the `.schedule` invocation to
11
28
  # a different line in schedule.rb will cause it to be run at a different time.
12
29
  #
13
30
  # This limitation exists because I (currently) don't know of a way to inspect
@@ -16,7 +33,7 @@ module CronSwanson
16
33
  #
17
34
  # @example run a job once/day
18
35
  # # in config/schedule.rb
19
- # CronSwanson::Whenever.add(self) do
36
+ # CronSwanson::Whenever.schedule(self) do
20
37
  # rake 'job'
21
38
  # end
22
39
  #
@@ -24,18 +41,18 @@ module CronSwanson
24
41
  # # in config/schedule.rb
25
42
  #
26
43
  # # with ActiveSupport
27
- # CronSwanson::Whenever.add(self, interval: 4.hours) do
44
+ # CronSwanson::Whenever.schedule(self, interval: 4.hours) do
28
45
  # rake 'job'
29
46
  # end
30
47
  #
31
48
  # # without ActiveSupport
32
- # CronSwanson::Whenever.add(self, interval: 60 * 60 * 4) do
49
+ # CronSwanson::Whenever.schedule(self, interval: 60 * 60 * 4) do
33
50
  # rake 'job'
34
51
  # end
35
52
  #
36
53
  # @example run a job only on servers with a given role
37
54
  # # in config/schedule.rb
38
- # CronSwanson::Whenever.add(self, roles: [:app]) do
55
+ # CronSwanson::Whenever.schedule(self, roles: [:app]) do
39
56
  # rake 'job'
40
57
  # end
41
58
  #
@@ -44,13 +61,9 @@ module CronSwanson
44
61
  # @param [Integer, ActiveSupport::Duration] interval how many seconds do you want between runs
45
62
  # of this job
46
63
  # @param [Array<Symbol>] roles capistrano roles that jobs in this block should be deployed to
47
- def self.add(whenever_job_list, interval: CronSwanson.default_interval, roles: [], &block)
64
+ def schedule(interval: CronSwanson.default_interval, roles: [], &block)
65
+ @in_schedule_method = true
48
66
  @whenever_jobs = []
49
- @whenever_job_list = whenever_job_list
50
-
51
- if !whenever_job_list.is_a?(::Whenever::JobList)
52
- raise ArgumentError, "supply a Whenever::JobList. (In schedule.rb code, use `self`.)"
53
- end
54
67
 
55
68
  raise ArgumentError, "provide a block containing jobs to schedule." if !block_given?
56
69
 
@@ -64,23 +77,23 @@ module CronSwanson
64
77
  m, args, _block = *job_config
65
78
  "#{m} #{args.join}"
66
79
  end
67
- schedule = CronSwanson.schedule(schedule_seed, interval: interval)
80
+ schedule = CronSwanson.build_schedule(@seed + schedule_seed.join, interval: interval)
68
81
 
69
82
  # now that we know when to schedule the jobs, actually pass the block to Whenever
70
83
  if roles.size > 0
71
- whenever_job_list.every(schedule, roles: roles, &Proc.new)
84
+ @whenever_job_list.every(schedule, roles: roles, &Proc.new)
72
85
  else
73
- whenever_job_list.every(schedule, &Proc.new)
86
+ @whenever_job_list.every(schedule, &Proc.new)
74
87
  end
75
88
 
76
- @whenever_job_list = nil
89
+ @in_schedule_method = false
77
90
  end
78
91
 
79
- # during .add, we accumulate calls to whenever job types
92
+ # during .schedule, we accumulate calls to whenever job types
80
93
  # this allows us to make a schedule hash from the actual jobs which are defined.
81
- def self.method_missing(m, *args, &block)
82
- if @whenever_job_list.nil? || @whenever_jobs.nil?
83
- raise "#{self.name}.method_missing invoked outside of #{self.name}.add"
94
+ def method_missing(m, *args, &block)
95
+ if !@in_schedule_method
96
+ raise "#{self.class.name}#method_missing invoked outside of #{self.class.name}#schedule"
84
97
  end
85
98
 
86
99
  if @whenever_job_list.respond_to?(m)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cron_swanson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dean
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-03 00:00:00.000000000 Z
11
+ date: 2019-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,8 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 2.7.6.2
86
+ rubygems_version: 3.0.3
88
87
  signing_key:
89
88
  specification_version: 4
90
89
  summary: distribute common cron jobs so they don't all start at the same moment