rufus-scheduler 3.0.3 → 3.0.4

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,6 +2,11 @@
2
2
  = rufus-scheduler CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-scheduler - 3.0.4 released 2014/10/19
6
+
7
+ - make CronLine#frequency faster (to avoid 20s schedule_cron times)
8
+
9
+
5
10
  == rufus-scheduler - 3.0.3 released 2013/12/12
6
11
 
7
12
  - CronLine#previous_time fix by Yassen Bantchev (https://github.com/yassenb)
data/LICENSE.txt CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright (c) 2005-2013, John Mettraux, jmettraux@gmail.com
2
+ Copyright (c) 2005-2014, John Mettraux, jmettraux@gmail.com
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -717,16 +717,22 @@ Rufus::Scheduler.parse('* * * * * *').frequency # ==> 1
717
717
  Rufus::Scheduler.parse('5 23 * * *').frequency # ==> 24 * 3600
718
718
  Rufus::Scheduler.parse('5 * * * *').frequency # ==> 3600
719
719
  Rufus::Scheduler.parse('10,20,30 * * * *').frequency # ==> 600
720
+
721
+ Rufus::Scheduler.parse('10,20,30 * * * * *').frequency # ==> 10
720
722
  ```
721
723
 
722
724
  It's used to determine if the job frequency is higher than the scheduler frequency (it raises an ArgumentError if that is the case).
723
725
 
726
+ ### brute_frequency
727
+
728
+ Cron jobs also have a ```#brute_frequency``` method that looks a one year of intervals to determine the shortest delta for the cron. This method can take between 20 to 50 seconds for cron lines that go the second level. ```#frequency``` above, when encountering second level cron lines will take a shortcut to answer as quickly as possible with a usable value.
729
+
724
730
 
725
731
  ## looking up jobs
726
732
 
727
733
  ### Scheduler#job(job_id)
728
734
 
729
- The scheduler #job(job_id) method can be used to lookup Job instances.
735
+ The scheduler ```#job(job_id)``` method can be used to lookup Job instances.
730
736
 
731
737
  ```ruby
732
738
  require 'rufus-scheduler'
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2006-2013, John Mettraux, jmettraux@gmail.com
2
+ # Copyright (c) 2006-2014, John Mettraux, jmettraux@gmail.com
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
@@ -38,7 +38,7 @@ module Rufus
38
38
  require 'rufus/scheduler/cronline'
39
39
  require 'rufus/scheduler/job_array'
40
40
 
41
- VERSION = '3.0.3'
41
+ VERSION = '3.0.4'
42
42
 
43
43
  #
44
44
  # A common error class for rufus-scheduler
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2006-2013, John Mettraux, jmettraux@gmail.com
2
+ # Copyright (c) 2006-2014, John Mettraux, jmettraux@gmail.com
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
@@ -197,6 +197,28 @@ class Rufus::Scheduler
197
197
  ]
198
198
  end
199
199
 
200
+ # Returns a quickly computed approximation of the frequency for this
201
+ # cron line.
202
+ #
203
+ # #brute_frequency, on the other hand, will compute the frequency by
204
+ # examining a whole, that can take more than seconds for a seconds
205
+ # level cron...
206
+ #
207
+ def frequency
208
+
209
+ return brute_frequency unless @seconds && @seconds.length > 1
210
+
211
+ delta = 60
212
+ prev = @seconds[0]
213
+
214
+ @seconds[1..-1].each do |sec|
215
+ d = sec - prev
216
+ delta = d if d < delta
217
+ end
218
+
219
+ delta
220
+ end
221
+
200
222
  # Returns the shortest delta between two potential occurences of the
201
223
  # schedule described by this cronline.
202
224
  #
@@ -221,7 +243,7 @@ class Rufus::Scheduler
221
243
  # See https://github.com/jmettraux/rufus-scheduler/issues/89
222
244
  # for a discussion about this method.
223
245
  #
224
- def frequency
246
+ def brute_frequency
225
247
 
226
248
  delta = 366 * DAY_S
227
249
 
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2006-2013, John Mettraux, jmettraux@gmail.com
2
+ # Copyright (c) 2006-2014, John Mettraux, jmettraux@gmail.com
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2006-2013, John Mettraux, jmettraux@gmail.com
2
+ # Copyright (c) 2006-2014, John Mettraux, jmettraux@gmail.com
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
@@ -535,6 +535,11 @@ module Rufus
535
535
  @cron_line.frequency
536
536
  end
537
537
 
538
+ def brute_frequency
539
+
540
+ @cron_line.brute_frequency
541
+ end
542
+
538
543
  protected
539
544
 
540
545
  def set_next_time(is_post, trigger_time)
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2006-2013, John Mettraux, jmettraux@gmail.com
2
+ # Copyright (c) 2006-2014, John Mettraux, jmettraux@gmail.com
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
@@ -396,6 +396,23 @@ describe Rufus::Scheduler::CronLine do
396
396
  match '* * * * sun#2,sun#3', local(1970, 1, 18)
397
397
  no_match '* * * * sun#2,sun#3', local(1970, 1, 25)
398
398
  end
399
+
400
+ it 'matches correctly for seconds' do
401
+
402
+ match '* * * * * *', local(1970, 1, 11)
403
+ match '* * * * * *', local(1970, 1, 11, 0, 0, 13)
404
+ end
405
+
406
+ it 'matches correctly for seconds / interval' do
407
+
408
+ match '*/2 * * * * *', local(1970, 1, 11)
409
+ match '*/5 * * * * *', local(1970, 1, 11)
410
+ match '*/5 * * * * *', local(1970, 1, 11, 0, 0, 0)
411
+ no_match '*/5 * * * * *', local(1970, 1, 11, 0, 0, 1)
412
+ match '*/5 * * * * *', local(1970, 1, 11, 0, 0, 5)
413
+ match '*/2 * * * * *', local(1970, 1, 11, 0, 0, 2)
414
+ match '*/2 * * * * *', local(1970, 1, 11, 0, 0, 2, 500)
415
+ end
399
416
  end
400
417
 
401
418
  describe '#monthdays' do
@@ -420,12 +437,43 @@ describe Rufus::Scheduler::CronLine do
420
437
 
421
438
  it 'returns the shortest delta between two occurrences' do
422
439
 
423
- Rufus::Scheduler::CronLine.new('* * * * *').frequency.should == 60
424
- Rufus::Scheduler::CronLine.new('* * * * * *').frequency.should == 1
440
+ Rufus::Scheduler::CronLine.new(
441
+ '* * * * *').frequency.should == 60
442
+ Rufus::Scheduler::CronLine.new(
443
+ '* * * * * *').frequency.should == 1
444
+
445
+ Rufus::Scheduler::CronLine.new(
446
+ '5 23 * * *').frequency.should == 24 * 3600
447
+ Rufus::Scheduler::CronLine.new(
448
+ '5 * * * *').frequency.should == 3600
449
+ Rufus::Scheduler::CronLine.new(
450
+ '10,20,30 * * * *').frequency.should == 600
451
+
452
+ Rufus::Scheduler::CronLine.new(
453
+ '10,20,30 * * * * *').frequency.should == 10
454
+ end
455
+ end
456
+
457
+ describe '#brute_frequency' do
458
+
459
+ it 'returns the shortest delta between two occurrences' do
460
+
461
+ Rufus::Scheduler::CronLine.new(
462
+ '* * * * *').brute_frequency.should == 60
463
+ Rufus::Scheduler::CronLine.new(
464
+ '* * * * * *').brute_frequency.should == 1
425
465
 
426
- Rufus::Scheduler::CronLine.new('5 23 * * *').frequency.should == 24 * 3600
427
- Rufus::Scheduler::CronLine.new('5 * * * *').frequency.should == 3600
428
- Rufus::Scheduler::CronLine.new('10,20,30 * * * *').frequency.should == 600
466
+ Rufus::Scheduler::CronLine.new(
467
+ '5 23 * * *').brute_frequency.should == 24 * 3600
468
+ Rufus::Scheduler::CronLine.new(
469
+ '5 * * * *').brute_frequency.should == 3600
470
+ Rufus::Scheduler::CronLine.new(
471
+ '10,20,30 * * * *').brute_frequency.should == 600
472
+
473
+ #Rufus::Scheduler::CronLine.new(
474
+ # '10,20,30 * * * * *').brute_frequency.should == 10
475
+ #
476
+ # takes > 20s ...
429
477
  end
430
478
  end
431
479
  end
@@ -62,6 +62,45 @@ describe Rufus::Scheduler::CronJob do
62
62
  job.first_at.should be_within_1s_of(t + 3)
63
63
  job.last_time.should == nil
64
64
  end
65
+
66
+ it 'triggers for the first time at first_at' do
67
+
68
+ first_time = nil
69
+ t = Time.now
70
+
71
+ job = @scheduler.schedule_cron '* * * * * *', :first_in => '3s' do
72
+ first_time ||= Time.now
73
+ end
74
+ sleep 4.5
75
+
76
+ job.first_at.should be_within_1s_of(t + 3)
77
+ first_time.should be_within_1s_of(job.first_at)
78
+ end
79
+ end
80
+
81
+ context 'scheduling the cron itself' do
82
+
83
+ # for https://github.com/jmettraux/rufus-scheduler/issues/95
84
+ #
85
+ # schedule_cron takes more than 30 seconds, blocking...
86
+ #
87
+ it 'does not sit scheduling and blocking...' do
88
+
89
+ n = Time.now
90
+ first = nil
91
+
92
+ job = @scheduler.schedule_cron '*/2 * * * * *' do
93
+ first ||= Time.now
94
+ end
95
+
96
+ (Time.now - n).should < 1.0
97
+
98
+ loop do
99
+ next unless first
100
+ (first - n).should < 4.0
101
+ break
102
+ end
103
+ end
65
104
  end
66
105
  end
67
106
 
@@ -363,37 +363,16 @@ describe Rufus::Scheduler do
363
363
  end
364
364
  end
365
365
 
366
- describe '#work_threads(:all)' do
366
+ describe '#work_threads(:all | :vacant)' do
367
367
 
368
368
  it 'returns an empty array when the scheduler has not yet done anything' do
369
369
 
370
370
  @scheduler.work_threads.should == []
371
- end
372
-
373
- it 'lists all the work threads in the pool' do
374
-
375
- @scheduler.in '0s' do
376
- sleep(0.2)
377
- end
378
- @scheduler.in '0s' do
379
- sleep(2.0)
380
- end
381
-
382
- sleep 0.6
383
-
384
- @scheduler.work_threads.size.should == 2
385
- @scheduler.work_threads(:all).size.should == 2
386
- end
387
- end
388
-
389
- describe '#work_threads(:vacant)' do
390
-
391
- it 'returns an empty array when the scheduler has not yet done anything' do
392
-
371
+ @scheduler.work_threads(:all).should == []
393
372
  @scheduler.work_threads(:vacant).should == []
394
373
  end
395
374
 
396
- it 'lists all the vacant work threads in the pool' do
375
+ it 'lists the [vacant] work threads in the pool' do
397
376
 
398
377
  @scheduler.in '0s' do
399
378
  sleep(0.2)
@@ -402,10 +381,17 @@ describe Rufus::Scheduler do
402
381
  sleep(2.0)
403
382
  end
404
383
 
405
- sleep 0.6
384
+ sleep 0.7
406
385
 
407
- @scheduler.work_threads(:all).size.should == 2
408
- @scheduler.work_threads(:vacant).size.should == 1
386
+ if @scheduler.work_threads.size == 1
387
+ @scheduler.work_threads.size.should == 1
388
+ @scheduler.work_threads(:all).size.should == 1
389
+ @scheduler.work_threads(:vacant).size.should == 0
390
+ else
391
+ @scheduler.work_threads.size.should == 2
392
+ @scheduler.work_threads(:all).size.should == 2
393
+ @scheduler.work_threads(:vacant).size.should == 1
394
+ end
409
395
  end
410
396
  end
411
397
 
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: 3.0.3
4
+ version: 3.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-11 00:00:00.000000000 Z
12
+ date: 2014-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tzinfo
@@ -103,7 +103,7 @@ files:
103
103
  homepage: http://github.com/jmettraux/rufus-scheduler
104
104
  licenses:
105
105
  - MIT
106
- post_install_message: ! "\n***\n\nThanks for installing rufus-scheduler 3.0.3\n\nIt
106
+ post_install_message: ! "\n***\n\nThanks for installing rufus-scheduler 3.0.4\n\nIt
107
107
  might not be 100% compatible with rufus-scheduler 2.x.\n\nIf you encounter issues
108
108
  with this new rufus-scheduler, especially\nif your app worked fine with previous
109
109
  versions of it, you can\n\nA) Forget it and peg your Gemfile to rufus-scheduler