openwferu-scheduler 0.9.9 → 0.9.11
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/openwfe/util/scheduler.rb +107 -77
- metadata +2 -2
@@ -81,9 +81,15 @@ module OpenWFE
|
|
81
81
|
#
|
82
82
|
# regenerator = Regenerator.new
|
83
83
|
#
|
84
|
-
# scheduler.schedule_in("4d", regenerator
|
84
|
+
# scheduler.schedule_in("4d", regenerator)
|
85
85
|
# #
|
86
|
-
# # will regenerate the
|
86
|
+
# # will regenerate the report in four days
|
87
|
+
#
|
88
|
+
# scheduler.schedule_in(
|
89
|
+
# "5d",
|
90
|
+
# { :schedulable => regenerator, :scope => :month })
|
91
|
+
# #
|
92
|
+
# # will regenerate the monthly report in 5 days
|
87
93
|
#
|
88
94
|
# There is also schedule_every() :
|
89
95
|
#
|
@@ -178,9 +184,11 @@ module OpenWFE
|
|
178
184
|
# Schedules a job by specifying at which time it should trigger.
|
179
185
|
# Returns the a job_id that can be used to unschedule the job.
|
180
186
|
#
|
181
|
-
def schedule_at (at,
|
187
|
+
def schedule_at (at, params={}, &block)
|
188
|
+
|
189
|
+
params = prepare_params(params)
|
182
190
|
|
183
|
-
sschedule_at(
|
191
|
+
sschedule_at(at, params, &block)
|
184
192
|
end
|
185
193
|
|
186
194
|
|
@@ -188,12 +196,12 @@ module OpenWFE
|
|
188
196
|
# Schedules a job by stating in how much time it should trigger.
|
189
197
|
# Returns the a job_id that can be used to unschedule the job.
|
190
198
|
#
|
191
|
-
def schedule_in (duration,
|
199
|
+
def schedule_in (duration, params={}, &block)
|
192
200
|
|
193
201
|
duration = duration_to_f(duration)
|
202
|
+
params = prepare_params(params)
|
194
203
|
|
195
|
-
|
196
|
-
Time.new.to_f + duration, schedulable, params, &block)
|
204
|
+
schedule_at(Time.new.to_f + duration, params, &block)
|
197
205
|
end
|
198
206
|
|
199
207
|
#
|
@@ -210,9 +218,29 @@ module OpenWFE
|
|
210
218
|
# end
|
211
219
|
# end
|
212
220
|
#
|
213
|
-
def schedule_every (freq,
|
221
|
+
def schedule_every (freq, params={}, &block)
|
222
|
+
|
223
|
+
f = duration_to_f freq
|
224
|
+
|
225
|
+
params = prepare_params params
|
226
|
+
schedulable = params[:schedulable]
|
227
|
+
params[:every] = true
|
214
228
|
|
215
|
-
|
229
|
+
sschedule_at Time.new.to_f + f, params do |job_id, at|
|
230
|
+
|
231
|
+
params[:job_id] = job_id
|
232
|
+
|
233
|
+
if schedulable
|
234
|
+
schedulable.trigger(params)
|
235
|
+
else
|
236
|
+
block.call job_id, at
|
237
|
+
end
|
238
|
+
|
239
|
+
schedule_every(f, params, &block) \
|
240
|
+
unless @dont_reschedule_every
|
241
|
+
|
242
|
+
job_id
|
243
|
+
end
|
216
244
|
end
|
217
245
|
|
218
246
|
#
|
@@ -224,14 +252,12 @@ module OpenWFE
|
|
224
252
|
|
225
253
|
for i in 0...@pending_jobs.length
|
226
254
|
if @pending_jobs[i].eid == job_id
|
227
|
-
@pending_jobs.delete_at
|
255
|
+
@pending_jobs.delete_at i
|
228
256
|
return true
|
229
257
|
end
|
230
258
|
end
|
231
259
|
|
232
|
-
|
233
|
-
|
234
|
-
return false
|
260
|
+
unschedule_cron_job job_id
|
235
261
|
end
|
236
262
|
end
|
237
263
|
|
@@ -241,10 +267,10 @@ module OpenWFE
|
|
241
267
|
def unschedule_cron_job (job_id)
|
242
268
|
synchronize do
|
243
269
|
if @cron_entries.has_key?(job_id)
|
244
|
-
@cron_entries.delete
|
270
|
+
@cron_entries.delete job_id
|
245
271
|
return true
|
246
272
|
end
|
247
|
-
|
273
|
+
false
|
248
274
|
end
|
249
275
|
end
|
250
276
|
|
@@ -255,11 +281,11 @@ module OpenWFE
|
|
255
281
|
#
|
256
282
|
# For example :
|
257
283
|
#
|
258
|
-
# scheduler.schedule("5 0 * * *",
|
259
|
-
# # will trigger the schedulable s
|
284
|
+
# scheduler.schedule("5 0 * * *", s)
|
285
|
+
# # will trigger the schedulable s every day
|
260
286
|
# # five minutes after midnight
|
261
287
|
#
|
262
|
-
# scheduler.schedule("15 14 1 * *",
|
288
|
+
# scheduler.schedule("15 14 1 * *", s)
|
263
289
|
# # will trigger s at 14:15 on the first of every month
|
264
290
|
#
|
265
291
|
# scheduler.schedule("0 22 * * 1-5") do
|
@@ -270,29 +296,28 @@ module OpenWFE
|
|
270
296
|
# Returns the job id attributed to this 'cron job', this id can
|
271
297
|
# be used to unschedule the job.
|
272
298
|
#
|
273
|
-
def schedule (
|
274
|
-
cron_line, cron_id=nil, schedulable=nil, params=nil, &block)
|
299
|
+
def schedule (cron_line, params={}, &block)
|
275
300
|
|
276
301
|
synchronize do
|
302
|
+
|
303
|
+
params = prepare_params(params)
|
277
304
|
|
278
305
|
#
|
279
306
|
# is a job with the same id already scheduled ?
|
280
307
|
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
end
|
286
|
-
end
|
308
|
+
cron_id = params[:cron_id]
|
309
|
+
cron_id = params[:job_id] unless cron_id
|
310
|
+
|
311
|
+
unschedule(cron_id) if cron_id
|
287
312
|
|
288
313
|
#
|
289
314
|
# schedule
|
290
315
|
|
291
|
-
b = to_block(
|
316
|
+
b = to_block(params, &block)
|
292
317
|
entry = CronEntry.new(cron_id, cron_line, &b)
|
293
318
|
@cron_entries[entry.eid] = entry
|
294
319
|
|
295
|
-
|
320
|
+
entry.eid
|
296
321
|
end
|
297
322
|
end
|
298
323
|
|
@@ -305,11 +330,9 @@ module OpenWFE
|
|
305
330
|
entry = @cron_entries[job_id]
|
306
331
|
return entry if entry
|
307
332
|
|
308
|
-
@pending_jobs.
|
309
|
-
|
333
|
+
@pending_jobs.find do |entry|
|
334
|
+
entry.eid == job_id
|
310
335
|
end
|
311
|
-
|
312
|
-
return nil
|
313
336
|
end
|
314
337
|
|
315
338
|
#
|
@@ -323,7 +346,8 @@ module OpenWFE
|
|
323
346
|
j = get_job(job_id)
|
324
347
|
|
325
348
|
return j.schedulable if j.respond_to? :schedulable
|
326
|
-
|
349
|
+
|
350
|
+
nil
|
327
351
|
end
|
328
352
|
|
329
353
|
#
|
@@ -359,34 +383,49 @@ module OpenWFE
|
|
359
383
|
# Returns true if the given string seems to be a cron string.
|
360
384
|
#
|
361
385
|
def Scheduler.is_cron_string (s)
|
362
|
-
|
386
|
+
s.match(".+ .+ .+ .+ .+")
|
363
387
|
end
|
364
388
|
|
365
389
|
protected
|
366
390
|
|
367
|
-
|
368
|
-
|
391
|
+
#
|
392
|
+
# Making sure that params is a Hash.
|
393
|
+
#
|
394
|
+
def prepare_params (params)
|
395
|
+
params = { :schedulable => params } \
|
396
|
+
if params.is_a?(Schedulable)
|
397
|
+
params
|
398
|
+
end
|
399
|
+
|
400
|
+
def sschedule_at (at, params={}, &block)
|
369
401
|
|
370
402
|
synchronize do
|
371
403
|
|
372
404
|
#puts "0 at is '#{at.to_s}' (#{at.class})"
|
373
405
|
|
374
406
|
at = OpenWFE::to_ruby_time(at) \
|
375
|
-
if at.kind_of?
|
407
|
+
if at.kind_of?(String)
|
376
408
|
|
377
409
|
at = OpenWFE::to_gm_time(at) \
|
378
|
-
if at.kind_of?
|
410
|
+
if at.kind_of?(DateTime)
|
379
411
|
|
380
412
|
at = at.to_f \
|
381
|
-
if at.kind_of?
|
413
|
+
if at.kind_of?(Time)
|
382
414
|
|
383
415
|
#puts "1 at is '#{at.to_s}' (#{at.class})"}"
|
384
416
|
|
385
|
-
jobClass =
|
386
|
-
|
417
|
+
jobClass = if params[:every]
|
418
|
+
EveryEntry
|
419
|
+
else
|
420
|
+
AtEntry
|
421
|
+
end
|
422
|
+
|
423
|
+
job_id = params[:job_id]
|
424
|
+
|
425
|
+
b = to_block(params, &block)
|
426
|
+
job = jobClass.new(at, job_id, &b)
|
387
427
|
|
388
|
-
|
389
|
-
job = jobClass.new(at, at_id, &b)
|
428
|
+
unschedule(job_id) if job_id
|
390
429
|
|
391
430
|
if at < (Time.new.to_f + @precision)
|
392
431
|
job.trigger()
|
@@ -408,30 +447,10 @@ module OpenWFE
|
|
408
447
|
end
|
409
448
|
end
|
410
449
|
|
411
|
-
|
450
|
+
push(job)
|
412
451
|
end
|
413
452
|
end
|
414
453
|
|
415
|
-
def sschedule_every (freq, at_id, schedulable, params, &block)
|
416
|
-
|
417
|
-
f = duration_to_f(freq)
|
418
|
-
|
419
|
-
job_id = sschedule_at(
|
420
|
-
true, Time.new.to_f + f, at_id) do |eid, at|
|
421
|
-
|
422
|
-
if schedulable
|
423
|
-
schedulable.trigger(params)
|
424
|
-
else
|
425
|
-
block.call eid, at
|
426
|
-
end
|
427
|
-
|
428
|
-
sschedule_every(f, eid, schedulable, params, &block) \
|
429
|
-
unless @dont_reschedule_every
|
430
|
-
end
|
431
|
-
|
432
|
-
job_id
|
433
|
-
end
|
434
|
-
|
435
454
|
#
|
436
455
|
# Ensures that a duration is a expressed as a Float instance.
|
437
456
|
#
|
@@ -442,22 +461,33 @@ module OpenWFE
|
|
442
461
|
def duration_to_f (s)
|
443
462
|
return s if s.kind_of? Float
|
444
463
|
return OpenWFE::parse_time_string(s) if s.kind_of? String
|
445
|
-
|
464
|
+
Float(s.to_s)
|
446
465
|
end
|
447
466
|
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
467
|
+
#
|
468
|
+
# Returns a block. If a block is passed, will return it, else,
|
469
|
+
# if a :schedulable is set in the params, will return a block
|
470
|
+
# wrapping a call to it.
|
471
|
+
#
|
472
|
+
def to_block (params, &block)
|
473
|
+
|
474
|
+
return block if block
|
475
|
+
|
476
|
+
schedulable = params[:schedulable]
|
477
|
+
|
478
|
+
return nil unless schedulable
|
479
|
+
|
480
|
+
params.delete :schedulable
|
481
|
+
|
482
|
+
l = lambda do
|
483
|
+
schedulable.trigger(params)
|
484
|
+
end
|
485
|
+
class << l
|
486
|
+
attr_accessor :schedulable
|
460
487
|
end
|
488
|
+
l.schedulable = schedulable
|
489
|
+
|
490
|
+
l
|
461
491
|
end
|
462
492
|
|
463
493
|
#
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: openwferu-scheduler
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.9.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.9.11
|
7
|
+
date: 2007-06-06 00:00:00 +09:00
|
8
8
|
summary: OpenWFEru scheduler for Ruby (at, cron and every)
|
9
9
|
require_paths:
|
10
10
|
- lib
|