clockwork 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/clockwork.rb +29 -9
  3. data/test/clockwork_test.rb +22 -0
  4. metadata +3 -5
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.3.0
data/lib/clockwork.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module Clockwork
2
2
  class FailedToParse < StandardError; end;
3
3
 
4
+ @@events = []
5
+
4
6
  class Event
5
7
  attr_accessor :job, :last
6
8
 
@@ -18,7 +20,7 @@ module Clockwork
18
20
 
19
21
  def time?(t)
20
22
  ellapsed_ready = (@last.nil? or (t - @last).to_i >= @period)
21
- time_ready = (@at.nil? or (t.hour == @at[0] and t.min == @at[1]))
23
+ time_ready = (@at.nil? or ((@at[0].nil? or t.hour == @at[0]) and t.min == @at[1]))
22
24
  ellapsed_ready and time_ready
23
25
  end
24
26
 
@@ -46,10 +48,16 @@ module Clockwork
46
48
 
47
49
  def parse_at(at)
48
50
  return unless at
49
- m = at.match(/^(\d\d):(\d\d)$/)
51
+ m = at.match(/^(\d\d|\*+):(\d\d)$/)
50
52
  raise FailedToParse, at unless m
51
- hour, min = m[1].to_i, m[2].to_i
52
- raise FailedToParse, at if hour >= 24 or min >= 60
53
+ if m[1][0] == '*'
54
+ hour = nil
55
+ else
56
+ hour = m[1].to_i
57
+ raise FailedToParse, at if hour >= 24
58
+ end
59
+ min = m[2].to_i
60
+ raise FailedToParse, at if min >= 60
53
61
  [ hour, min ]
54
62
  end
55
63
  end
@@ -68,10 +76,15 @@ module Clockwork
68
76
  end
69
77
 
70
78
  def every(period, job, options={}, &block)
71
- event = Event.new(period, job, block || get_handler, options)
72
- @@events ||= []
73
- @@events << event
74
- event
79
+ if options[:at].respond_to?(:each)
80
+ each_options = options.clone
81
+ options[:at].each do |at|
82
+ each_options[:at] = at
83
+ register(period, job, block, each_options)
84
+ end
85
+ else
86
+ register(period, job, block, options)
87
+ end
75
88
  end
76
89
 
77
90
  def run
@@ -103,6 +116,13 @@ module Clockwork
103
116
  @@events = []
104
117
  @@handler = nil
105
118
  end
119
+
120
+ private
121
+ def register(period, job, block, options)
122
+ event = Event.new(period, job, block || get_handler, options)
123
+ @@events << event
124
+ event
125
+ end
106
126
 
107
127
  end
108
128
 
@@ -120,4 +140,4 @@ unless 1.respond_to?(:seconds)
120
140
  def days; self * 86400; end
121
141
  alias :day :days
122
142
  end
123
- end
143
+ end
@@ -56,6 +56,28 @@ class ClockworkTest < Test::Unit::TestCase
56
56
  assert_will_run Time.parse('jan 2 2010 16:20:00')
57
57
  end
58
58
 
59
+ test "twice a day at 16:20 and 18:10" do
60
+ Clockwork.every(1.day, 'myjob', :at => ['16:20', '18:10'])
61
+
62
+ assert_wont_run Time.parse('jan 1 2010 16:19:59')
63
+ assert_will_run Time.parse('jan 1 2010 16:20:00')
64
+ assert_wont_run Time.parse('jan 1 2010 16:20:01')
65
+
66
+ assert_wont_run Time.parse('jan 1 2010 18:09:59')
67
+ assert_will_run Time.parse('jan 1 2010 18:10:00')
68
+ assert_wont_run Time.parse('jan 1 2010 18:10:01')
69
+ end
70
+
71
+ test "once an hour at **:20" do
72
+ Clockwork.every(1.hour, 'myjob', :at => '**:20')
73
+
74
+ assert_wont_run Time.parse('jan 1 2010 15:19:59')
75
+ assert_will_run Time.parse('jan 1 2010 15:20:00')
76
+ assert_wont_run Time.parse('jan 1 2010 15:20:01')
77
+ assert_wont_run Time.parse('jan 2 2010 16:19:59')
78
+ assert_will_run Time.parse('jan 2 2010 16:20:00')
79
+ end
80
+
59
81
  test "aborts when no handler defined" do
60
82
  Clockwork.clear!
61
83
  assert_raise(Clockwork::NoHandlerDefined) do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: clockwork
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.4
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Adam Wiggins
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-31 00:00:00 -07:00
14
- default_executable: clockwork
13
+ date: 2011-10-14 00:00:00 Z
15
14
  dependencies: []
16
15
 
17
16
  description: A scheduler process to replace cron, using a more flexible Ruby syntax running as a single long-running process. Inspired by rufus-scheduler and resque-scheduler.
@@ -29,7 +28,6 @@ files:
29
28
  - bin/clockwork
30
29
  - lib/clockwork.rb
31
30
  - test/clockwork_test.rb
32
- has_rdoc: true
33
31
  homepage: http://github.com/adamwiggins/clockwork
34
32
  licenses: []
35
33
 
@@ -53,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
51
  requirements: []
54
52
 
55
53
  rubyforge_project: clockwork
56
- rubygems_version: 1.5.0
54
+ rubygems_version: 1.8.6
57
55
  signing_key:
58
56
  specification_version: 3
59
57
  summary: A scheduler process to replace cron.