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
data/.gitignore
ADDED
data/CHANGELOG.txt
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
= rufus-scheduler CHANGELOG.txt
|
3
|
+
|
4
|
+
|
5
|
+
== rufus-scheduler - 2.0.7 not yet released
|
6
|
+
|
7
|
+
- Scheduler#trigger_threads, thanks Tim Uckun
|
8
|
+
|
9
|
+
|
10
|
+
== rufus-scheduler - 2.0.6 released 2010/05/01
|
11
|
+
|
12
|
+
- timeout jobs not outliving their parent job anymore, thanks Joel Wood
|
13
|
+
|
14
|
+
|
15
|
+
== rufus-scheduler - 2.0.5 released 2010/03/02
|
16
|
+
|
17
|
+
- fixed parse_time_string(s) issue, thanks Gonzalo Suarez
|
18
|
+
|
19
|
+
|
20
|
+
== rufus-scheduler - 2.0.4 released 2010/02/12
|
21
|
+
|
22
|
+
- addressing issue with every and timeout, thanks Tony Day
|
23
|
+
|
24
|
+
|
25
|
+
== rufus-scheduler - 2.0.3 released 2009/11/04
|
26
|
+
|
27
|
+
- made sure Schedulables with a call(job) method were OK when passed as second
|
28
|
+
parameter (thanks Nate Wiger)
|
29
|
+
|
30
|
+
|
31
|
+
== rufus-scheduler - 2.0.2 released 2009/10/31
|
32
|
+
|
33
|
+
- unified JobQueue and CronJobQueue, and handed @last_second management to the
|
34
|
+
latter
|
35
|
+
- #trigger_block method for easier override
|
36
|
+
- passing :job => job among Schedulable trigger parameters
|
37
|
+
|
38
|
+
|
39
|
+
== rufus-scheduler - 2.0.1 released 2009/05/07
|
40
|
+
== rufus-scheduler - 2.0.0 released 2009/05/07
|
41
|
+
|
42
|
+
- initial release
|
43
|
+
|
data/CREDITS.txt
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
= CREDITS.txt
|
3
|
+
|
4
|
+
|
5
|
+
== Contributors
|
6
|
+
|
7
|
+
- Adam Davies (http://github.com/adz), @allow_overlap = false
|
8
|
+
- Klaas Jan Wierenga, at/every/in stress tests (1.0 and 2.0)
|
9
|
+
- TobyH (http://github.com/tobyh), faster and cleaner CronLine#next_time
|
10
|
+
|
11
|
+
|
12
|
+
== Feedback
|
13
|
+
|
14
|
+
- Gonzalo Suarez - parse_time_string(s) issue
|
15
|
+
- Tony Day - http://github.com/tonyday - every and overlapping timeout issue
|
16
|
+
- Nate Wiger (Schedulable call/trigger issue)
|
17
|
+
- Aldric (readme errors)
|
18
|
+
- Kenneth Kalmer (daemon-kit)
|
19
|
+
- Chris Evans, :timeout tests on JRuby
|
20
|
+
- Tim Uckun, :timeout concept
|
21
|
+
- K Liu, for the note about CronLine#next_time
|
22
|
+
- Xianhang Zhang, find_jobs(tag=nil) patch
|
23
|
+
- Yi Wen, for lib/rufus-scheduler.rb
|
24
|
+
- Adam Green and Rael Dornfest for the Taskr breaking issue feedback
|
25
|
+
- Sean Liu, unschedule_every issue
|
26
|
+
- Manfred Usselman (no cronjobs on sundays bug)
|
27
|
+
- Michael Goth, tests with precision > 1s
|
28
|
+
|
29
|
+
* many people gave feedback previously, see
|
30
|
+
http://openwferu.rubyforge.org/svn/trunk/openwfe-ruby/CREDITS.txt
|
31
|
+
|
32
|
+
|
33
|
+
== and finally
|
34
|
+
|
35
|
+
- many thanks to the EventMachine team (especially Aman Gupta)
|
36
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
Copyright (c) 2005-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
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,388 @@
|
|
1
|
+
|
2
|
+
= rufus-scheduler
|
3
|
+
|
4
|
+
rufus-scheduler is a Ruby gem for scheduling pieces of code (jobs). It understands running a job AT a certain time, IN a certain time, EVERY x time or simply via a CRON statement.
|
5
|
+
|
6
|
+
rufus-scheduler is no replacement for cron/at since it runs inside of Ruby.
|
7
|
+
|
8
|
+
|
9
|
+
== alternatives / complements
|
10
|
+
|
11
|
+
A list of related Ruby projects :
|
12
|
+
|
13
|
+
http://github.com/javan/whenever
|
14
|
+
http://github.com/yakischloba/em-timers/
|
15
|
+
|
16
|
+
More like complements :
|
17
|
+
|
18
|
+
http://github.com/mojombo/chronic/
|
19
|
+
http://github.com/hpoydar/chronic_duration
|
20
|
+
|
21
|
+
|
22
|
+
== installation
|
23
|
+
|
24
|
+
sudo gem install rufus-scheduler --source http://gemcutter.org
|
25
|
+
|
26
|
+
|
27
|
+
== usage
|
28
|
+
|
29
|
+
The usage is similar to the one of the old rufus-scheduler. There are a few differences though.
|
30
|
+
|
31
|
+
require 'rubygems'
|
32
|
+
require 'rufus/scheduler'
|
33
|
+
|
34
|
+
scheduler = Rufus::Scheduler.start_new
|
35
|
+
|
36
|
+
scheduler.in '20m' do
|
37
|
+
puts "order ristretto"
|
38
|
+
end
|
39
|
+
|
40
|
+
scheduler.at 'Thu Mar 26 07:31:43 +0900 2009' do
|
41
|
+
puts 'order pizza'
|
42
|
+
end
|
43
|
+
|
44
|
+
scheduler.cron '0 22 * * 1-5' do
|
45
|
+
# every day of the week at 22:00 (10pm)
|
46
|
+
puts 'activate security system'
|
47
|
+
end
|
48
|
+
|
49
|
+
scheduler.every '5m' do
|
50
|
+
puts 'check blood pressure'
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
This code summons a plain version of the scheduler, this can be made more explicit via :
|
55
|
+
|
56
|
+
scheduler = Rufus::Scheduler::PlainScheduler.start_new
|
57
|
+
|
58
|
+
This PlainScheduler accepts a :thread_name option :
|
59
|
+
|
60
|
+
scheduler = Rufus::Scheduler::PlainScheduler.start_new(:thread_name => 'my scheduler')
|
61
|
+
|
62
|
+
might be helpful when tracking threads.
|
63
|
+
|
64
|
+
|
65
|
+
Note that is there is EventMachine present and running,
|
66
|
+
|
67
|
+
scheduler = Rufus::Scheduler.start_new
|
68
|
+
|
69
|
+
will return an instance of Rufus::Scheduler::EmScheduler (leveraging EventMachine).
|
70
|
+
|
71
|
+
|
72
|
+
== scheduler.join
|
73
|
+
|
74
|
+
Note that if you have a tiny script like this one :
|
75
|
+
|
76
|
+
require 'rubygems'; require 'rufus-scheduler'
|
77
|
+
|
78
|
+
scheduler = Rufus::Scheduler.start_new
|
79
|
+
|
80
|
+
scheduler.at 'Thu Mar 26 07:31:43 +0900 2009' do
|
81
|
+
puts 'order pizza'
|
82
|
+
end
|
83
|
+
|
84
|
+
And you run it, it will exit immediately.
|
85
|
+
|
86
|
+
If you place
|
87
|
+
|
88
|
+
scheduler.join
|
89
|
+
|
90
|
+
at then end of it will make the current (main) thread join the scheduler and prevent the Ruby runtime from exiting.
|
91
|
+
|
92
|
+
You shouldn't be exposed to this issue when using EventMachine, since while running EM, your runtime won't exit.
|
93
|
+
|
94
|
+
|
95
|
+
== block parameters
|
96
|
+
|
97
|
+
Scheduled blocks accept 0 or 1 parameter (this unique parameter is the job
|
98
|
+
instance itself).
|
99
|
+
|
100
|
+
scheduler.every '5m' do
|
101
|
+
puts 'check blood pressure'
|
102
|
+
end
|
103
|
+
scheduler.every '1y' do |job|
|
104
|
+
puts "check cholesterol levels (#{job.job_id})"
|
105
|
+
end
|
106
|
+
|
107
|
+
See the class Job for more details :
|
108
|
+
|
109
|
+
http://rufus.rubyforge.org/rufus-scheduler/classes/Rufus/Scheduler/Job.html
|
110
|
+
|
111
|
+
|
112
|
+
== the time strings understood by rufus-scheduler
|
113
|
+
|
114
|
+
require 'rubygems'
|
115
|
+
require 'rufus/scheduler'
|
116
|
+
|
117
|
+
p Rufus.parse_time_string '500' # => 0.5
|
118
|
+
p Rufus.parse_time_string '1000' # => 1.0
|
119
|
+
p Rufus.parse_time_string '1h' # => 3600.0
|
120
|
+
p Rufus.parse_time_string '1h10s' # => 3610.0
|
121
|
+
p Rufus.parse_time_string '1w2d' # => 777600.0
|
122
|
+
|
123
|
+
p Rufus.to_time_string 60 # => "1m"
|
124
|
+
p Rufus.to_time_string 3661 # => "1h1m1s"
|
125
|
+
p Rufus.to_time_string 7 * 24 * 3600 # => "1w"
|
126
|
+
|
127
|
+
|
128
|
+
== :blocking
|
129
|
+
|
130
|
+
Jobs will, by default, trigger in their own thread. This is usually desirable since one expects the scheduler to continue scheduling even if a job is currently running.
|
131
|
+
|
132
|
+
Jobs scheduled with the :blocking parameter will run in the thread of the scheduler, blocking it.
|
133
|
+
|
134
|
+
scheduler.in '20m', :blocking => true do
|
135
|
+
puts "order ristretto"
|
136
|
+
sleep 2 * 60
|
137
|
+
end
|
138
|
+
scheduler.in '21m' do
|
139
|
+
puts "order espresso"
|
140
|
+
end
|
141
|
+
|
142
|
+
Hence, our espresso wil come in 22 minutes instead of 21.
|
143
|
+
|
144
|
+
|
145
|
+
== 'every' jobs and :first_at / :first_in
|
146
|
+
|
147
|
+
This job will execute every 3 days, but first time will be in 5 days from now :
|
148
|
+
|
149
|
+
scheduler.every '3d', :first_in => '5d' do
|
150
|
+
# do something
|
151
|
+
end
|
152
|
+
|
153
|
+
This job will execute every 3 days, starting from Christmas Eve at noon :
|
154
|
+
|
155
|
+
scheduler.every '3d', :first_at => '2009/12/24 12:00' do
|
156
|
+
# do something
|
157
|
+
end
|
158
|
+
|
159
|
+
The chronic gem may help (http://chronic.rubyforge.org/) :
|
160
|
+
|
161
|
+
require 'chronic' # sudo gem install chronic
|
162
|
+
|
163
|
+
scheduler.every '3h', :first_at => Chronic.parse('this tuesday 5:00') do
|
164
|
+
# do something starting this tueday
|
165
|
+
end
|
166
|
+
|
167
|
+
|
168
|
+
== self unschedule for 'cron' and 'every' jobs
|
169
|
+
|
170
|
+
'at' and 'in' jobs fire once only. 'cron' and 'every' jobs do fire repeatedly, so it might be useful to stop them.
|
171
|
+
|
172
|
+
scheduler.every '3d' do |job|
|
173
|
+
l = determine_crop_maturity_level()
|
174
|
+
if l >= 7
|
175
|
+
puts "crop is ready."
|
176
|
+
job.unschedule
|
177
|
+
else
|
178
|
+
puts "crop not yet ready..."
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
In this example, the 'every' job will unschedule itself when the crop is ready.
|
183
|
+
|
184
|
+
|
185
|
+
== schedulables
|
186
|
+
|
187
|
+
Sometimes passing a block isn't that convenient :
|
188
|
+
|
189
|
+
class JobThing
|
190
|
+
def initialize (relevant_info)
|
191
|
+
@ri = relevant_info
|
192
|
+
end
|
193
|
+
def call (job)
|
194
|
+
do_something_about_it
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# ...
|
199
|
+
|
200
|
+
scheduler.in '3d', JobThing.new('http://news.example.com/data_xyz')
|
201
|
+
scheduler.in '1w', JobThing.new('http://news.example.com/data_abc'), :timeout => '1d'
|
202
|
+
|
203
|
+
rufus-scheduler accepts anything that responds to a call method with a unique parameter (it will pass the job) as a 'schedulable'.
|
204
|
+
|
205
|
+
For compatibility with older (1.x) versions, schedulables with a trigger methods are accepted :
|
206
|
+
|
207
|
+
class JobThing
|
208
|
+
def trigger (params)
|
209
|
+
job = params[:job]
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
The 'params' correspond to the scheduler job params, and the key :job points to the rufus-scheduler job for the schedulable that is passed to a 'call schedulable'.
|
214
|
+
|
215
|
+
|
216
|
+
== looking up jobs
|
217
|
+
|
218
|
+
scheduler.jobs
|
219
|
+
# returns a map job_id => job of at/in/every jobs
|
220
|
+
|
221
|
+
scheduler.cron_jobs
|
222
|
+
# idem for cron jobs
|
223
|
+
|
224
|
+
scheduler.all_jobs
|
225
|
+
# idem but for every/at/in/cron jobs (all of them)
|
226
|
+
|
227
|
+
scheduler.find_by_tag(t)
|
228
|
+
# returns all the jobs with a given tag (passed at schedule time with :tags)
|
229
|
+
|
230
|
+
|
231
|
+
== unscheduling jobs
|
232
|
+
|
233
|
+
The 'scheduling' methods always return an instance of Rufus::Scheduler::Job. This object can be used for unscheduling :
|
234
|
+
|
235
|
+
job = scheduler.in '2d', :tags => 'admin' do
|
236
|
+
run_backlog_cleaning()
|
237
|
+
end
|
238
|
+
|
239
|
+
# later ...
|
240
|
+
|
241
|
+
job.unschedule
|
242
|
+
# or
|
243
|
+
scheduler.unschedule(job.job_id)
|
244
|
+
|
245
|
+
|
246
|
+
== tags
|
247
|
+
|
248
|
+
You can specify tags at schedule time :
|
249
|
+
|
250
|
+
scheduler.in '2d', :tags => 'admin' do
|
251
|
+
run_backlog_cleaning()
|
252
|
+
end
|
253
|
+
scheduler.every '3m', :tags => 'production' do
|
254
|
+
check_order_log()
|
255
|
+
end
|
256
|
+
|
257
|
+
And later query the scheduler for those jobs :
|
258
|
+
|
259
|
+
admin_jobs = scheduler.find_by_tag('admin')
|
260
|
+
production_jobs = scheduler.find_by_tag('production')
|
261
|
+
|
262
|
+
|
263
|
+
== timeout
|
264
|
+
|
265
|
+
One can specify a timeout for the triggering of a job.
|
266
|
+
|
267
|
+
scheduler.every '2d', :timeout => '40m' do
|
268
|
+
begin
|
269
|
+
run_backlog_cleaning()
|
270
|
+
rescue Rufus::Scheduler::TimeOutError => toe
|
271
|
+
# timeout occurred
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
This job will run every two days. If a run takes more than 40 minutes it will timeout (its thread will receive a TimeOutError).
|
276
|
+
|
277
|
+
This timeout feature relies on an 'in' job scheduled at the moment the main job gets triggered, hence the '40m' time string format.
|
278
|
+
|
279
|
+
|
280
|
+
== exceptions in jobs
|
281
|
+
|
282
|
+
By default, when exception occur when a job performs, the error message will be output to the STDOUT.
|
283
|
+
|
284
|
+
It's easy to customize that behaviour :
|
285
|
+
|
286
|
+
scheduler = Rufus::Scheduler::PlainScheduler.start_new
|
287
|
+
# or
|
288
|
+
#scheduler = Rufus::Scheduler::EmScheduler.start_new
|
289
|
+
|
290
|
+
def scheduler.handle_exception (job, exception)
|
291
|
+
puts "job #{job.job_id} caught exception '#{exception}'"
|
292
|
+
end
|
293
|
+
|
294
|
+
For backward compatibility, overriding #log_exception is still OK :
|
295
|
+
|
296
|
+
def scheduler.log_exception (exception)
|
297
|
+
puts "caught exception '#{exception}'"
|
298
|
+
end
|
299
|
+
|
300
|
+
Note that an every job or a cron job will stay scheduled even if it experiences an exception.
|
301
|
+
|
302
|
+
|
303
|
+
== frequency
|
304
|
+
|
305
|
+
The default frequency for the scheduler is 0.330 seconds. This means that the usual scheduler implementation will wake up, trigger jobs that are to be triggered and then go back to sleep for 0.330 seconds. Note that this doesn't mean that the scheduler will wake up very 0.330 seconds (checking and triggering do take time).
|
306
|
+
|
307
|
+
You can set a different frequency when starting / initializing the scheduler :
|
308
|
+
|
309
|
+
require 'rubygems'
|
310
|
+
require 'rufus/scheduler'
|
311
|
+
|
312
|
+
scheduler = Rufus::Scheduler.start_new(:frequency => 60.0)
|
313
|
+
# for a lazy scheduler that only wakes up every 60 seconds
|
314
|
+
|
315
|
+
|
316
|
+
== usage with EventMachine
|
317
|
+
|
318
|
+
rufus-scheduler 2.0 can be used in conjunction with EventMachine (http://github.com/eventmachine/eventmachine/).
|
319
|
+
|
320
|
+
More and more ruby applications are using EventMachine. This flavour of the scheduler relies on EventMachine, thus it doesn't require a separate thread like the PlainScheduler does.
|
321
|
+
|
322
|
+
require 'rubygems'
|
323
|
+
require 'eventmachine'
|
324
|
+
|
325
|
+
EM.run {
|
326
|
+
|
327
|
+
scheduler = Rufus::Scheduler::EmScheduler.start_new
|
328
|
+
|
329
|
+
scheduler.in '20m' do
|
330
|
+
puts "order ristretto"
|
331
|
+
end
|
332
|
+
}
|
333
|
+
|
334
|
+
|
335
|
+
== tested with
|
336
|
+
|
337
|
+
ruby 1.8.6, ruby 1.9.1p0
|
338
|
+
on jruby 1.2.0 it has some tiny issues (spec/blocking_spec.rb)
|
339
|
+
|
340
|
+
|
341
|
+
== dependencies
|
342
|
+
|
343
|
+
the ruby gem 'eventmachine' if you use Rufus::Scheduler::EmScheduler, else no other dependencies.
|
344
|
+
|
345
|
+
|
346
|
+
== mailing list
|
347
|
+
|
348
|
+
On the rufus-ruby list :
|
349
|
+
|
350
|
+
http://groups.google.com/group/rufus-ruby
|
351
|
+
|
352
|
+
|
353
|
+
== issue tracker
|
354
|
+
|
355
|
+
http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
|
356
|
+
|
357
|
+
|
358
|
+
== irc
|
359
|
+
|
360
|
+
irc.freenode.net #ruote
|
361
|
+
|
362
|
+
|
363
|
+
== source
|
364
|
+
|
365
|
+
http://github.com/jmettraux/rufus-scheduler
|
366
|
+
|
367
|
+
git clone git://github.com/jmettraux/rufus-scheduler.git
|
368
|
+
|
369
|
+
|
370
|
+
== credits
|
371
|
+
|
372
|
+
http://github.com/jmettraux/rufus-scheduler/blob/master/CREDITS.txt
|
373
|
+
|
374
|
+
|
375
|
+
== authors
|
376
|
+
|
377
|
+
John Mettraux, jmettraux@gmail.com, http://jmettraux.wordpress.com
|
378
|
+
|
379
|
+
|
380
|
+
== the rest of Rufus
|
381
|
+
|
382
|
+
http://rufus.rubyforge.org
|
383
|
+
|
384
|
+
|
385
|
+
== license
|
386
|
+
|
387
|
+
MIT
|
388
|
+
|