tanzeeb-rufus-scheduler 2.0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG.txt +43 -0
- data/CREDITS.txt +36 -0
- data/LICENSE.txt +21 -0
- data/README.rdoc +388 -0
- data/Rakefile +86 -0
- data/TODO.txt +57 -0
- data/lib/rufus-scheduler.rb +3 -0
- data/lib/rufus/otime.rb +3 -0
- data/lib/rufus/sc/cronline.rb +281 -0
- data/lib/rufus/sc/jobqueues.rb +160 -0
- data/lib/rufus/sc/jobs.rb +363 -0
- data/lib/rufus/sc/rtime.rb +365 -0
- data/lib/rufus/sc/scheduler.rb +481 -0
- data/lib/rufus/sc/version.rb +32 -0
- data/lib/rufus/scheduler.rb +55 -0
- data/misc/cronline_next_time_cost.rb +14 -0
- data/spec/at_in_spec.rb +48 -0
- data/spec/at_spec.rb +121 -0
- data/spec/blocking_spec.rb +54 -0
- data/spec/cron_spec.rb +122 -0
- data/spec/cronline_spec.rb +163 -0
- data/spec/every_spec.rb +229 -0
- data/spec/exception_spec.rb +77 -0
- data/spec/in_spec.rb +165 -0
- data/spec/rtime_spec.rb +93 -0
- data/spec/schedulable_spec.rb +79 -0
- data/spec/scheduler_spec.rb +81 -0
- data/spec/spec.rb +14 -0
- data/spec/spec_base.rb +82 -0
- data/spec/stress_schedule_unschedule_spec.rb +155 -0
- data/spec/timeout_spec.rb +125 -0
- data/tanzeeb-rufus-scheduler.gemspec +97 -0
- data/test/kjw.rb +113 -0
- data/test/t.rb +20 -0
- metadata +160 -0
@@ -0,0 +1,32 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006-2010, John Mettraux, jmettraux@gmail.com
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
# of this software and associated documentation files (the "Software"), to deal
|
6
|
+
# in the Software without restriction, including without limitation the rights
|
7
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
# copies of the Software, and to permit persons to whom the Software is
|
9
|
+
# furnished to do so, subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in
|
12
|
+
# all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
# THE SOFTWARE.
|
21
|
+
#
|
22
|
+
# Made in Japan.
|
23
|
+
#++
|
24
|
+
|
25
|
+
|
26
|
+
module Rufus
|
27
|
+
module Scheduler
|
28
|
+
|
29
|
+
VERSION = '2.0.7.2'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006-2010, John Mettraux, jmettraux@gmail.com
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
# of this software and associated documentation files (the "Software"), to deal
|
6
|
+
# in the Software without restriction, including without limitation the rights
|
7
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
# copies of the Software, and to permit persons to whom the Software is
|
9
|
+
# furnished to do so, subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in
|
12
|
+
# all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
# THE SOFTWARE.
|
21
|
+
#
|
22
|
+
# Made in Japan.
|
23
|
+
#++
|
24
|
+
|
25
|
+
|
26
|
+
require 'rufus/sc/scheduler'
|
27
|
+
|
28
|
+
|
29
|
+
module Rufus::Scheduler
|
30
|
+
|
31
|
+
# A quick way to get a scheduler up an running
|
32
|
+
#
|
33
|
+
# require 'rubygems'
|
34
|
+
# s = Rufus::Scheduler.start_new
|
35
|
+
#
|
36
|
+
# If EventMachine is present and running will create an EmScheduler, else
|
37
|
+
# it will create a PlainScheduler instance.
|
38
|
+
#
|
39
|
+
def self.start_new (opts={})
|
40
|
+
|
41
|
+
if defined?(EM) and EM.reactor_running?
|
42
|
+
EmScheduler.start_new(opts)
|
43
|
+
else
|
44
|
+
PlainScheduler.start_new(opts)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns true if the given string seems to be a cron string.
|
49
|
+
#
|
50
|
+
def self.is_cron_string (s)
|
51
|
+
|
52
|
+
s.match(/.+ .+ .+ .+ .+/) # well...
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
data/spec/at_in_spec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Specifying rufus-scheduler
|
4
|
+
#
|
5
|
+
# Sun Mar 22 16:47:28 JST 2009
|
6
|
+
#
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/spec_base'
|
9
|
+
|
10
|
+
|
11
|
+
describe SCHEDULER_CLASS do
|
12
|
+
|
13
|
+
before do
|
14
|
+
@s = start_scheduler
|
15
|
+
end
|
16
|
+
after do
|
17
|
+
stop_scheduler(@s)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
it 'should override jobs with the same id' do
|
22
|
+
|
23
|
+
hits = []
|
24
|
+
|
25
|
+
job0 = @s.in '1s', :job_id => 'nada' do
|
26
|
+
hits << 0
|
27
|
+
end
|
28
|
+
|
29
|
+
wait_next_tick
|
30
|
+
|
31
|
+
job1 = @s.in '1s', :job_id => 'nada' do
|
32
|
+
hits << 1
|
33
|
+
end
|
34
|
+
|
35
|
+
wait_next_tick
|
36
|
+
@s.jobs.size.should.equal(1)
|
37
|
+
|
38
|
+
hits.should.be.empty
|
39
|
+
|
40
|
+
sleep 1.5
|
41
|
+
|
42
|
+
hits.should.equal([ 1 ])
|
43
|
+
|
44
|
+
@s.jobs.size.should.equal(0)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
data/spec/at_spec.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Specifying rufus-scheduler
|
4
|
+
#
|
5
|
+
# Sat Mar 21 20:19:30 JST 2009
|
6
|
+
#
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/spec_base'
|
9
|
+
|
10
|
+
|
11
|
+
describe "#{SCHEDULER_CLASS}#schedule_at" do
|
12
|
+
|
13
|
+
before do
|
14
|
+
@s = start_scheduler
|
15
|
+
end
|
16
|
+
after do
|
17
|
+
stop_scheduler(@s)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should have job ids with the class name in it' do
|
21
|
+
|
22
|
+
j0 = @s.at(Time.now + 1) {}
|
23
|
+
j0.job_id.should.match(/Rufus::Scheduler::AtJob/)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should accept integers as 'at'" do
|
27
|
+
|
28
|
+
lambda { @s.at(1) {} }.should.not.raise
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should schedule at 'top + 1'" do
|
32
|
+
|
33
|
+
var = nil
|
34
|
+
|
35
|
+
@s.at Time.now + 1 do
|
36
|
+
var = true
|
37
|
+
end
|
38
|
+
|
39
|
+
var.should.be.nil
|
40
|
+
sleep 1.5
|
41
|
+
|
42
|
+
var.should.be.true
|
43
|
+
@s.jobs.should.be.empty
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should trigger immediately jobs in the past' do
|
47
|
+
|
48
|
+
var = nil
|
49
|
+
|
50
|
+
j = @s.at Time.now - 2 do
|
51
|
+
var = true
|
52
|
+
end
|
53
|
+
|
54
|
+
j.should.not.be.nil
|
55
|
+
|
56
|
+
#wait_next_tick
|
57
|
+
sleep 0.500
|
58
|
+
|
59
|
+
var.should.be.true
|
60
|
+
@s.jobs.should.be.empty
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should unschedule' do
|
64
|
+
|
65
|
+
job = @s.at Time.now + 3 * 3600 do
|
66
|
+
end
|
67
|
+
|
68
|
+
wait_next_tick
|
69
|
+
|
70
|
+
@s.jobs.size.should.equal(1)
|
71
|
+
|
72
|
+
@s.unschedule(job.job_id)
|
73
|
+
|
74
|
+
@s.jobs.size.should.equal(0)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should accept tags for jobs' do
|
78
|
+
|
79
|
+
job = @s.at Time.now + 3 * 3600, :tags => 'spec' do
|
80
|
+
end
|
81
|
+
|
82
|
+
wait_next_tick
|
83
|
+
|
84
|
+
@s.find_by_tag('spec').size.should.equal(1)
|
85
|
+
@s.find_by_tag('spec').first.job_id.should.equal(job.job_id)
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
describe Rufus::Scheduler::AtJob do
|
91
|
+
|
92
|
+
before do
|
93
|
+
@s = start_scheduler
|
94
|
+
end
|
95
|
+
after do
|
96
|
+
stop_scheduler(@s)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should unschedule itself' do
|
100
|
+
|
101
|
+
job = @s.at Time.now + 3 * 3600 do
|
102
|
+
end
|
103
|
+
|
104
|
+
wait_next_tick
|
105
|
+
|
106
|
+
job.unschedule
|
107
|
+
|
108
|
+
@s.jobs.size.should.equal(0)
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should respond to #next_time' do
|
112
|
+
|
113
|
+
t = Time.now + 3 * 3600
|
114
|
+
|
115
|
+
job = @s.at Time.now + 3 * 3600 do
|
116
|
+
end
|
117
|
+
|
118
|
+
job.next_time.to_i.should.equal(t.to_i)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Specifying rufus-scheduler
|
4
|
+
#
|
5
|
+
# Sat Mar 21 17:36:36 JST 2009
|
6
|
+
#
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/spec_base'
|
9
|
+
|
10
|
+
|
11
|
+
describe SCHEDULER_CLASS do
|
12
|
+
|
13
|
+
before do
|
14
|
+
@s = start_scheduler
|
15
|
+
end
|
16
|
+
after do
|
17
|
+
stop_scheduler(@s)
|
18
|
+
end
|
19
|
+
|
20
|
+
JOB = Proc.new do |x|
|
21
|
+
begin
|
22
|
+
$var << "a#{x}"
|
23
|
+
sleep 0.500
|
24
|
+
$var << "b#{x}"
|
25
|
+
rescue Exception => e
|
26
|
+
puts '=' * 80
|
27
|
+
p e
|
28
|
+
puts '=' * 80
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should not block when :blocking => nil' do
|
33
|
+
|
34
|
+
$var = []
|
35
|
+
@s.in('1s') { JOB.call(1) }
|
36
|
+
@s.in('1s') { JOB.call(2) }
|
37
|
+
|
38
|
+
sleep 5.0
|
39
|
+
|
40
|
+
[ %w{ a1 a2 b1 b2 }, %w{ a1 a2 b2 b1 } ].should.include($var)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should block when :blocking => true' do
|
44
|
+
|
45
|
+
$var = []
|
46
|
+
@s.in('1s', :blocking => true) { JOB.call(8) }
|
47
|
+
@s.in('1s', :blocking => true) { JOB.call(9) }
|
48
|
+
|
49
|
+
sleep 4.5
|
50
|
+
|
51
|
+
$var.should.equal(%w{ a8 b8 a9 b9 })
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
data/spec/cron_spec.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Specifying rufus-scheduler
|
4
|
+
#
|
5
|
+
# Sun Mar 22 19:59:12 JST 2009
|
6
|
+
#
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/spec_base'
|
9
|
+
|
10
|
+
|
11
|
+
describe "#{SCHEDULER_CLASS}#cron" do
|
12
|
+
|
13
|
+
before do
|
14
|
+
@s = start_scheduler
|
15
|
+
end
|
16
|
+
after do
|
17
|
+
stop_scheduler(@s)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should have job ids with the class name in it' do
|
21
|
+
|
22
|
+
j0 = @s.cron('7 10-12 * * * *') {}
|
23
|
+
j0.job_id.should.match(/Rufus::Scheduler::CronJob/)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should cron every second' do
|
27
|
+
|
28
|
+
seconds = []
|
29
|
+
|
30
|
+
job = @s.cron '* * * * * *' do |job|
|
31
|
+
seconds << job.last.sec
|
32
|
+
end
|
33
|
+
sleep 4.990
|
34
|
+
|
35
|
+
job.unschedule
|
36
|
+
|
37
|
+
seconds.uniq.size.should.equal(seconds.size)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should unschedule' do
|
41
|
+
|
42
|
+
second = nil
|
43
|
+
|
44
|
+
job = @s.cron '* * * * * *' do |job|
|
45
|
+
second = job.last.sec
|
46
|
+
end
|
47
|
+
|
48
|
+
second.should.be.nil
|
49
|
+
|
50
|
+
sleep 2
|
51
|
+
|
52
|
+
second.should.not.be.nil
|
53
|
+
|
54
|
+
job.unschedule
|
55
|
+
|
56
|
+
after = second
|
57
|
+
|
58
|
+
sleep 2
|
59
|
+
|
60
|
+
second.should.equal(after)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should keep track of cron jobs' do
|
64
|
+
|
65
|
+
j0 = @s.cron '7 10-12 * * * *' do
|
66
|
+
end
|
67
|
+
j1 = @s.cron '7 10-12 * * * *' do
|
68
|
+
end
|
69
|
+
|
70
|
+
@s.cron_jobs.keys.sort.should.equal([ j0.job_id, j1.job_id ].sort)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should accept tags for jobs' do
|
74
|
+
|
75
|
+
job = @s.cron '* * * * * *', :tags => 'spec' do
|
76
|
+
end
|
77
|
+
|
78
|
+
wait_next_tick
|
79
|
+
|
80
|
+
@s.find_by_tag('spec').size.should.equal(1)
|
81
|
+
@s.find_by_tag('spec').first.job_id.should.equal(job.job_id)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should accept job.unschedule within the job' do
|
85
|
+
|
86
|
+
stack = []
|
87
|
+
|
88
|
+
@s.cron '* * * * * *' do |job|
|
89
|
+
if stack.size > 2
|
90
|
+
stack << 'done'
|
91
|
+
job.unschedule
|
92
|
+
else
|
93
|
+
stack << 'ok'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
sleep 4
|
98
|
+
|
99
|
+
@s.jobs.size.should.equal(0)
|
100
|
+
stack.should.equal(%w[ ok ok ok done ])
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
describe Rufus::Scheduler::CronJob do
|
106
|
+
|
107
|
+
before do
|
108
|
+
@s = start_scheduler
|
109
|
+
end
|
110
|
+
after do
|
111
|
+
stop_scheduler(@s)
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should respond to #next_time' do
|
115
|
+
|
116
|
+
job = @s.cron '* * * * *' do
|
117
|
+
end
|
118
|
+
|
119
|
+
(job.next_time.to_i - Time.now.to_i).should.satisfy { |v| v < 60 }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|