rufus-scheduler 3.0.0 → 3.0.9

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/spec/job_spec.rb CHANGED
@@ -38,7 +38,7 @@ describe Rufus::Scheduler::Job do
38
38
 
39
39
  job = @scheduler.schedule_in '10d' do; end
40
40
 
41
- job.last_time.should == nil
41
+ expect(job.last_time).to eq(nil)
42
42
  end
43
43
 
44
44
  it 'returns the last time the job fired' do
@@ -47,7 +47,7 @@ describe Rufus::Scheduler::Job do
47
47
 
48
48
  sleep 0.4
49
49
 
50
- job.last_time.should_not == nil
50
+ expect(job.last_time).not_to eq(nil)
51
51
  end
52
52
  end
53
53
 
@@ -57,7 +57,7 @@ describe Rufus::Scheduler::Job do
57
57
 
58
58
  job = @scheduler.in('1d', :job => true) {}
59
59
 
60
- job.threads.size.should == 0
60
+ expect(job.threads.size).to eq(0)
61
61
  end
62
62
 
63
63
  it 'returns an empty list after the job terminated' do
@@ -66,7 +66,7 @@ describe Rufus::Scheduler::Job do
66
66
 
67
67
  sleep 0.8
68
68
 
69
- job.threads.size.should == 0
69
+ expect(job.threads.size).to eq(0)
70
70
  end
71
71
 
72
72
  it 'lists the threads the job currently runs in' do
@@ -78,10 +78,10 @@ describe Rufus::Scheduler::Job do
78
78
 
79
79
  sleep 0.4
80
80
 
81
- job.threads.size.should == 1
81
+ expect(job.threads.size).to eq(1)
82
82
 
83
83
  t = job.threads.first
84
- t[:rufus_scheduler_job].should == job
84
+ expect(t[:rufus_scheduler_job]).to eq(job)
85
85
  end
86
86
  end
87
87
 
@@ -95,7 +95,7 @@ describe Rufus::Scheduler::Job do
95
95
 
96
96
  job.kill
97
97
 
98
- Thread.list.size.should == tls
98
+ expect(Thread.list.size).to eq(tls)
99
99
  end
100
100
 
101
101
  it 'makes the threads vacant' do
@@ -120,13 +120,13 @@ describe Rufus::Scheduler::Job do
120
120
  v1 = @scheduler.work_threads(:vacant).size
121
121
  a1 = @scheduler.work_threads(:active).size
122
122
 
123
- counter.should == 0
123
+ expect(counter).to eq(0)
124
124
 
125
- v0.should == 0
126
- a0.should == 1
125
+ expect(v0).to eq(0)
126
+ expect(a0).to eq(1)
127
127
 
128
- v1.should == 1
129
- a1.should == 0
128
+ expect(v1).to eq(1)
129
+ expect(a1).to eq(0)
130
130
  end
131
131
  end
132
132
 
@@ -136,7 +136,7 @@ describe Rufus::Scheduler::Job do
136
136
 
137
137
  job = @scheduler.in('1d', :job => true) {}
138
138
 
139
- job.running?.should == false
139
+ expect(job.running?).to eq(false)
140
140
  end
141
141
 
142
142
  it 'returns true when the job is running in at least one thread' do
@@ -145,7 +145,7 @@ describe Rufus::Scheduler::Job do
145
145
 
146
146
  sleep 0.4
147
147
 
148
- job.running?.should == true
148
+ expect(job.running?).to eq(true)
149
149
  end
150
150
  end
151
151
 
@@ -155,7 +155,7 @@ describe Rufus::Scheduler::Job do
155
155
 
156
156
  job = @scheduler.schedule_in('1d') {}
157
157
 
158
- job.scheduled?.should == true
158
+ expect(job.scheduled?).to eq(true)
159
159
  end
160
160
 
161
161
  it 'returns false when the job is not scheduled' do
@@ -164,7 +164,7 @@ describe Rufus::Scheduler::Job do
164
164
 
165
165
  sleep 0.4
166
166
 
167
- job.scheduled?.should == false
167
+ expect(job.scheduled?).to eq(false)
168
168
  end
169
169
 
170
170
  it 'returns true for repeat jobs that are running' do
@@ -173,8 +173,72 @@ describe Rufus::Scheduler::Job do
173
173
 
174
174
  sleep 1
175
175
 
176
- job.running?.should == true
177
- job.scheduled?.should == true
176
+ expect(job.running?).to eq(true)
177
+ expect(job.scheduled?).to eq(true)
178
+ end
179
+ end
180
+
181
+ describe '#call' do
182
+
183
+ it 'calls the job (like it were a proc)' do
184
+
185
+ counter = 0
186
+
187
+ job =
188
+ @scheduler.schedule_in('0.5s') do
189
+ counter = counter + 1
190
+ end
191
+ job.call
192
+
193
+ sleep 0.8
194
+
195
+ expect(counter).to eq(2)
196
+ end
197
+ end
198
+
199
+ describe '#call(true)' do
200
+
201
+ it 'calls the job and let the scheduler handle errors' do
202
+
203
+ $err = nil
204
+
205
+ def @scheduler.on_error(job, err)
206
+ $err = "#{job.class} #{job.original} #{err.message}"
207
+ rescue
208
+ p $!
209
+ end
210
+
211
+ job =
212
+ @scheduler.schedule_in('1d') do
213
+ fail 'again'
214
+ end
215
+
216
+ job.call(true)
217
+
218
+ expect($err).to eq('Rufus::Scheduler::InJob 1d again')
219
+ end
220
+ end
221
+
222
+ describe '#call(false)' do
223
+
224
+ it 'calls the job and let errors slip through' do
225
+
226
+ job =
227
+ @scheduler.schedule_in('1d') do
228
+ fail 'fast'
229
+ end
230
+
231
+ begin
232
+
233
+ #job.call(false)
234
+ job.call # false is the default
235
+
236
+ expect(false).to eq(true)
237
+
238
+ rescue => ex
239
+
240
+ expect(ex.message).to eq('fast')
241
+ end
178
242
  end
179
243
  end
180
244
 
@@ -192,7 +256,7 @@ describe Rufus::Scheduler::Job do
192
256
 
193
257
  sleep 3
194
258
 
195
- job[:counter].should > 1
259
+ expect(job[:counter]).to be > 1
196
260
  end
197
261
  end
198
262
 
@@ -202,7 +266,7 @@ describe Rufus::Scheduler::Job do
202
266
 
203
267
  job = @scheduler.schedule_in '1s' do; end
204
268
 
205
- job[:nada].should == nil
269
+ expect(job[:nada]).to eq(nil)
206
270
  end
207
271
 
208
272
  it 'returns the value of a job-local variable' do
@@ -210,7 +274,7 @@ describe Rufus::Scheduler::Job do
210
274
  job = @scheduler.schedule_in '1s' do; end
211
275
  job[:x] = :y
212
276
 
213
- job[:x].should == :y
277
+ expect(job[:x]).to eq(:y)
214
278
  end
215
279
  end
216
280
 
@@ -221,7 +285,7 @@ describe Rufus::Scheduler::Job do
221
285
  job = @scheduler.schedule_in '1s' do; end
222
286
  job[:x] = :y
223
287
 
224
- job.key?(:x).should == true
288
+ expect(job.key?(:x)).to eq(true)
225
289
  end
226
290
  end
227
291
 
@@ -234,7 +298,7 @@ describe Rufus::Scheduler::Job do
234
298
  job['hello'] = :z
235
299
  job[123] = {}
236
300
 
237
- job.keys.sort_by { |k| k.to_s }.should == [ 123, 'hello', :x ]
301
+ expect(job.keys.sort_by { |k| k.to_s }).to eq([ 123, 'hello', :x ])
238
302
  end
239
303
  end
240
304
  end
@@ -245,21 +309,21 @@ describe Rufus::Scheduler::Job do
245
309
 
246
310
  job = @scheduler.in '10d', :job => true, :tag => 't0' do; end
247
311
 
248
- job.tags.should == %w[ t0 ]
312
+ expect(job.tags).to eq(%w[ t0 ])
249
313
  end
250
314
 
251
315
  it 'accepts an array of tags' do
252
316
 
253
317
  job = @scheduler.in '10d', :job => true, :tag => %w[ t0 t1 ] do; end
254
318
 
255
- job.tags.should == %w[ t0 t1 ]
319
+ expect(job.tags).to eq(%w[ t0 t1 ])
256
320
  end
257
321
 
258
322
  it 'turns tags into strings' do
259
323
 
260
324
  job = @scheduler.in '10d', :job => true, :tags => [ 1, 2 ] do; end
261
325
 
262
- job.tags.should == %w[ 1 2 ]
326
+ expect(job.tags).to eq(%w[ 1 2 ])
263
327
  end
264
328
  end
265
329
 
@@ -274,11 +338,11 @@ describe Rufus::Scheduler::Job do
274
338
 
275
339
  sleep 0.4
276
340
 
277
- job.threads.first.should == @scheduler.thread
341
+ expect(job.threads.first).to eq(@scheduler.thread)
278
342
 
279
343
  sleep 1.4
280
344
 
281
- job.threads.size.should == 0
345
+ expect(job.threads.size).to eq(0)
282
346
  end
283
347
  end
284
348
 
@@ -293,11 +357,11 @@ describe Rufus::Scheduler::Job do
293
357
 
294
358
  sleep 0.4
295
359
 
296
- job.threads.first.should_not == @scheduler.thread
360
+ expect(job.threads.first).not_to eq(@scheduler.thread)
297
361
 
298
362
  sleep 1.4
299
363
 
300
- job.threads.size.should == 0
364
+ expect(job.threads.size).to eq(0)
301
365
  end
302
366
  end
303
367
 
@@ -314,7 +378,7 @@ describe Rufus::Scheduler::Job do
314
378
 
315
379
  sleep 3
316
380
 
317
- job.threads.size.should > 1
381
+ expect(job.threads.size).to be > 1
318
382
  end
319
383
  end
320
384
 
@@ -329,7 +393,7 @@ describe Rufus::Scheduler::Job do
329
393
 
330
394
  sleep 3
331
395
 
332
- job.threads.size.should == 1
396
+ expect(job.threads.size).to eq(1)
333
397
  end
334
398
  end
335
399
  end
@@ -352,14 +416,14 @@ describe Rufus::Scheduler::Job do
352
416
  sleep 0.7
353
417
 
354
418
  if j0.threads.any?
355
- j0.threads.size.should == 1
356
- j1.threads.size.should == 0
419
+ expect(j0.threads.size).to eq(1)
420
+ expect(j1.threads.size).to eq(0)
357
421
  else
358
- j0.threads.size.should == 0
359
- j1.threads.size.should == 1
422
+ expect(j0.threads.size).to eq(0)
423
+ expect(j1.threads.size).to eq(1)
360
424
  end
361
425
 
362
- @scheduler.mutexes.keys.should == %w[ vladivostok ]
426
+ expect(@scheduler.mutexes.keys).to eq(%w[ vladivostok ])
363
427
  end
364
428
  end
365
429
 
@@ -375,14 +439,14 @@ describe Rufus::Scheduler::Job do
375
439
  sleep 0.7
376
440
 
377
441
  if j0.threads.any?
378
- j0.threads.size.should == 1
379
- j1.threads.size.should == 0
442
+ expect(j0.threads.size).to eq(1)
443
+ expect(j1.threads.size).to eq(0)
380
444
  else
381
- j0.threads.size.should == 0
382
- j1.threads.size.should == 1
445
+ expect(j0.threads.size).to eq(0)
446
+ expect(j1.threads.size).to eq(1)
383
447
  end
384
448
 
385
- @scheduler.mutexes.keys.should == []
449
+ expect(@scheduler.mutexes.keys).to eq([])
386
450
  end
387
451
  end
388
452
 
@@ -402,14 +466,14 @@ describe Rufus::Scheduler::Job do
402
466
  sleep 0.7
403
467
 
404
468
  if j0.threads.any?
405
- j0.threads.size.should == 1
406
- j1.threads.size.should == 0
469
+ expect(j0.threads.size).to eq(1)
470
+ expect(j1.threads.size).to eq(0)
407
471
  else
408
- j0.threads.size.should == 0
409
- j1.threads.size.should == 1
472
+ expect(j0.threads.size).to eq(0)
473
+ expect(j1.threads.size).to eq(1)
410
474
  end
411
475
 
412
- @scheduler.mutexes.keys.sort.should == %w[ a b ]
476
+ expect(@scheduler.mutexes.keys.sort).to eq(%w[ a b ])
413
477
  end
414
478
  end
415
479
  end
@@ -434,8 +498,8 @@ describe Rufus::Scheduler::Job do
434
498
 
435
499
  sleep(3)
436
500
 
437
- counter.should == 1
438
- toe.class.should == Rufus::Scheduler::TimeoutError
501
+ expect(counter).to eq(1)
502
+ expect(toe.class).to eq(Rufus::Scheduler::TimeoutError)
439
503
  end
440
504
 
441
505
  it 'interrupts the job it is stashed to (point in time)' do
@@ -454,7 +518,7 @@ describe Rufus::Scheduler::Job do
454
518
 
455
519
  sleep(3)
456
520
 
457
- counter.should == 1
521
+ expect(counter).to eq(1)
458
522
  end
459
523
 
460
524
  it 'starts timing when the job enters successfully all its mutexes' do
@@ -478,11 +542,11 @@ describe Rufus::Scheduler::Job do
478
542
 
479
543
  sleep 3
480
544
 
481
- t0.should <= t1
545
+ expect(t0).to be <= t1
482
546
 
483
547
  d = t2 - t1
484
- d.should >= 1.0
485
- d.should < 1.5
548
+ expect(d).to be >= 1.0
549
+ expect(d).to be < 1.5
486
550
  end
487
551
 
488
552
  it 'emits the timeout information to $stderr (default #on_error)' do
@@ -493,7 +557,7 @@ describe Rufus::Scheduler::Job do
493
557
 
494
558
  sleep 2
495
559
 
496
- $stderr.string.should match(/Rufus::Scheduler::TimeoutError/)
560
+ expect($stderr.string).to match(/Rufus::Scheduler::TimeoutError/)
497
561
  end
498
562
 
499
563
  it 'does not prevent a repeat job from recurring' do
@@ -507,7 +571,60 @@ describe Rufus::Scheduler::Job do
507
571
 
508
572
  sleep 3
509
573
 
510
- counter.should > 1
574
+ expect(counter).to be > 1
575
+ end
576
+ end
577
+
578
+ context 'work time' do
579
+
580
+ describe '#last_work_time' do
581
+
582
+ it 'starts at 0' do
583
+
584
+ job = @scheduler.schedule_every '5m' do; end
585
+
586
+ expect(job.last_work_time).to eq(0.0)
587
+ end
588
+
589
+ it 'keeps track of how long the work was upon last trigger' do
590
+
591
+ job =
592
+ @scheduler.schedule_in '0.5s' do
593
+ sleep 0.7
594
+ end
595
+
596
+ sleep 2
597
+
598
+ expect(job.last_work_time).to be >= 0.7
599
+ expect(job.last_work_time).to be < 0.8
600
+ end
601
+ end
602
+
603
+ describe '#mean_work_time' do
604
+
605
+ it 'starts at 0' do
606
+
607
+ job = @scheduler.schedule_every '5m' do; end
608
+
609
+ expect(job.mean_work_time).to eq(0.0)
610
+ end
611
+
612
+ it 'gathers work times and computes the mean' do
613
+
614
+ job =
615
+ @scheduler.schedule_every '0.5s' do |j|
616
+ #p j.last_work_time
617
+ #p j.mean_work_time
618
+ sleep 0.01 * (j.count + 1)
619
+ end
620
+
621
+ sleep 4.6
622
+
623
+ expect(job.last_work_time).to be >= 0.08
624
+ expect(job.last_work_time).to be < 0.099
625
+ expect(job.mean_work_time).to be > 0.05
626
+ expect(job.mean_work_time).to be < 0.06
627
+ end
511
628
  end
512
629
  end
513
630
  end
@@ -0,0 +1,47 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Fri Nov 1 05:56:03 JST 2013
6
+ #
7
+ # Ishinomaki
8
+ #
9
+
10
+ require 'spec_helper'
11
+
12
+
13
+ describe Rufus::Scheduler do
14
+
15
+ class LosingLockScheduler < Rufus::Scheduler
16
+
17
+ attr_reader :counter
18
+
19
+ def initialize
20
+ super
21
+ @counter = 0
22
+ end
23
+
24
+ def confirm_lock
25
+ @counter = @counter + 1
26
+ false
27
+ end
28
+ end
29
+
30
+ context 'custom locks' do
31
+
32
+ it 'does not trigger when #confirm_lock returns false' do
33
+
34
+ s = LosingLockScheduler.new
35
+
36
+ count = 0
37
+
38
+ s.in('0s') { count = count + 1 }
39
+
40
+ sleep 0.7
41
+
42
+ expect(count).to eq(0)
43
+ expect(s.counter).to eq(1)
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,47 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Sat Aug 16 05:43:06 JST 2014
6
+ # added by @ecin
7
+ #
8
+
9
+ require 'spec_helper'
10
+
11
+
12
+ describe Rufus::Scheduler::FileLock do
13
+
14
+ before :each do
15
+
16
+ @lock_path = '.rufus-scheduler.lock'
17
+ @lock = Rufus::Scheduler::FileLock.new(@lock_path)
18
+ end
19
+
20
+ after :each do
21
+
22
+ FileUtils.rm_f(@lock_path)
23
+ FileUtils.rm_f('lock.txt')
24
+ end
25
+
26
+ context ':scheduler_lock => Rufus::Scheduler::FileLock.new(path)' do
27
+
28
+ it 'writes down a .rufus-scheduler.lock file' do
29
+
30
+ @lock.lock
31
+
32
+ line = File.read(@lock_path)
33
+
34
+ expect(line).to match(/pid: #{$$}/)
35
+ end
36
+
37
+ it '"flocks" the lock file' do
38
+
39
+ @lock.lock
40
+
41
+ f = File.new(@lock_path, 'a')
42
+
43
+ expect(f.flock(File::LOCK_NB | File::LOCK_EX)).to eq(false)
44
+ end
45
+ end
46
+ end
47
+
@@ -25,7 +25,7 @@ describe Rufus::Scheduler do
25
25
  line = File.read('.rufus-scheduler.lock')
26
26
 
27
27
  #p line
28
- line.should match(/pid: #{$$}/)
28
+ expect(line).to match(/pid: #{$$}/)
29
29
  end
30
30
 
31
31
  it '"flocks" the lock file' do
@@ -34,7 +34,7 @@ describe Rufus::Scheduler do
34
34
 
35
35
  f = File.new('.rufus-scheduler.lock', 'a')
36
36
 
37
- f.flock(File::LOCK_NB | File::LOCK_EX).should == false
37
+ expect(f.flock(File::LOCK_NB | File::LOCK_EX)).to eq(false)
38
38
  end
39
39
 
40
40
  it 'prevents newer schedulers from starting' do
@@ -42,8 +42,8 @@ describe Rufus::Scheduler do
42
42
  s0 = Rufus::Scheduler.new :lockfile => '.rufus-scheduler.lock'
43
43
  s1 = Rufus::Scheduler.new :lockfile => '.rufus-scheduler.lock'
44
44
 
45
- s0.started_at.should_not == nil
46
- s1.started_at.should == nil
45
+ expect(s0.started_at).not_to eq(nil)
46
+ expect(s1.started_at).to eq(nil)
47
47
  end
48
48
 
49
49
  it 'releases the lockfile when shutting down' do
@@ -54,7 +54,7 @@ describe Rufus::Scheduler do
54
54
 
55
55
  f = File.new('.rufus-scheduler.lock', 'a')
56
56
 
57
- f.flock(File::LOCK_NB | File::LOCK_EX).should == 0
57
+ expect(f.flock(File::LOCK_NB | File::LOCK_EX)).to eq(0)
58
58
  end
59
59
  end
60
60
  end
data/spec/lock_spec.rb ADDED
@@ -0,0 +1,59 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Sat Aug 16 05:42:11 JST 2014
6
+ # added by @ecin
7
+ #
8
+
9
+ require 'spec_helper'
10
+
11
+
12
+ describe Rufus::Scheduler do
13
+
14
+ context "when running multiple schedulers side-by-side" do
15
+
16
+ class AlwaysLock
17
+ def lock; true; end
18
+ def unlock; true; end
19
+ def locked?; true; end
20
+ end
21
+
22
+ class NeverLock
23
+ def lock; false; end
24
+ def unlock; true; end
25
+ def locked?; true; end
26
+ end
27
+
28
+ it "only starts if it can acquire a scheduler lock" do
29
+
30
+ main = Rufus::Scheduler.new :scheduler_lock => AlwaysLock.new
31
+ backup = Rufus::Scheduler.new :scheduler_lock => NeverLock.new
32
+
33
+ expect(main).to be_up
34
+ expect(backup).to be_down
35
+ end
36
+
37
+ it "only triggers jobs when it can acquire a trigger lock" do
38
+
39
+ main = Rufus::Scheduler.new(:trigger_lock => AlwaysLock.new)
40
+ backup = Rufus::Scheduler.new(:trigger_lock => NeverLock.new)
41
+
42
+ expect(main).to be_up
43
+ expect(backup).to be_up
44
+
45
+ counter = 0
46
+ job = proc { counter += 1 }
47
+ main.schedule_in(0, job)
48
+ backup.schedule_in(0, job)
49
+
50
+ sleep 0.5
51
+
52
+ expect(main.jobs).to be_empty
53
+ expect(backup.jobs.count).to eq(1)
54
+ expect(backup.jobs.first.next_time).to be(false)
55
+ expect(counter).to eq(1)
56
+ end
57
+ end
58
+ end
59
+