zhong 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rubocop.yml +216 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/zhong.rb +22 -0
- data/lib/zhong/at.rb +70 -0
- data/lib/zhong/every.rb +39 -0
- data/lib/zhong/job.rb +122 -0
- data/lib/zhong/scheduler.rb +76 -0
- data/lib/zhong/version.rb +3 -0
- data/test/minitest_helper.rb +4 -0
- data/test/zhong_test.rb +7 -0
- data/zhong.gemspec +35 -0
- metadata +179 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cdb0c3e763f67f1b0a6401a13cd9090fa3a7a45a
|
4
|
+
data.tar.gz: 77c008fecedfabc3994d95a621a0c2dd7e0fa786
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fccf0555dc69c9ef3cfffa60050e0af92fada4d294047bcdac7d36e95e1a98528f4a1f7533da179c4203e5db39db623df72f5be9459c61fb9cb9f84288ec6d7a
|
7
|
+
data.tar.gz: d9063ca1237fd65f9abbabe979788483174bda374670da24050b4bed064d90ab2cbbe6882ca89590c9e2b3690ccc69c50eb933ab34a239cdd326ae718f31172f
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- .git/**/*
|
4
|
+
- tmp/**/*
|
5
|
+
- zhong.gemspec
|
6
|
+
|
7
|
+
Lint/DuplicateMethods:
|
8
|
+
Enabled: true
|
9
|
+
|
10
|
+
Lint/DeprecatedClassMethods:
|
11
|
+
Enabled: true
|
12
|
+
|
13
|
+
Style/TrailingWhitespace:
|
14
|
+
Enabled: true
|
15
|
+
|
16
|
+
Style/Tab:
|
17
|
+
Enabled: true
|
18
|
+
|
19
|
+
Style/TrailingBlankLines:
|
20
|
+
Enabled: true
|
21
|
+
|
22
|
+
Style/NilComparison:
|
23
|
+
Enabled: true
|
24
|
+
|
25
|
+
Style/NonNilCheck:
|
26
|
+
Enabled: true
|
27
|
+
|
28
|
+
Style/Not:
|
29
|
+
Enabled: true
|
30
|
+
|
31
|
+
Style/RedundantReturn:
|
32
|
+
Enabled: true
|
33
|
+
|
34
|
+
Style/ClassCheck:
|
35
|
+
Enabled: true
|
36
|
+
|
37
|
+
Style/EmptyLines:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
Style/EmptyLiteral:
|
41
|
+
Enabled: true
|
42
|
+
|
43
|
+
Style/Alias:
|
44
|
+
Enabled: true
|
45
|
+
|
46
|
+
Style/MethodCallParentheses:
|
47
|
+
Enabled: true
|
48
|
+
|
49
|
+
Style/MethodDefParentheses:
|
50
|
+
Enabled: true
|
51
|
+
|
52
|
+
Style/SpaceBeforeBlockBraces:
|
53
|
+
Enabled: true
|
54
|
+
|
55
|
+
Style/SpaceInsideBlockBraces:
|
56
|
+
Enabled: true
|
57
|
+
|
58
|
+
Style/SpaceInsideParens:
|
59
|
+
Enabled: true
|
60
|
+
|
61
|
+
Style/DeprecatedHashMethods:
|
62
|
+
Enabled: true
|
63
|
+
|
64
|
+
Style/HashSyntax:
|
65
|
+
Enabled: true
|
66
|
+
|
67
|
+
Style/SpaceInsideHashLiteralBraces:
|
68
|
+
Enabled: true
|
69
|
+
EnforcedStyle: no_space
|
70
|
+
|
71
|
+
Style/SpaceInsideBrackets:
|
72
|
+
Enabled: true
|
73
|
+
|
74
|
+
Style/AndOr:
|
75
|
+
Enabled: false
|
76
|
+
|
77
|
+
Style/TrailingComma:
|
78
|
+
Enabled: true
|
79
|
+
|
80
|
+
Style/SpaceBeforeComma:
|
81
|
+
Enabled: true
|
82
|
+
|
83
|
+
Style/SpaceBeforeComment:
|
84
|
+
Enabled: true
|
85
|
+
|
86
|
+
Style/SpaceBeforeSemicolon:
|
87
|
+
Enabled: true
|
88
|
+
|
89
|
+
Style/SpaceAroundBlockParameters:
|
90
|
+
Enabled: true
|
91
|
+
|
92
|
+
Style/SpaceAroundOperators:
|
93
|
+
Enabled: true
|
94
|
+
|
95
|
+
Style/SpaceAfterColon:
|
96
|
+
Enabled: true
|
97
|
+
|
98
|
+
Style/SpaceAfterComma:
|
99
|
+
Enabled: true
|
100
|
+
|
101
|
+
Style/SpaceAfterControlKeyword:
|
102
|
+
Enabled: true
|
103
|
+
|
104
|
+
Style/SpaceAfterNot:
|
105
|
+
Enabled: true
|
106
|
+
|
107
|
+
Style/SpaceAfterSemicolon:
|
108
|
+
Enabled: true
|
109
|
+
|
110
|
+
Lint/UselessComparison:
|
111
|
+
Enabled: true
|
112
|
+
|
113
|
+
Lint/InvalidCharacterLiteral:
|
114
|
+
Enabled: true
|
115
|
+
|
116
|
+
Lint/LiteralInInterpolation:
|
117
|
+
Enabled: true
|
118
|
+
|
119
|
+
Lint/LiteralInCondition:
|
120
|
+
Enabled: true
|
121
|
+
|
122
|
+
Lint/UnusedBlockArgument:
|
123
|
+
Enabled: true
|
124
|
+
|
125
|
+
Style/VariableInterpolation:
|
126
|
+
Enabled: true
|
127
|
+
|
128
|
+
Style/RedundantSelf:
|
129
|
+
Enabled: true
|
130
|
+
|
131
|
+
Style/ParenthesesAroundCondition:
|
132
|
+
Enabled: true
|
133
|
+
|
134
|
+
Style/WhileUntilDo:
|
135
|
+
Enabled: true
|
136
|
+
|
137
|
+
Style/EmptyLineBetweenDefs:
|
138
|
+
Enabled: true
|
139
|
+
|
140
|
+
Style/EmptyLinesAroundAccessModifier:
|
141
|
+
Enabled: true
|
142
|
+
|
143
|
+
Style/EmptyLinesAroundMethodBody:
|
144
|
+
Enabled: true
|
145
|
+
|
146
|
+
Style/ColonMethodCall:
|
147
|
+
Enabled: true
|
148
|
+
|
149
|
+
Lint/SpaceBeforeFirstArg:
|
150
|
+
Enabled: true
|
151
|
+
|
152
|
+
Lint/UnreachableCode:
|
153
|
+
Enabled: true
|
154
|
+
|
155
|
+
Style/UnlessElse:
|
156
|
+
Enabled: true
|
157
|
+
|
158
|
+
Style/ClassVars:
|
159
|
+
Enabled: true
|
160
|
+
|
161
|
+
Style/StringLiterals:
|
162
|
+
Enabled: true
|
163
|
+
EnforcedStyle: double_quotes
|
164
|
+
|
165
|
+
Metrics/CyclomaticComplexity:
|
166
|
+
Max: 8
|
167
|
+
|
168
|
+
Metrics/LineLength:
|
169
|
+
Max: 128
|
170
|
+
|
171
|
+
Metrics/MethodLength:
|
172
|
+
Max: 32
|
173
|
+
|
174
|
+
Metrics/PerceivedComplexity:
|
175
|
+
Max: 8
|
176
|
+
|
177
|
+
# Disabled
|
178
|
+
|
179
|
+
Style/EvenOdd:
|
180
|
+
Enabled: false
|
181
|
+
|
182
|
+
Style/AsciiComments:
|
183
|
+
Enabled: false
|
184
|
+
|
185
|
+
Style/NumericLiterals:
|
186
|
+
Enabled: false
|
187
|
+
|
188
|
+
Style/UnneededPercentQ:
|
189
|
+
Enabled: false
|
190
|
+
|
191
|
+
Style/SpecialGlobalVars:
|
192
|
+
Enabled: false
|
193
|
+
|
194
|
+
Style/TrivialAccessors:
|
195
|
+
Enabled: false
|
196
|
+
|
197
|
+
Style/PerlBackrefs:
|
198
|
+
Enabled: false
|
199
|
+
|
200
|
+
Metrics/AbcSize:
|
201
|
+
Enabled: false
|
202
|
+
|
203
|
+
Metrics/BlockNesting:
|
204
|
+
Enabled: false
|
205
|
+
|
206
|
+
Metrics/ClassLength:
|
207
|
+
Enabled: false
|
208
|
+
|
209
|
+
Metrics/MethodLength:
|
210
|
+
Enabled: false
|
211
|
+
|
212
|
+
Metrics/ParameterLists:
|
213
|
+
Enabled: false
|
214
|
+
|
215
|
+
Metrics/PerceivedComplexity:
|
216
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Nick Elser
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Zhong
|
2
|
+
|
3
|
+
Useful, reliable distributed cron.
|
4
|
+
|
5
|
+
# Installation
|
6
|
+
|
7
|
+
Add this line to your application’s Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'zhong'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
r = Redis.new
|
17
|
+
|
18
|
+
Zhong.schedule(redis: r) do
|
19
|
+
category "stuff" do
|
20
|
+
every(5.seconds, "foo") { puts "foo" }
|
21
|
+
every(1.week, "baz", at: "mon 22:45") { puts "baz" }
|
22
|
+
end
|
23
|
+
|
24
|
+
category "clutter" do
|
25
|
+
every(1.second, "compute", if: -> (t) { rand < 0.5 }) { puts "something happened" }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
## TODO
|
31
|
+
- better logging
|
32
|
+
- error handling
|
33
|
+
- tests
|
34
|
+
- examples
|
35
|
+
- callbacks
|
36
|
+
- generic handler
|
37
|
+
|
38
|
+
## History
|
39
|
+
|
40
|
+
View the [changelog](https://github.com/nickelser/zhong/blob/master/CHANGELOG.md).
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
45
|
+
|
46
|
+
- [Report bugs](https://github.com/nickelser/zhong/issues)
|
47
|
+
- Fix bugs and [submit pull requests](https://github.com/nickelser/zhong/pulls)
|
48
|
+
- Write, clarify, or fix documentation
|
49
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zhong"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/zhong.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "logger"
|
2
|
+
require "redis"
|
3
|
+
require "suo"
|
4
|
+
require "active_support/time"
|
5
|
+
|
6
|
+
require "zhong/version"
|
7
|
+
|
8
|
+
require "zhong/at"
|
9
|
+
require "zhong/every"
|
10
|
+
|
11
|
+
require "zhong/job"
|
12
|
+
require "zhong/scheduler"
|
13
|
+
|
14
|
+
module Zhong
|
15
|
+
class << self
|
16
|
+
def schedule(**opts, &block)
|
17
|
+
@scheduler = Scheduler.new(opts)
|
18
|
+
@scheduler.instance_eval(&block)
|
19
|
+
@scheduler.start
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/zhong/at.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Zhong
|
2
|
+
# Strongly inspired by the Clockwork At class
|
3
|
+
class At
|
4
|
+
class FailedToParse < StandardError; end
|
5
|
+
|
6
|
+
WDAYS = %w(sunday monday tuesday wednesday thursday friday saturday).each.with_object({}).with_index do |(w, wdays), index|
|
7
|
+
[w, w[0...3]].each do |k|
|
8
|
+
wdays[k] = index
|
9
|
+
|
10
|
+
if k == "tue"
|
11
|
+
wdays["tues"] = index
|
12
|
+
elsif k == "thu"
|
13
|
+
wdays["thr"] = index
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end.freeze
|
17
|
+
|
18
|
+
attr_accessor :minute, :hour, :wday
|
19
|
+
|
20
|
+
def initialize(minute: nil, hour: nil, wday: nil, grace: 0.minutes)
|
21
|
+
@minute = minute
|
22
|
+
@hour = hour
|
23
|
+
@wday = wday
|
24
|
+
@grace = grace
|
25
|
+
end
|
26
|
+
|
27
|
+
def next_at(time = Time.now)
|
28
|
+
at_time = @wday.nil? ? time.dup : (time + (@wday - time.wday).days)
|
29
|
+
|
30
|
+
at_time = at_time.change(hour: @hour, min: @minute)
|
31
|
+
|
32
|
+
if at_time < @grace.ago
|
33
|
+
if @wday.nil?
|
34
|
+
at_time += 1.day
|
35
|
+
else
|
36
|
+
at_time += 1.week
|
37
|
+
end
|
38
|
+
else
|
39
|
+
at_time
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.parse(at, grace: 0)
|
44
|
+
return unless at
|
45
|
+
|
46
|
+
case at
|
47
|
+
when /\A([[:alpha:]]+)\s+(.*)\z/
|
48
|
+
wday = WDAYS[$1]
|
49
|
+
|
50
|
+
if wday
|
51
|
+
parsed_time = parse($2, grace: grace)
|
52
|
+
parsed_time.wday = wday
|
53
|
+
parsed_time
|
54
|
+
else
|
55
|
+
fail FailedToParse, at
|
56
|
+
end
|
57
|
+
when /\A(\d{1,2}):(\d\d)\z/
|
58
|
+
new(minute: $2.to_i, hour: $1.to_i, grace: grace)
|
59
|
+
when /\A\*{1,2}:(\d\d)\z/
|
60
|
+
new(minute: $1.to_i, grace: grace)
|
61
|
+
when /\A(\d{1,2}):\*{1,2}\z/
|
62
|
+
new(hour: $1, grace: grace)
|
63
|
+
else
|
64
|
+
fail FailedToParse, at
|
65
|
+
end
|
66
|
+
rescue ArgumentError
|
67
|
+
throw FailedToParse, at
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/zhong/every.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module Zhong
|
2
|
+
class Every
|
3
|
+
class FailedToParse < StandardError; end
|
4
|
+
|
5
|
+
EVERY_KEYWORDS = {
|
6
|
+
day: 1.day,
|
7
|
+
week: 1.week,
|
8
|
+
month: 1.month,
|
9
|
+
semiannual: 6.months, # enterprise!
|
10
|
+
year: 1.year,
|
11
|
+
decade: 10.year
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
def initialize(period)
|
15
|
+
@period = period
|
16
|
+
end
|
17
|
+
|
18
|
+
def next_at(last = Time.now)
|
19
|
+
last + @period
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.parse(every)
|
23
|
+
return unless every
|
24
|
+
|
25
|
+
case every
|
26
|
+
when Numeric, ActiveSupport::Duration
|
27
|
+
new(every)
|
28
|
+
when String, Symbol
|
29
|
+
key = every.downcase.to_sym
|
30
|
+
|
31
|
+
fail FailedToParse, every unless EVERY_KEYWORDS.key?(key)
|
32
|
+
|
33
|
+
new(EVERY_KEYWORDS[key])
|
34
|
+
else
|
35
|
+
fail FailedToParse, every
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/zhong/job.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
module Zhong
|
2
|
+
class Job
|
3
|
+
attr_reader :name, :category
|
4
|
+
|
5
|
+
def initialize(scheduler:, name:, every: nil, at: nil, only_if: nil, category: nil, &block)
|
6
|
+
@name = name
|
7
|
+
@category = category
|
8
|
+
|
9
|
+
@at = At.parse(at, grace: scheduler.config[:grace])
|
10
|
+
@every = Every.parse(every)
|
11
|
+
|
12
|
+
if @at && !@every
|
13
|
+
@logger.error "warning: #{self} has `at` but no `every`; could run far more often than expected!"
|
14
|
+
end
|
15
|
+
|
16
|
+
@block = block
|
17
|
+
@redis = scheduler.config[:redis]
|
18
|
+
@logger = scheduler.config[:logger]
|
19
|
+
@tz = scheduler.config[:tz]
|
20
|
+
@if = only_if
|
21
|
+
@lock = Suo::Client::Redis.new(lock_key, client: @redis, stale_lock_expiration: scheduler.config[:long_running_timeout])
|
22
|
+
@timeout = 5
|
23
|
+
|
24
|
+
refresh_last_ran
|
25
|
+
end
|
26
|
+
|
27
|
+
def run?(time = Time.now)
|
28
|
+
run_every?(time) && run_at?(time) && run_if?(time)
|
29
|
+
end
|
30
|
+
|
31
|
+
def run(time = Time.now)
|
32
|
+
return unless run?(time)
|
33
|
+
|
34
|
+
if running?
|
35
|
+
@logger.info "already running: #{self}"
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
ran_set = @lock.lock do
|
40
|
+
refresh_last_ran
|
41
|
+
|
42
|
+
break true unless run?(time)
|
43
|
+
|
44
|
+
if disabled?
|
45
|
+
@logger.info "disabled: #{self}"
|
46
|
+
break true
|
47
|
+
end
|
48
|
+
|
49
|
+
@logger.info "running: #{self}"
|
50
|
+
|
51
|
+
@thread = Thread.new { @block.call } if @block
|
52
|
+
|
53
|
+
ran!(time)
|
54
|
+
end
|
55
|
+
|
56
|
+
@logger.info "unable to acquire exclusive run lock: #{self}" unless ran_set
|
57
|
+
end
|
58
|
+
|
59
|
+
def stop
|
60
|
+
return unless running?
|
61
|
+
Thread.new { @logger.error "killing #{self} due to stop" } # thread necessary due to trap context
|
62
|
+
@thread.join(@timeout)
|
63
|
+
@thread.kill
|
64
|
+
end
|
65
|
+
|
66
|
+
def running?
|
67
|
+
@thread && @thread.alive?
|
68
|
+
end
|
69
|
+
|
70
|
+
def refresh_last_ran
|
71
|
+
last_ran_val = @redis.get(run_time_key)
|
72
|
+
@last_ran = last_ran_val ? Time.at(last_ran_val.to_i) : nil
|
73
|
+
end
|
74
|
+
|
75
|
+
def disable
|
76
|
+
@redis.set(disabled_key, "true")
|
77
|
+
end
|
78
|
+
|
79
|
+
def enable
|
80
|
+
@redis.del(disabled_key)
|
81
|
+
end
|
82
|
+
|
83
|
+
def disabled?
|
84
|
+
!!@redis.get(disabled_key)
|
85
|
+
end
|
86
|
+
|
87
|
+
def to_s
|
88
|
+
[@category, @name].compact.join(".")
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def run_every?(time)
|
94
|
+
!@last_ran || !@every || @every.next_at(@last_ran) <= time
|
95
|
+
end
|
96
|
+
|
97
|
+
def run_at?(time)
|
98
|
+
!@at || @at.next_at(time) <= time
|
99
|
+
end
|
100
|
+
|
101
|
+
def run_if?(time)
|
102
|
+
!@if || @if.call(time)
|
103
|
+
end
|
104
|
+
|
105
|
+
def ran!(time)
|
106
|
+
@last_ran = time
|
107
|
+
@redis.set(run_time_key, @last_ran.to_i)
|
108
|
+
end
|
109
|
+
|
110
|
+
def run_time_key
|
111
|
+
"zhong:last_ran:#{self}"
|
112
|
+
end
|
113
|
+
|
114
|
+
def disabled_key
|
115
|
+
"zhong:disabled:#{self}"
|
116
|
+
end
|
117
|
+
|
118
|
+
def lock_key
|
119
|
+
"zhong:lock:#{self}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Zhong
|
2
|
+
class Scheduler
|
3
|
+
attr_reader :config, :redis, :jobs
|
4
|
+
|
5
|
+
def initialize(config = {})
|
6
|
+
@jobs = {}
|
7
|
+
@config = {timeout: 0.5, grace: 15.minutes, long_running_timeout: 5.minutes}.merge(config)
|
8
|
+
@logger = @config[:logger] ||= self.class.default_logger
|
9
|
+
@redis = @config[:redis] ||= Redis.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def category(name)
|
13
|
+
@category = name
|
14
|
+
|
15
|
+
yield
|
16
|
+
|
17
|
+
@category = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def every(period, name, opts = {}, &block)
|
21
|
+
add(Job.new(scheduler: self, name: name, every: period, at: opts[:at], only_if: opts[:if], category: @category, &block))
|
22
|
+
end
|
23
|
+
|
24
|
+
def start
|
25
|
+
%w(QUIT INT TERM).each do |sig|
|
26
|
+
Signal.trap(sig) { stop }
|
27
|
+
end
|
28
|
+
|
29
|
+
@logger.info "starting at #{redis_time}"
|
30
|
+
|
31
|
+
loop do
|
32
|
+
now = redis_time
|
33
|
+
|
34
|
+
jobs.each { |_, job| job.run(now) }
|
35
|
+
|
36
|
+
sleep(interval)
|
37
|
+
|
38
|
+
break if @stop
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def stop
|
43
|
+
Thread.new { @logger.error "stopping" } # thread necessary due to trap context
|
44
|
+
@stop = true
|
45
|
+
jobs.values.each(&:stop)
|
46
|
+
Thread.new { @logger.info "stopped" }
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def add(job)
|
52
|
+
if @jobs.key?(job.to_s)
|
53
|
+
@logger.error "duplicate job #{job}, skipping"
|
54
|
+
return
|
55
|
+
end
|
56
|
+
|
57
|
+
@jobs[job.to_s] = job
|
58
|
+
end
|
59
|
+
|
60
|
+
def interval
|
61
|
+
1.0 - Time.now.subsec + 0.001
|
62
|
+
end
|
63
|
+
|
64
|
+
def redis_time
|
65
|
+
s, ms = @redis.time # returns [seconds since epoch, microseconds]
|
66
|
+
now = Time.at(s + ms / (10**6))
|
67
|
+
config[:tz] ? now.in_time_zone(config[:tz]) : now
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.default_logger
|
71
|
+
Logger.new(STDOUT).tap do |logger|
|
72
|
+
logger.formatter = -> (_, datetime, _, msg) { "#{datetime}: #{msg}\n" }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/test/zhong_test.rb
ADDED
data/zhong.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "zhong/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zhong"
|
8
|
+
spec.version = Zhong::VERSION
|
9
|
+
spec.authors = ["Nick Elser"]
|
10
|
+
spec.email = ["nick.elser@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Reliable, distributed cron.}
|
13
|
+
spec.description = %q{Reliable, distributed cron.}
|
14
|
+
spec.homepage = "https://www.github.com/nickelser/zhong"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.bindir = "bin"
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.required_ruby_version = "~> 2.1"
|
24
|
+
|
25
|
+
spec.add_dependency "suo"
|
26
|
+
spec.add_dependency "redis"
|
27
|
+
spec.add_dependency "tzinfo"
|
28
|
+
spec.add_dependency "activesupport"
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rubocop", "~> 0.30.0"
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.5.0"
|
34
|
+
# spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zhong
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Elser
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: suo
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: redis
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: tzinfo
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.30.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.30.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: minitest
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 5.5.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 5.5.0
|
125
|
+
description: Reliable, distributed cron.
|
126
|
+
email:
|
127
|
+
- nick.elser@gmail.com
|
128
|
+
executables:
|
129
|
+
- console
|
130
|
+
- setup
|
131
|
+
extensions: []
|
132
|
+
extra_rdoc_files: []
|
133
|
+
files:
|
134
|
+
- ".gitignore"
|
135
|
+
- ".rubocop.yml"
|
136
|
+
- ".travis.yml"
|
137
|
+
- CHANGELOG.md
|
138
|
+
- Gemfile
|
139
|
+
- LICENSE.txt
|
140
|
+
- README.md
|
141
|
+
- Rakefile
|
142
|
+
- bin/console
|
143
|
+
- bin/setup
|
144
|
+
- lib/zhong.rb
|
145
|
+
- lib/zhong/at.rb
|
146
|
+
- lib/zhong/every.rb
|
147
|
+
- lib/zhong/job.rb
|
148
|
+
- lib/zhong/scheduler.rb
|
149
|
+
- lib/zhong/version.rb
|
150
|
+
- test/minitest_helper.rb
|
151
|
+
- test/zhong_test.rb
|
152
|
+
- zhong.gemspec
|
153
|
+
homepage: https://www.github.com/nickelser/zhong
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - "~>"
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '2.1'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.4.5
|
174
|
+
signing_key:
|
175
|
+
specification_version: 4
|
176
|
+
summary: Reliable, distributed cron.
|
177
|
+
test_files:
|
178
|
+
- test/minitest_helper.rb
|
179
|
+
- test/zhong_test.rb
|