rufus-scheduler 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.txt CHANGED
@@ -2,5 +2,13 @@
2
2
  = rufus-scheduler CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-scheduler - 1.0.2 released 2008/01/26
6
+
7
+ - patch #17505 : added params :first_at and :first_in for schedule_every()
8
+ a patch by John Didion - s115
9
+
10
+
5
11
  == rufus-scheduler - 1.0 released 2008/01/24
6
12
 
13
+ - initial release after "openwferu-scheduler" - 0.9.16
14
+
@@ -45,7 +45,25 @@ module Rufus
45
45
  # params (usually an array or nil), either a block, which is more in the
46
46
  # Ruby way.
47
47
  #
48
+ # == The gem "openwferu-scheduler"
49
+ #
50
+ # This scheduler was previously known as the "openwferu-scheduler" gem.
51
+ #
52
+ # To ensure that code tapping the previous gem still runs fine with
53
+ # "rufus-scheduler", this new gem has 'pointers' for the old class
54
+ # names.
55
+ #
56
+ # require 'rubygems'
57
+ # require 'openwfe/util/scheduler'
58
+ # s = OpenWFE::Scheduler.new
59
+ #
60
+ # will still run OK with "rufus-scheduler".
61
+ #
48
62
  # == Examples
63
+ #
64
+ # require 'rubygems'
65
+ # require 'rufus/scheduler'
66
+ #
49
67
  #
50
68
  # scheduler.schedule_in("3d") do
51
69
  # regenerate_monthly_report()
@@ -199,6 +217,19 @@ module Rufus
199
217
  # params[:dont_reschedule] = true if $mail
200
218
  # end
201
219
  #
220
+ # == 'Every jobs', :first_at and :first_in
221
+ #
222
+ # Since rufus-scheduler 1.0.2, the schedule_every methods recognizes two
223
+ # optional parameters, :first_at and :first_in
224
+ #
225
+ # scheduler.schedule_every "2d", :first_in => "5h" do
226
+ # # schedule something every two days, start in 5 hours...
227
+ # end
228
+ #
229
+ # scheduler.schedule_every "2d", :first_at => "5h" do
230
+ # # schedule something every two days, start in 5 hours...
231
+ # end
232
+ #
202
233
  class Scheduler
203
234
 
204
235
  #
@@ -375,6 +406,13 @@ module Rufus
375
406
  # # won't get rescheduled in case of exception
376
407
  # end
377
408
  #
409
+ # Since rufus-scheduler 1.0.2, the params :first_at and :first_in are
410
+ # accepted.
411
+ #
412
+ # scheduler.schedule_every "2d", :first_in => "5h" do
413
+ # # schedule something every two days, start in 5 hours...
414
+ # end
415
+ #
378
416
  def schedule_every (freq, params={}, &block)
379
417
 
380
418
  f = duration_to_f freq
@@ -383,9 +421,17 @@ module Rufus
383
421
  schedulable = params[:schedulable]
384
422
  params[:every] = freq
385
423
 
386
- last_at = params[:last_at]
387
- next_at = if last_at
388
- last_at + f
424
+ first_at = params.delete :first_at
425
+ first_in = params.delete :first_in
426
+
427
+ previous_at = params[:previous_at]
428
+
429
+ next_at = if first_at
430
+ first_at
431
+ elsif first_in
432
+ Time.now.to_f + duration_to_f(first_in)
433
+ elsif previous_at
434
+ previous_at + f
389
435
  else
390
436
  Time.now.to_f + f
391
437
  end
@@ -423,7 +469,7 @@ module Rufus
423
469
  # ok, reschedule ...
424
470
 
425
471
  params[:job_id] = job_id
426
- params[:last_at] = at
472
+ params[:previous_at] = at
427
473
 
428
474
  schedule_every params[:every], params, &block
429
475
  #
@@ -662,14 +708,7 @@ module Rufus
662
708
 
663
709
  #puts "0 at is '#{at.to_s}' (#{at.class})"
664
710
 
665
- at = Rufus::to_ruby_time(at) \
666
- if at.kind_of?(String)
667
-
668
- at = Rufus::to_gm_time(at) \
669
- if at.kind_of?(DateTime)
670
-
671
- at = at.to_f \
672
- if at.kind_of?(Time)
711
+ at = at_to_f at
673
712
 
674
713
  #puts "1 at is '#{at.to_s}' (#{at.class})"}"
675
714
 
@@ -677,13 +716,14 @@ module Rufus
677
716
 
678
717
  job_id = params[:job_id]
679
718
 
680
- b = to_block(params, &block)
719
+ b = to_block params, &block
681
720
 
682
- job = jobClass.new(self, at, job_id, params, &b)
721
+ job = jobClass.new self, at, job_id, params, &b
683
722
 
684
723
  #do_unschedule(job_id) if job_id
685
724
 
686
725
  if at < (Time.new.to_f + @precision)
726
+
687
727
  job.trigger() unless params[:discard_past]
688
728
  return nil
689
729
  end
@@ -707,6 +747,18 @@ module Rufus
707
747
  Float(s.to_s)
708
748
  end
709
749
 
750
+ #
751
+ # Ensures an 'at' instance is translated to a float
752
+ # (to be compared with the float coming from time.to_f)
753
+ #
754
+ def at_to_f (at)
755
+
756
+ at = Rufus::to_ruby_time(at) if at.kind_of?(String)
757
+ at = Rufus::to_gm_time(at) if at.kind_of?(DateTime)
758
+ at = at.to_f if at.kind_of?(Time)
759
+ at
760
+ end
761
+
710
762
  #
711
763
  # Returns a block. If a block is passed, will return it, else,
712
764
  # if a :schedulable is set in the params, will return a block
@@ -20,7 +20,7 @@ class Scheduler4Test < Test::Unit::TestCase
20
20
  #end
21
21
 
22
22
  #
23
- # Checking that a sleep in a schedule won't raise any execption
23
+ # Checking that a sleep in a schedule won't raise any exception
24
24
  #
25
25
  def test_0
26
26
 
@@ -0,0 +1,82 @@
1
+
2
+ #
3
+ # Testing the 'rufus-scheduler'
4
+ #
5
+ # John Mettraux at openwfe.org
6
+ #
7
+ # Sat Jan 26 20:05:57 JST 2008
8
+ #
9
+
10
+ require 'test/unit'
11
+ require 'rufus/scheduler'
12
+
13
+
14
+ class Scheduler5Test < Test::Unit::TestCase
15
+
16
+ #def setup
17
+ #end
18
+
19
+ #def teardown
20
+ #end
21
+
22
+ #
23
+ # Testing the :first_at parameter
24
+ #
25
+ def test_0
26
+
27
+ s = Rufus::Scheduler.new
28
+ s.start
29
+
30
+ $count = 0
31
+
32
+ fa = Time.now + 3
33
+
34
+ s.schedule_every "1s", :first_at => fa do
35
+ $count += 1
36
+ end
37
+
38
+ sleep 1
39
+
40
+ assert_equal 0, $count
41
+
42
+ sleep 3
43
+
44
+ assert_equal 1, $count
45
+
46
+ sleep 1
47
+
48
+ assert_equal 2, $count
49
+
50
+ s.stop
51
+ end
52
+
53
+ #
54
+ # Testing the :first_in parameter
55
+ #
56
+ def test_1
57
+
58
+ s = Rufus::Scheduler.new
59
+ s.start
60
+
61
+ $count = 0
62
+
63
+ s.schedule_every "1s", :first_in => "3s" do
64
+ $count += 1
65
+ end
66
+
67
+ sleep 1
68
+
69
+ assert_equal 0, $count
70
+
71
+ sleep 3
72
+
73
+ assert_equal 1, $count
74
+
75
+ sleep 1
76
+
77
+ assert_equal 2, $count
78
+
79
+ s.stop
80
+ end
81
+ end
82
+
data/test/test.rb CHANGED
@@ -6,6 +6,7 @@ require 'scheduler_1_test'
6
6
  require 'scheduler_2_test'
7
7
  require 'scheduler_3_test'
8
8
  require 'scheduler_4_test'
9
+ require 'scheduler_5_test'
9
10
 
10
11
  require 'cron_test'
11
12
  require 'cronline_test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-01-24 00:00:00 +09:00
12
+ date: 2008-01-26 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -36,6 +36,7 @@ files:
36
36
  - test/scheduler_2_test.rb
37
37
  - test/scheduler_3_test.rb
38
38
  - test/scheduler_4_test.rb
39
+ - test/scheduler_5_test.rb
39
40
  - test/test.rb
40
41
  - test/time_test.rb
41
42
  - README.txt