clockwork 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +2 -1
- data/clockwork.gemspec +1 -1
- data/lib/clockwork.rb +5 -0
- data/lib/clockwork/at.rb +4 -2
- data/lib/clockwork/manager.rb +31 -11
- data/test/at_test.rb +23 -3
- data/test/manager_test.rb +70 -0
- metadata +17 -39
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2e720594452bfaa38dfa5531dd7b8de45423f4b3
|
4
|
+
data.tar.gz: f0258265ef190bccce9fb5360e733a03c38ce4ba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 138b331deb24d582e1da4126a8f661bec5e101b2a251068b5cd12f3da422a672bf0e6d00c3d1c6bed4d0955e536138063bb7d707e7a7cc3ac6d8c1ae84f3f2ac
|
7
|
+
data.tar.gz: deb399044bf8266c9f9d0d70f9aec93f01f7c092b6f931cbf73bcc13a8569f393a07243803cd1bccffe981ccc7c210058448b5249cbdd9ee15d263006f407a42
|
data/README.md
CHANGED
@@ -139,10 +139,11 @@ You can omit 0 of the hour:
|
|
139
139
|
every(1.day, 'reminders.send', :at => '1:30')
|
140
140
|
```
|
141
141
|
|
142
|
-
The wildcard for hour is supported:
|
142
|
+
The wildcard for hour and minute is supported:
|
143
143
|
|
144
144
|
```ruby
|
145
145
|
every(1.hour, 'reminders.send', :at => '**:30')
|
146
|
+
every(10.seconds, 'frequent.job', :at => '9:**')
|
146
147
|
```
|
147
148
|
|
148
149
|
You can set more than one timing:
|
data/clockwork.gemspec
CHANGED
data/lib/clockwork.rb
CHANGED
@@ -18,6 +18,10 @@ module Clockwork
|
|
18
18
|
@@manager.handler(&block)
|
19
19
|
end
|
20
20
|
|
21
|
+
def on(event, options={}, &block)
|
22
|
+
@@manager.on(event, options, &block)
|
23
|
+
end
|
24
|
+
|
21
25
|
def every(period, job, options={}, &block)
|
22
26
|
@@manager.every(period, job, options, &block)
|
23
27
|
end
|
@@ -29,6 +33,7 @@ module Clockwork
|
|
29
33
|
def clear!
|
30
34
|
@@manager = Manager.new
|
31
35
|
end
|
36
|
+
|
32
37
|
end
|
33
38
|
|
34
39
|
unless 1.respond_to?(:seconds)
|
data/lib/clockwork/at.rb
CHANGED
@@ -20,6 +20,8 @@ module Clockwork
|
|
20
20
|
new($2.to_i, $1.to_i)
|
21
21
|
when /^\*{1,2}:(\d\d)$/
|
22
22
|
new($1.to_i)
|
23
|
+
when /^(\d{1,2}):\*\*$/
|
24
|
+
new(NOT_SPECIFIED, $1.to_i)
|
23
25
|
else
|
24
26
|
raise FailedToParse, at
|
25
27
|
end
|
@@ -30,7 +32,7 @@ module Clockwork
|
|
30
32
|
attr_writer :min, :hour, :wday
|
31
33
|
|
32
34
|
def initialize(min, hour=NOT_SPECIFIED, wday=NOT_SPECIFIED)
|
33
|
-
if min
|
35
|
+
if (min != NOT_SPECIFIED && (min < 0 || min > 59)) ||
|
34
36
|
(hour != NOT_SPECIFIED && (hour < 0 || hour > 23)) ||
|
35
37
|
(wday != NOT_SPECIFIED && (wday < 0 || wday > 6))
|
36
38
|
raise ArgumentError
|
@@ -41,7 +43,7 @@ module Clockwork
|
|
41
43
|
end
|
42
44
|
|
43
45
|
def ready?(t)
|
44
|
-
t.min == @min and
|
46
|
+
(@min == NOT_SPECIFIED or t.min == @min) and
|
45
47
|
(@hour == NOT_SPECIFIED or t.hour == @hour) and
|
46
48
|
(@wday == NOT_SPECIFIED or t.wday == @wday)
|
47
49
|
end
|
data/lib/clockwork/manager.rb
CHANGED
@@ -6,6 +6,7 @@ module Clockwork
|
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
@events = []
|
9
|
+
@callbacks = {}
|
9
10
|
@config = default_configuration
|
10
11
|
@handler = nil
|
11
12
|
end
|
@@ -31,18 +32,23 @@ module Clockwork
|
|
31
32
|
@handler
|
32
33
|
end
|
33
34
|
|
35
|
+
def on(event, options={}, &block)
|
36
|
+
raise "Unsupported callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
|
37
|
+
(@callbacks[event.to_sym]||=[]) << block
|
38
|
+
end
|
39
|
+
|
34
40
|
def every(period, job, options={}, &block)
|
35
41
|
if options[:at].respond_to?(:each)
|
36
|
-
|
37
|
-
options[:at].each do |at|
|
38
|
-
each_options[:at] = at
|
39
|
-
register(period, job, block, each_options)
|
40
|
-
end
|
42
|
+
every_with_multiple_times(period, job, options, &block)
|
41
43
|
else
|
42
44
|
register(period, job, block, options)
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
48
|
+
def fire_callbacks(event, *args)
|
49
|
+
@callbacks[event].nil? || @callbacks[event].all? { |h| h.call(*args) }
|
50
|
+
end
|
51
|
+
|
46
52
|
def run
|
47
53
|
log "Starting clock for #{@events.size} events: [ " + @events.map { |e| e.to_s }.join(' ') + " ]"
|
48
54
|
loop do
|
@@ -52,15 +58,21 @@ module Clockwork
|
|
52
58
|
end
|
53
59
|
|
54
60
|
def tick(t=Time.now)
|
55
|
-
|
56
|
-
event
|
57
|
-
|
61
|
+
if (fire_callbacks(:before_tick))
|
62
|
+
to_run = @events.select do |event|
|
63
|
+
event.time?(t)
|
64
|
+
end
|
58
65
|
|
59
|
-
|
60
|
-
|
61
|
-
|
66
|
+
to_run.each do |event|
|
67
|
+
if (fire_callbacks(:before_run, event, t))
|
68
|
+
log "Triggering '#{event}'"
|
69
|
+
event.run(t)
|
70
|
+
fire_callbacks(:after_run, event, t)
|
71
|
+
end
|
72
|
+
end
|
62
73
|
end
|
63
74
|
|
75
|
+
fire_callbacks(:after_tick)
|
64
76
|
to_run
|
65
77
|
end
|
66
78
|
|
@@ -87,5 +99,13 @@ module Clockwork
|
|
87
99
|
|
88
100
|
options
|
89
101
|
end
|
102
|
+
|
103
|
+
def every_with_multiple_times(period, job, options={}, &block)
|
104
|
+
each_options = options.clone
|
105
|
+
options[:at].each do |at|
|
106
|
+
each_options[:at] = at
|
107
|
+
register(period, job, block, each_options)
|
108
|
+
end
|
109
|
+
end
|
90
110
|
end
|
91
111
|
end
|
data/test/at_test.rb
CHANGED
@@ -24,7 +24,7 @@ class AtTest < Test::Unit::TestCase
|
|
24
24
|
assert !at.ready?(time_in_day(8, 21))
|
25
25
|
end
|
26
26
|
|
27
|
-
test '**:20' do
|
27
|
+
test '**:20 with two stars' do
|
28
28
|
at = Clockwork::At.parse('**:20')
|
29
29
|
|
30
30
|
assert !at.ready?(time_in_day(15, 19))
|
@@ -36,7 +36,7 @@ class AtTest < Test::Unit::TestCase
|
|
36
36
|
assert !at.ready?(time_in_day(16, 21))
|
37
37
|
end
|
38
38
|
|
39
|
-
test '
|
39
|
+
test '*:20 with one star' do
|
40
40
|
at = Clockwork::At.parse('*:20')
|
41
41
|
|
42
42
|
assert !at.ready?(time_in_day(15, 19))
|
@@ -48,6 +48,26 @@ class AtTest < Test::Unit::TestCase
|
|
48
48
|
assert !at.ready?(time_in_day(16, 21))
|
49
49
|
end
|
50
50
|
|
51
|
+
test '16:**' do
|
52
|
+
at = Clockwork::At.parse('16:**')
|
53
|
+
|
54
|
+
assert !at.ready?(time_in_day(15, 59))
|
55
|
+
assert at.ready?(time_in_day(16, 00))
|
56
|
+
assert at.ready?(time_in_day(16, 30))
|
57
|
+
assert at.ready?(time_in_day(16, 59))
|
58
|
+
assert !at.ready?(time_in_day(17, 00))
|
59
|
+
end
|
60
|
+
|
61
|
+
test '8:**' do
|
62
|
+
at = Clockwork::At.parse('8:**')
|
63
|
+
|
64
|
+
assert !at.ready?(time_in_day(7, 59))
|
65
|
+
assert at.ready?(time_in_day(8, 00))
|
66
|
+
assert at.ready?(time_in_day(8, 30))
|
67
|
+
assert at.ready?(time_in_day(8, 59))
|
68
|
+
assert !at.ready?(time_in_day(9, 00))
|
69
|
+
end
|
70
|
+
|
51
71
|
test 'Saturday 12:00' do
|
52
72
|
at = Clockwork::At.parse('Saturday 12:00')
|
53
73
|
|
@@ -57,7 +77,7 @@ class AtTest < Test::Unit::TestCase
|
|
57
77
|
assert at.ready?(Time.new(2010, 1, 9, 12, 00))
|
58
78
|
end
|
59
79
|
|
60
|
-
test '
|
80
|
+
test 'sat 12:00' do
|
61
81
|
at = Clockwork::At.parse('sat 12:00')
|
62
82
|
|
63
83
|
assert !at.ready?(Time.new(2010, 1, 1, 12, 00))
|
data/test/manager_test.rb
CHANGED
@@ -269,4 +269,74 @@ class ManagerTest < Test::Unit::TestCase
|
|
269
269
|
end
|
270
270
|
end
|
271
271
|
|
272
|
+
describe "callbacks" do
|
273
|
+
test "should not accept unknown callback name" do
|
274
|
+
assert_raise(RuntimeError, "Unsupported callback unknown_callback") do
|
275
|
+
@manager.on(:unknown_callback) do
|
276
|
+
true
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
test "should run before_tick callback once on tick" do
|
282
|
+
counter = 0
|
283
|
+
@manager.on(:before_tick) do
|
284
|
+
counter += 1
|
285
|
+
end
|
286
|
+
@manager.tick
|
287
|
+
assert_equal 1, counter
|
288
|
+
end
|
289
|
+
|
290
|
+
test "should not run events if before_tick returns false" do
|
291
|
+
@manager.on(:before_tick) do
|
292
|
+
false
|
293
|
+
end
|
294
|
+
@manager.every(1.second, 'myjob') { raise "should not run" }
|
295
|
+
@manager.tick
|
296
|
+
end
|
297
|
+
|
298
|
+
test "should run before_run twice if two events are registered" do
|
299
|
+
counter = 0
|
300
|
+
@manager.on(:before_run) do
|
301
|
+
counter += 1
|
302
|
+
end
|
303
|
+
@manager.every(1.second, 'myjob')
|
304
|
+
@manager.every(1.second, 'myjob2')
|
305
|
+
@manager.tick
|
306
|
+
assert_equal 2, counter
|
307
|
+
end
|
308
|
+
|
309
|
+
test "should run even jobs only" do
|
310
|
+
counter = 0
|
311
|
+
ran = false
|
312
|
+
@manager.on(:before_run) do
|
313
|
+
counter += 1
|
314
|
+
counter % 2 == 0
|
315
|
+
end
|
316
|
+
@manager.every(1.second, 'myjob') { raise "should not ran" }
|
317
|
+
@manager.every(1.second, 'myjob2') { ran = true }
|
318
|
+
@manager.tick
|
319
|
+
assert ran
|
320
|
+
end
|
321
|
+
|
322
|
+
test "should run after_run callback for each event" do
|
323
|
+
counter = 0
|
324
|
+
@manager.on(:after_run) do
|
325
|
+
counter += 1
|
326
|
+
end
|
327
|
+
@manager.every(1.second, 'myjob')
|
328
|
+
@manager.every(1.second, 'myjob2')
|
329
|
+
@manager.tick
|
330
|
+
assert_equal 2, counter
|
331
|
+
end
|
332
|
+
|
333
|
+
test "should run after_tick callback once" do
|
334
|
+
counter = 0
|
335
|
+
@manager.on(:after_tick) do
|
336
|
+
counter += 1
|
337
|
+
end
|
338
|
+
@manager.tick
|
339
|
+
assert_equal 1, counter
|
340
|
+
end
|
341
|
+
end
|
272
342
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clockwork
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
5
|
-
prerelease:
|
4
|
+
version: 0.6.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Adam Wiggins
|
@@ -10,12 +9,11 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date: 2013-
|
12
|
+
date: 2013-09-30 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: tzinfo
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
17
|
requirements:
|
20
18
|
- - ~>
|
21
19
|
- !ruby/object:Gem::Version
|
@@ -23,7 +21,6 @@ dependencies:
|
|
23
21
|
type: :runtime
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
24
|
requirements:
|
28
25
|
- - ~>
|
29
26
|
- !ruby/object:Gem::Version
|
@@ -31,23 +28,20 @@ dependencies:
|
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
29
|
name: activesupport
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
31
|
requirements:
|
36
|
-
- -
|
32
|
+
- - '>='
|
37
33
|
- !ruby/object:Gem::Version
|
38
34
|
version: '0'
|
39
35
|
type: :runtime
|
40
36
|
prerelease: false
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
38
|
requirements:
|
44
|
-
- -
|
39
|
+
- - '>='
|
45
40
|
- !ruby/object:Gem::Version
|
46
41
|
version: '0'
|
47
42
|
- !ruby/object:Gem::Dependency
|
48
43
|
name: bundler
|
49
44
|
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
45
|
requirements:
|
52
46
|
- - ~>
|
53
47
|
- !ruby/object:Gem::Version
|
@@ -55,7 +49,6 @@ dependencies:
|
|
55
49
|
type: :development
|
56
50
|
prerelease: false
|
57
51
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
52
|
requirements:
|
60
53
|
- - ~>
|
61
54
|
- !ruby/object:Gem::Version
|
@@ -63,65 +56,57 @@ dependencies:
|
|
63
56
|
- !ruby/object:Gem::Dependency
|
64
57
|
name: rake
|
65
58
|
requirement: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
59
|
requirements:
|
68
|
-
- -
|
60
|
+
- - '>='
|
69
61
|
- !ruby/object:Gem::Version
|
70
62
|
version: '0'
|
71
63
|
type: :development
|
72
64
|
prerelease: false
|
73
65
|
version_requirements: !ruby/object:Gem::Requirement
|
74
|
-
none: false
|
75
66
|
requirements:
|
76
|
-
- -
|
67
|
+
- - '>='
|
77
68
|
- !ruby/object:Gem::Version
|
78
69
|
version: '0'
|
79
70
|
- !ruby/object:Gem::Dependency
|
80
71
|
name: daemons
|
81
72
|
requirement: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
73
|
requirements:
|
84
|
-
- -
|
74
|
+
- - '>='
|
85
75
|
- !ruby/object:Gem::Version
|
86
76
|
version: '0'
|
87
77
|
type: :development
|
88
78
|
prerelease: false
|
89
79
|
version_requirements: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
80
|
requirements:
|
92
|
-
- -
|
81
|
+
- - '>='
|
93
82
|
- !ruby/object:Gem::Version
|
94
83
|
version: '0'
|
95
84
|
- !ruby/object:Gem::Dependency
|
96
85
|
name: contest
|
97
86
|
requirement: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
87
|
requirements:
|
100
|
-
- -
|
88
|
+
- - '>='
|
101
89
|
- !ruby/object:Gem::Version
|
102
90
|
version: '0'
|
103
91
|
type: :development
|
104
92
|
prerelease: false
|
105
93
|
version_requirements: !ruby/object:Gem::Requirement
|
106
|
-
none: false
|
107
94
|
requirements:
|
108
|
-
- -
|
95
|
+
- - '>='
|
109
96
|
- !ruby/object:Gem::Version
|
110
97
|
version: '0'
|
111
98
|
- !ruby/object:Gem::Dependency
|
112
99
|
name: mocha
|
113
100
|
requirement: !ruby/object:Gem::Requirement
|
114
|
-
none: false
|
115
101
|
requirements:
|
116
|
-
- -
|
102
|
+
- - '>='
|
117
103
|
- !ruby/object:Gem::Version
|
118
104
|
version: '0'
|
119
105
|
type: :development
|
120
106
|
prerelease: false
|
121
107
|
version_requirements: !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
108
|
requirements:
|
124
|
-
- -
|
109
|
+
- - '>='
|
125
110
|
- !ruby/object:Gem::Version
|
126
111
|
version: '0'
|
127
112
|
description: A scheduler process to replace cron, using a more flexible Ruby syntax
|
@@ -157,33 +142,26 @@ files:
|
|
157
142
|
homepage: http://github.com/tomykaira/clockwork
|
158
143
|
licenses:
|
159
144
|
- MIT
|
145
|
+
metadata: {}
|
160
146
|
post_install_message:
|
161
147
|
rdoc_options: []
|
162
148
|
require_paths:
|
163
149
|
- lib
|
164
150
|
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
-
none: false
|
166
151
|
requirements:
|
167
|
-
- -
|
152
|
+
- - '>='
|
168
153
|
- !ruby/object:Gem::Version
|
169
154
|
version: '0'
|
170
|
-
segments:
|
171
|
-
- 0
|
172
|
-
hash: 1040886093
|
173
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
|
-
none: false
|
175
156
|
requirements:
|
176
|
-
- -
|
157
|
+
- - '>='
|
177
158
|
- !ruby/object:Gem::Version
|
178
159
|
version: '0'
|
179
|
-
segments:
|
180
|
-
- 0
|
181
|
-
hash: 1040886093
|
182
160
|
requirements: []
|
183
161
|
rubyforge_project:
|
184
|
-
rubygems_version:
|
162
|
+
rubygems_version: 2.0.3
|
185
163
|
signing_key:
|
186
|
-
specification_version:
|
164
|
+
specification_version: 4
|
187
165
|
summary: A scheduler process to replace cron.
|
188
166
|
test_files:
|
189
167
|
- test/at_test.rb
|