rufus-scheduler 3.1.4 → 3.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +410 -0
  3. data/CREDITS.md +142 -0
  4. data/LICENSE.txt +1 -1
  5. data/Makefile +27 -0
  6. data/README.md +407 -143
  7. data/lib/rufus/scheduler/job_array.rb +36 -66
  8. data/lib/rufus/scheduler/jobs_core.rb +369 -0
  9. data/lib/rufus/scheduler/jobs_one_time.rb +53 -0
  10. data/lib/rufus/scheduler/jobs_repeat.rb +335 -0
  11. data/lib/rufus/scheduler/locks.rb +41 -67
  12. data/lib/rufus/scheduler/util.rb +89 -179
  13. data/lib/rufus/scheduler.rb +545 -453
  14. data/rufus-scheduler.gemspec +22 -11
  15. metadata +44 -85
  16. data/CHANGELOG.txt +0 -243
  17. data/CREDITS.txt +0 -88
  18. data/Rakefile +0 -83
  19. data/TODO.txt +0 -151
  20. data/lib/rufus/scheduler/cronline.rb +0 -470
  21. data/lib/rufus/scheduler/jobs.rb +0 -633
  22. data/lib/rufus/scheduler/zones.rb +0 -174
  23. data/lib/rufus/scheduler/zotime.rb +0 -155
  24. data/spec/basics_spec.rb +0 -54
  25. data/spec/cronline_spec.rb +0 -915
  26. data/spec/error_spec.rb +0 -139
  27. data/spec/job_array_spec.rb +0 -39
  28. data/spec/job_at_spec.rb +0 -58
  29. data/spec/job_cron_spec.rb +0 -128
  30. data/spec/job_every_spec.rb +0 -104
  31. data/spec/job_in_spec.rb +0 -20
  32. data/spec/job_interval_spec.rb +0 -68
  33. data/spec/job_repeat_spec.rb +0 -357
  34. data/spec/job_spec.rb +0 -631
  35. data/spec/lock_custom_spec.rb +0 -47
  36. data/spec/lock_flock_spec.rb +0 -47
  37. data/spec/lock_lockfile_spec.rb +0 -61
  38. data/spec/lock_spec.rb +0 -59
  39. data/spec/parse_spec.rb +0 -263
  40. data/spec/schedule_at_spec.rb +0 -158
  41. data/spec/schedule_cron_spec.rb +0 -66
  42. data/spec/schedule_every_spec.rb +0 -109
  43. data/spec/schedule_in_spec.rb +0 -80
  44. data/spec/schedule_interval_spec.rb +0 -128
  45. data/spec/scheduler_spec.rb +0 -1067
  46. data/spec/spec_helper.rb +0 -126
  47. data/spec/threads_spec.rb +0 -96
  48. data/spec/zotime_spec.rb +0 -396
@@ -1,1067 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-scheduler
4
- #
5
- # Wed Apr 17 06:00:59 JST 2013
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Rufus::Scheduler do
12
-
13
- describe '#initialize' do
14
-
15
- it 'starts the scheduler thread' do
16
-
17
- scheduler = Rufus::Scheduler.new
18
-
19
- t = Thread.list.find { |t|
20
- t[:name] == "rufus_scheduler_#{scheduler.object_id}_scheduler"
21
- }
22
-
23
- expect(t[:rufus_scheduler]).to eq(scheduler)
24
- end
25
-
26
- it 'sets :name and :rufus_scheduler in the scheduler thread local vars' do
27
-
28
- scheduler = Rufus::Scheduler.new
29
-
30
- expect(scheduler.thread[:name]).to eq(
31
- "rufus_scheduler_#{scheduler.object_id}_scheduler"
32
- )
33
- expect(scheduler.thread[:rufus_scheduler]).to eq(
34
- scheduler
35
- )
36
- end
37
-
38
- it 'accepts a :frequency => integer option' do
39
-
40
- scheduler = Rufus::Scheduler.new(:frequency => 2)
41
-
42
- expect(scheduler.frequency).to eq(2)
43
- end
44
-
45
- it 'accepts a :frequency => "2h1m" option' do
46
-
47
- scheduler = Rufus::Scheduler.new(:frequency => '2h1m')
48
-
49
- expect(scheduler.frequency).to eq(3600 * 2 + 60)
50
- end
51
-
52
- it 'accepts a :thread_name option' do
53
-
54
- scheduler = Rufus::Scheduler.new(:thread_name => 'oliphant')
55
-
56
- t = Thread.list.find { |t| t[:name] == 'oliphant' }
57
-
58
- expect(t[:rufus_scheduler]).to eq(scheduler)
59
- end
60
-
61
- #it 'accepts a :min_work_threads option' do
62
- # scheduler = Rufus::Scheduler.new(:min_work_threads => 9)
63
- # scheduler.min_work_threads.should == 9
64
- #end
65
-
66
- it 'accepts a :max_work_threads option' do
67
-
68
- scheduler = Rufus::Scheduler.new(:max_work_threads => 9)
69
-
70
- expect(scheduler.max_work_threads).to eq(9)
71
- end
72
- end
73
-
74
- before :each do
75
- @scheduler = Rufus::Scheduler.new
76
- end
77
- after :each do
78
- @scheduler.shutdown
79
- end
80
-
81
- describe 'a schedule method' do
82
-
83
- it 'passes the job to its block when it triggers' do
84
-
85
- j = nil
86
- job = @scheduler.schedule_in('0s') { |jj| j = jj }
87
-
88
- sleep 0.4
89
-
90
- expect(j).to eq(job)
91
- end
92
-
93
- it 'passes the trigger time as second block argument' do
94
-
95
- t = nil
96
- @scheduler.schedule_in('0s') { |jj, tt| t = tt }
97
-
98
- sleep 0.4
99
-
100
- expect(t.class).to eq(Time)
101
- end
102
-
103
- class MyHandler
104
- attr_reader :counter
105
- def initialize
106
- @counter = 0
107
- end
108
- def call(job, time)
109
- @counter = @counter + 1
110
- end
111
- end
112
-
113
- it 'accepts a callable object instead of a block' do
114
-
115
- mh = MyHandler.new
116
-
117
- @scheduler.schedule_in('0s', mh)
118
-
119
- sleep 0.4
120
-
121
- expect(mh.counter).to eq(1)
122
- end
123
-
124
- class MyOtherHandler
125
- attr_reader :counter
126
- def initialize
127
- @counter = 0
128
- end
129
- def call
130
- @counter = @counter + 1
131
- end
132
- end
133
-
134
- it 'accepts a callable obj instead of a block (#call with no args)' do
135
-
136
- job = @scheduler.schedule_in('0s', MyOtherHandler.new)
137
-
138
- sleep 0.4
139
-
140
- expect(job.handler.counter).to eq(1)
141
- end
142
-
143
- it 'accepts a class as callable' do
144
-
145
- job =
146
- @scheduler.schedule_in('0s', Class.new do
147
- attr_reader :value
148
- def call
149
- @value = 7
150
- end
151
- end)
152
-
153
- sleep 0.4
154
-
155
- expect(job.handler.value).to eq(7)
156
- end
157
-
158
- it 'raises if the scheduler is shutting down' do
159
-
160
- @scheduler.shutdown
161
-
162
- expect {
163
- @scheduler.in('0s') { puts 'hhhhhhhhhhhello!!' }
164
- }.to raise_error(Rufus::Scheduler::NotRunningError)
165
- end
166
- end
167
-
168
- describe '#in / #at' do
169
-
170
- # scheduler.in(2.hours.from_now) { ... }
171
-
172
- it 'accepts point in time and duration indifferently (#in)' do
173
-
174
- seen = false
175
-
176
- t = Time.now + 1
177
-
178
- @scheduler.in(t) { seen = true }
179
-
180
- sleep 0.1 while seen != true
181
- end
182
-
183
- it 'accepts point in time and duration indifferently (#at)' do
184
-
185
- seen = false
186
-
187
- t = 1
188
-
189
- @scheduler.at(t) { seen = true }
190
-
191
- sleep 0.1 while seen != true
192
- end
193
- end
194
-
195
- describe '#schedule' do
196
-
197
- it 'accepts a duration and schedules an InJob' do
198
-
199
- j = @scheduler.schedule '1s' do; end
200
-
201
- expect(j.class).to eq(Rufus::Scheduler::InJob)
202
- expect(j.original).to eq('1s')
203
- end
204
-
205
- it 'accepts a point in time and schedules an AtJob' do
206
-
207
- j = @scheduler.schedule '2070/12/24 23:00' do; end
208
-
209
- expect(j.class).to eq(Rufus::Scheduler::AtJob)
210
- expect(j.next_time.strftime('%Y %m %d')).to eq('2070 12 24')
211
- end
212
-
213
- it 'accepts a cron string and schedules a CronJob' do
214
-
215
- j = @scheduler.schedule '* * * * *' do; end
216
-
217
- expect(j.class).to eq(Rufus::Scheduler::CronJob)
218
- end
219
- end
220
-
221
- describe '#repeat' do
222
-
223
- it 'accepts a duration and schedules an EveryJob' do
224
-
225
- j = @scheduler.repeat '1s' do; end
226
-
227
- expect(j.class).to eq(Rufus::Scheduler::EveryJob)
228
- end
229
-
230
- it 'accepts a cron string and schedules a CronJob' do
231
-
232
- j = @scheduler.repeat '* * * * *' do; end
233
-
234
- expect(j.class).to eq(Rufus::Scheduler::CronJob)
235
- end
236
- end
237
-
238
- describe '#unschedule(job_or_work_id)' do
239
-
240
- it 'accepts job ids' do
241
-
242
- job = @scheduler.schedule_in '10d' do; end
243
-
244
- expect(job.unscheduled_at).to eq(nil)
245
-
246
- @scheduler.unschedule(job.id)
247
-
248
- expect(job.unscheduled_at).not_to eq(nil)
249
- end
250
-
251
- it 'accepts jobs' do
252
-
253
- job = @scheduler.schedule_in '10d' do; end
254
-
255
- expect(job.unscheduled_at).to eq(nil)
256
-
257
- @scheduler.unschedule(job)
258
-
259
- expect(job.unscheduled_at).not_to eq(nil)
260
- end
261
-
262
- it 'carefully unschedules repeat jobs' do
263
-
264
- counter = 0
265
-
266
- job =
267
- @scheduler.schedule_every '0.5s' do
268
- counter = counter + 1
269
- end
270
-
271
- sleep 1.5
272
- c = counter
273
-
274
- @scheduler.unschedule(job)
275
-
276
- sleep 1.5
277
- expect(counter).to eq(c)
278
- end
279
- end
280
-
281
- describe '#uptime' do
282
-
283
- it 'returns the uptime as a float' do
284
-
285
- expect(@scheduler.uptime).to be >= 0.0
286
- end
287
- end
288
-
289
- describe '#uptime_s' do
290
-
291
- it 'returns the uptime as a human readable string' do
292
-
293
- sleep 1
294
-
295
- expect(@scheduler.uptime_s).to match(/^[12]s\d+$/)
296
- end
297
- end
298
-
299
- describe '#join' do
300
-
301
- it 'joins the scheduler thread' do
302
-
303
- t = Thread.new { @scheduler.join; Thread.current['a'] = 'over' }
304
-
305
- expect(t['a']).to eq(nil)
306
-
307
- @scheduler.shutdown
308
-
309
- sleep(1)
310
-
311
- expect(t['a']).to eq('over')
312
- end
313
- end
314
-
315
- describe '#job(job_id)' do
316
-
317
- it 'returns nil if there is no corresponding Job instance' do
318
-
319
- expect(@scheduler.job('nada')).to eq(nil)
320
- end
321
-
322
- it 'returns the corresponding Job instance' do
323
-
324
- job_id = @scheduler.in '10d' do; end
325
-
326
- sleep(1) # give it some time to get scheduled
327
-
328
- expect(@scheduler.job(job_id).job_id).to eq(job_id)
329
- end
330
- end
331
-
332
- # describe '#find_by_tag(t)' do
333
- #
334
- # it 'returns an empty list when there are no jobs with the given tag' do
335
- #
336
- # @scheduler.find_by_tag('nada').should == []
337
- # end
338
- #
339
- # it 'returns all the jobs with the given tag' do
340
- #
341
- # @scheduler.in '10d', :tag => 't0' do; end
342
- # @scheduler.every '2h', :tag => %w[ t0 t1 ] do; end
343
- # @scheduler.every '3h' do; end
344
- #
345
- # @scheduler.find_by_tag('t0').map(&:original).should ==
346
- # %w[ 2h 10d ]
347
- # @scheduler.find_by_tag('t1').map(&:original).should ==
348
- # %w[ 2h ]
349
- # @scheduler.find_by_tag('t1', 't0').map(&:original).sort.should ==
350
- # %w[ 2h ]
351
- # end
352
- # end
353
-
354
- describe '#threads' do
355
-
356
- it 'just lists the main thread (scheduler thread) when no job is scheduled' do
357
-
358
- expect(@scheduler.threads).to eq([ @scheduler.thread ])
359
- end
360
-
361
- it 'lists all the threads a scheduler uses' do
362
-
363
- @scheduler.in '0s' do
364
- sleep(2)
365
- end
366
-
367
- sleep 0.4
368
-
369
- expect(@scheduler.threads.size).to eq(2)
370
- end
371
- end
372
-
373
- describe '#work_threads(:all | :vacant)' do
374
-
375
- it 'returns an empty array when the scheduler has not yet done anything' do
376
-
377
- expect(@scheduler.work_threads).to eq([])
378
- expect(@scheduler.work_threads(:all)).to eq([])
379
- expect(@scheduler.work_threads(:vacant)).to eq([])
380
- end
381
-
382
- it 'lists the [vacant] work threads in the pool' do
383
-
384
- @scheduler.in '0s' do
385
- sleep(0.2)
386
- end
387
- @scheduler.in '0s' do
388
- sleep(2.0)
389
- end
390
-
391
- sleep 0.7
392
-
393
- if @scheduler.work_threads.size == 1
394
- expect(@scheduler.work_threads.size).to eq(1)
395
- expect(@scheduler.work_threads(:all).size).to eq(1)
396
- expect(@scheduler.work_threads(:vacant).size).to eq(0)
397
- else
398
- expect(@scheduler.work_threads.size).to eq(2)
399
- expect(@scheduler.work_threads(:all).size).to eq(2)
400
- expect(@scheduler.work_threads(:vacant).size).to eq(1)
401
- end
402
- end
403
- end
404
-
405
- describe '#work_threads(:active)' do
406
-
407
- it 'returns [] when there are no jobs running' do
408
-
409
- expect(@scheduler.work_threads(:active)).to eq([])
410
- end
411
-
412
- it 'returns the list of threads of the running jobs' do
413
-
414
- job =
415
- @scheduler.schedule_in('0s') do
416
- sleep 1
417
- end
418
-
419
- sleep 0.4
420
-
421
- expect(@scheduler.work_threads(:active).size).to eq(1)
422
-
423
- t = @scheduler.work_threads(:active).first
424
-
425
- expect(t.class).to eq(Thread)
426
- expect(t[@scheduler.thread_key]).to eq(true)
427
- expect(t[:rufus_scheduler_job]).to eq(job)
428
- expect(t[:rufus_scheduler_time]).not_to eq(nil)
429
- end
430
-
431
- it 'does not return threads from other schedulers' do
432
-
433
- scheduler = Rufus::Scheduler.new
434
-
435
- job =
436
- @scheduler.schedule_in('0s') do
437
- sleep(1)
438
- end
439
-
440
- sleep 0.4
441
-
442
- expect(scheduler.work_threads(:active)).to eq([])
443
-
444
- scheduler.shutdown
445
- end
446
- end
447
-
448
- #describe '#min_work_threads' do
449
- # it 'returns the min job thread count' do
450
- # @scheduler.min_work_threads.should == 3
451
- # end
452
- #end
453
- #describe '#min_work_threads=' do
454
- # it 'sets the min job thread count' do
455
- # @scheduler.min_work_threads = 1
456
- # @scheduler.min_work_threads.should == 1
457
- # end
458
- #end
459
-
460
- describe '#max_work_threads' do
461
-
462
- it 'returns the max job thread count' do
463
-
464
- expect(@scheduler.max_work_threads).to eq(28)
465
- end
466
- end
467
-
468
- describe '#max_work_threads=' do
469
-
470
- it 'sets the max job thread count' do
471
-
472
- @scheduler.max_work_threads = 14
473
-
474
- expect(@scheduler.max_work_threads).to eq(14)
475
- end
476
- end
477
-
478
- #describe '#kill_all_work_threads' do
479
- #
480
- # it 'kills all the work threads' do
481
- #
482
- # @scheduler.in '0s' do; sleep(5); end
483
- # @scheduler.in '0s' do; sleep(5); end
484
- # @scheduler.in '0s' do; sleep(5); end
485
- #
486
- # sleep 0.5
487
- #
488
- # @scheduler.work_threads.size.should == 3
489
- #
490
- # @scheduler.send(:kill_all_work_threads)
491
- #
492
- # sleep 0.5
493
- #
494
- # @scheduler.work_threads.size.should == 0
495
- # end
496
- #end
497
-
498
- describe '#running_jobs' do
499
-
500
- it 'returns [] when there are no running jobs' do
501
-
502
- expect(@scheduler.running_jobs).to eq([])
503
- end
504
-
505
- it 'returns a list of running Job instances' do
506
-
507
- job =
508
- @scheduler.schedule_in('0s') do
509
- sleep(1)
510
- end
511
-
512
- sleep 0.4
513
-
514
- expect(job.running?).to eq(true)
515
- expect(@scheduler.running_jobs).to eq([ job ])
516
- end
517
-
518
- it 'does not return twice the same job' do
519
-
520
- job =
521
- @scheduler.schedule_every('0.3s') do
522
- sleep(5)
523
- end
524
-
525
- sleep 1.5
526
-
527
- expect(job.running?).to eq(true)
528
- expect(@scheduler.running_jobs).to eq([ job ])
529
- end
530
- end
531
-
532
- describe '#running_jobs(:tag/:tags => x)' do
533
-
534
- it 'returns a list of running jobs filtered by tag' do
535
-
536
- @scheduler.in '0.1s', :tag => 't0' do
537
- sleep 3
538
- end
539
- @scheduler.in '0.2s', :tag => 't1' do
540
- sleep 3
541
- end
542
-
543
- sleep 0.49
544
-
545
- expect(@scheduler.running_jobs(:tag => 't0').map(&:original)).to eq(
546
- %w[ 0.1s ]
547
- )
548
- expect(@scheduler.running_jobs(:tag => 't1').map(&:original)).to eq(
549
- %w[ 0.2s ]
550
- )
551
- expect(@scheduler.running_jobs(:tags => %w[ t0 t1 ]).map(&:original)).to eq(
552
- []
553
- )
554
- end
555
- end
556
-
557
- describe '#occurrences(time0, time1)' do
558
-
559
- it 'returns a { job => [ times ] } of job occurrences' do
560
-
561
- j0 = @scheduler.schedule_in '7m' do; end
562
- j1 = @scheduler.schedule_at '10m' do; end
563
- j2 = @scheduler.schedule_every '5m' do; end
564
- j3 = @scheduler.schedule_interval '5m' do; end
565
- j4 = @scheduler.schedule_cron '* * * * *' do; end
566
-
567
- h = @scheduler.occurrences(Time.now + 4 * 60, Time.now + 11 * 60)
568
-
569
- expect(h.size).to eq(5)
570
- expect(h[j0]).to eq([ j0.next_time ])
571
- expect(h[j1]).to eq([ j1.next_time ])
572
- expect(h[j2].size).to eq(2)
573
- expect(h[j3].size).to eq(2)
574
- expect(h[j4].size).to eq(7)
575
- end
576
-
577
- it 'returns a [ [ time, job ], ... ] of job occurrences when :timeline' do
578
-
579
- j0 = @scheduler.schedule_in '5m' do; end
580
- j1 = @scheduler.schedule_in '10m' do; end
581
-
582
- a =
583
- @scheduler.occurrences(Time.now + 4 * 60, Time.now + 11 * 60, :timeline)
584
-
585
- expect(a[0][0]).to be_within_1s_of(Time.now + 5 * 60)
586
- expect(a[0][1]).to eq(j0)
587
- expect(a[1][0]).to be_within_1s_of(Time.now + 10 * 60)
588
- expect(a[1][1]).to eq(j1)
589
- end
590
-
591
- it 'respects :first_at for repeat jobs' do
592
-
593
- j0 = @scheduler.schedule_every '5m', :first_in => '10m' do; end
594
-
595
- h = @scheduler.occurrences(Time.now + 4 * 60, Time.now + 16 * 60)
596
-
597
- expect(h[j0][0]).to be_within_1s_of(Time.now + 10 * 60)
598
- expect(h[j0][1]).to be_within_1s_of(Time.now + 15 * 60)
599
- end
600
-
601
- it 'respects :times for repeat jobs' do
602
-
603
- j0 = @scheduler.schedule_every '1m', :times => 10 do; end
604
-
605
- t0 = Time.parse((Time.now + 5 * 60).strftime('%Y-%m-%d %H:%M:01'))
606
- t1 = t0 + 12 * 60 - 1
607
-
608
- h = @scheduler.occurrences(t0, t1)
609
-
610
- expect(h[j0].size).to eq(6)
611
- end
612
- end
613
-
614
- describe '#timeline' do
615
-
616
- it 'returns a [ [ time, job ], ... ] of job occurrences' do
617
-
618
- j0 = @scheduler.schedule_in '5m' do; end
619
- j1 = @scheduler.schedule_in '10m' do; end
620
-
621
- a = @scheduler.timeline(Time.now + 4 * 60, Time.now + 11 * 60)
622
-
623
- expect(a[0][0]).to be_within_1s_of(Time.now + 5 * 60)
624
- expect(a[0][1]).to eq(j0)
625
- expect(a[1][0]).to be_within_1s_of(Time.now + 10 * 60)
626
- expect(a[1][1]).to eq(j1)
627
- end
628
-
629
- it 'should not lock when running timeline with a :first_at specified' do
630
-
631
- now = Time.now
632
-
633
- # scheduling a cron job with a first_at and running #timeline used
634
- # to result in an infinite loop.
635
-
636
- @scheduler.cron('* * * * * *', :first_at => now + 3) {}
637
-
638
- jobs = @scheduler.timeline(now, now + 4)
639
- expect(jobs.size).to be 2
640
- expect(jobs[0][0]).to be_within_1s_of now + 3
641
- expect(jobs[1][0]).to be_within_1s_of now + 4
642
- end
643
- end
644
-
645
- #--
646
- # management methods
647
- #++
648
-
649
- describe '#shutdown' do
650
-
651
- it 'blanks the uptime' do
652
-
653
- @scheduler.shutdown
654
-
655
- expect(@scheduler.uptime).to eq(nil)
656
- end
657
-
658
- it 'shuts the scheduler down' do
659
-
660
- @scheduler.shutdown
661
-
662
- sleep 0.100
663
- sleep 0.400 if RUBY_VERSION < '1.9.0'
664
-
665
- t = Thread.list.find { |t|
666
- t[:name] == "rufus_scheduler_#{@scheduler.object_id}"
667
- }
668
-
669
- expect(t).to eq(nil)
670
- end
671
-
672
- it 'has a #stop alias' do
673
-
674
- @scheduler.stop
675
-
676
- expect(@scheduler.uptime).to eq(nil)
677
- end
678
-
679
- #it 'has a #close alias'
680
- end
681
-
682
- describe '#shutdown(:wait)' do
683
-
684
- it 'shuts down and blocks until all the jobs ended their current runs' do
685
-
686
- counter = 0
687
-
688
- @scheduler.in '0s' do
689
- sleep 1
690
- counter = counter + 1
691
- end
692
-
693
- sleep 0.4
694
-
695
- @scheduler.shutdown(:wait)
696
-
697
- expect(counter).to eq(1)
698
- expect(@scheduler.uptime).to eq(nil)
699
- expect(@scheduler.running_jobs).to eq([])
700
- expect(@scheduler.threads).to eq([])
701
- end
702
- end
703
-
704
- describe '#shutdown(:kill)' do
705
-
706
- it 'kills all the jobs and then shuts down' do
707
-
708
- counter = 0
709
-
710
- @scheduler.in '0s' do
711
- sleep 1
712
- counter = counter + 1
713
- end
714
- @scheduler.at Time.now + 0.3 do
715
- sleep 1
716
- counter = counter + 1
717
- end
718
-
719
- sleep 0.4
720
-
721
- @scheduler.shutdown(:kill)
722
-
723
- sleep 1.4
724
-
725
- expect(counter).to eq(0)
726
- expect(@scheduler.uptime).to eq(nil)
727
- expect(@scheduler.running_jobs).to eq([])
728
- expect(@scheduler.threads).to eq([])
729
- end
730
- end
731
-
732
- describe '#pause' do
733
-
734
- it 'pauses the scheduler' do
735
-
736
- job = @scheduler.schedule_in '1s' do; end
737
-
738
- @scheduler.pause
739
-
740
- sleep(3)
741
-
742
- expect(job.last_time).to eq(nil)
743
- end
744
- end
745
-
746
- describe '#resume' do
747
-
748
- it 'works' do
749
-
750
- job = @scheduler.schedule_in '2s' do; end
751
-
752
- @scheduler.pause
753
- sleep(1)
754
- @scheduler.resume
755
- sleep(2)
756
-
757
- expect(job.last_time).not_to eq(nil)
758
- end
759
- end
760
-
761
- describe '#paused?' do
762
-
763
- it 'returns true if the scheduler is paused' do
764
-
765
- @scheduler.pause
766
- expect(@scheduler.paused?).to eq(true)
767
- end
768
-
769
- it 'returns false if the scheduler is not paused' do
770
-
771
- expect(@scheduler.paused?).to eq(false)
772
-
773
- @scheduler.pause
774
- @scheduler.resume
775
-
776
- expect(@scheduler.paused?).to eq(false)
777
- end
778
- end
779
-
780
- describe '#down?' do
781
-
782
- it 'returns true when the scheduler is down' do
783
-
784
- @scheduler.shutdown
785
-
786
- expect(@scheduler.down?).to eq(true)
787
- end
788
-
789
- it 'returns false when the scheduler is up' do
790
-
791
- expect(@scheduler.down?).to eq(false)
792
- end
793
- end
794
-
795
- describe '#up?' do
796
-
797
- it 'returns true when the scheduler is up' do
798
-
799
- expect(@scheduler.up?).to eq(true)
800
- end
801
- end
802
-
803
- #--
804
- # job methods
805
- #++
806
-
807
- describe '#jobs' do
808
-
809
- it 'is empty at the beginning' do
810
-
811
- expect(@scheduler.jobs).to eq([])
812
- end
813
-
814
- it 'returns the list of scheduled jobs' do
815
-
816
- @scheduler.in '10d' do; end
817
- @scheduler.in '1w' do; end
818
-
819
- sleep(1)
820
-
821
- jobs = @scheduler.jobs
822
-
823
- expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 10d 1w ])
824
- end
825
-
826
- it 'returns all the jobs (even those pending reschedule)' do
827
-
828
- @scheduler.in '0s', :blocking => true do
829
- sleep 2
830
- end
831
-
832
- sleep 0.4
833
-
834
- expect(@scheduler.jobs.size).to eq(1)
835
- end
836
-
837
- it 'does not return unscheduled jobs' do
838
-
839
- job =
840
- @scheduler.schedule_in '0s', :blocking => true do
841
- sleep 2
842
- end
843
-
844
- sleep 0.4
845
-
846
- job.unschedule
847
-
848
- expect(@scheduler.jobs.size).to eq(0)
849
- end
850
- end
851
-
852
- describe '#jobs(:tag / :tags => x)' do
853
-
854
- it 'returns [] when there are no jobs with the corresponding tag' do
855
-
856
- expect(@scheduler.jobs(:tag => 'nada')).to eq([])
857
- expect(@scheduler.jobs(:tags => %w[ nada hello ])).to eq([])
858
- end
859
-
860
- it 'returns the jobs with the corresponding tag' do
861
-
862
- @scheduler.in '10d', :tag => 't0' do; end
863
- @scheduler.every '2h', :tag => %w[ t0 t1 ] do; end
864
- @scheduler.every '3h' do; end
865
-
866
- expect(@scheduler.jobs(:tags => 't0').map(&:original).sort).to eq(
867
- %w[ 10d 2h ]
868
- )
869
- expect(@scheduler.jobs(:tags => 't1').map(&:original).sort).to eq(
870
- %w[ 2h ]
871
- )
872
- expect(@scheduler.jobs(:tags => [ 't1', 't0' ]).map(&:original).sort).to eq(
873
- %w[ 2h ]
874
- )
875
- end
876
- end
877
-
878
- describe '#every_jobs' do
879
-
880
- it 'returns EveryJob instances' do
881
-
882
- @scheduler.at '2030/12/12 12:10:00' do; end
883
- @scheduler.in '10d' do; end
884
- @scheduler.every '5m' do; end
885
-
886
- jobs = @scheduler.every_jobs
887
-
888
- expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 5m ])
889
- end
890
- end
891
-
892
- describe '#at_jobs' do
893
-
894
- it 'returns AtJob instances' do
895
-
896
- @scheduler.at '2030/12/12 12:10:00' do; end
897
- @scheduler.in '10d' do; end
898
- @scheduler.every '5m' do; end
899
-
900
- jobs = @scheduler.at_jobs
901
-
902
- expect(jobs.collect { |j| j.original }.sort).to eq([ '2030/12/12 12:10:00' ])
903
- end
904
- end
905
-
906
- describe '#in_jobs' do
907
-
908
- it 'returns InJob instances' do
909
-
910
- @scheduler.at '2030/12/12 12:10:00' do; end
911
- @scheduler.in '10d' do; end
912
- @scheduler.every '5m' do; end
913
-
914
- jobs = @scheduler.in_jobs
915
-
916
- expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 10d ])
917
- end
918
- end
919
-
920
- describe '#cron_jobs' do
921
-
922
- it 'returns CronJob instances' do
923
-
924
- @scheduler.at '2030/12/12 12:10:00' do; end
925
- @scheduler.in '10d' do; end
926
- @scheduler.every '5m' do; end
927
- @scheduler.cron '* * * * *' do; end
928
-
929
- jobs = @scheduler.cron_jobs
930
-
931
- expect(jobs.collect { |j| j.original }.sort).to eq([ '* * * * *' ])
932
- end
933
- end
934
-
935
- describe '#interval_jobs' do
936
-
937
- it 'returns IntervalJob instances' do
938
-
939
- @scheduler.at '2030/12/12 12:10:00' do; end
940
- @scheduler.in '10d' do; end
941
- @scheduler.every '5m' do; end
942
- @scheduler.cron '* * * * *' do; end
943
- @scheduler.interval '7m' do; end
944
-
945
- jobs = @scheduler.interval_jobs
946
-
947
- expect(jobs.collect { |j| j.original }.sort).to eq(%w[ 7m ])
948
- end
949
- end
950
-
951
- #--
952
- # callbacks
953
- #++
954
-
955
- describe '#on_pre_trigger' do
956
-
957
- it 'is called right before a job triggers' do
958
-
959
- $out = []
960
-
961
- def @scheduler.on_pre_trigger(job)
962
- $out << "pre #{job.id}"
963
- end
964
-
965
- job_id =
966
- @scheduler.in '0.5s' do |job|
967
- $out << job.id
968
- end
969
-
970
- sleep 0.7
971
-
972
- expect($out).to eq([ "pre #{job_id}", job_id ])
973
- end
974
-
975
- it 'accepts the job and the triggerTime as argument' do
976
-
977
- $tt = nil
978
-
979
- def @scheduler.on_pre_trigger(job, trigger_time)
980
- $tt = trigger_time
981
- end
982
-
983
- start = Time.now
984
-
985
- @scheduler.in '0.5s' do; end
986
-
987
- sleep 0.7
988
-
989
- expect($tt.class).to eq(Time)
990
- expect($tt).to be > start
991
- expect($tt).to be < Time.now
992
- end
993
-
994
- context 'when it returns false' do
995
-
996
- it 'prevents the job from triggering' do
997
-
998
- $out = []
999
-
1000
- def @scheduler.on_pre_trigger(job)
1001
- $out << "pre #{job.id}"
1002
- false
1003
- end
1004
-
1005
- job_id =
1006
- @scheduler.in '0.5s' do |job|
1007
- $out << job.id
1008
- end
1009
-
1010
- sleep 0.7
1011
-
1012
- expect($out).to eq([ "pre #{job_id}" ])
1013
- end
1014
- end
1015
- end
1016
-
1017
- describe '#on_post_trigger' do
1018
-
1019
- it 'is called right after a job triggers' do
1020
-
1021
- $out = []
1022
-
1023
- def @scheduler.on_post_trigger(job)
1024
- $out << "post #{job.id}"
1025
- end
1026
-
1027
- job_id =
1028
- @scheduler.in '0.5s' do |job|
1029
- $out << job.id
1030
- end
1031
-
1032
- sleep 0.7
1033
-
1034
- expect($out).to eq([ job_id, "post #{job_id}" ])
1035
- end
1036
- end
1037
-
1038
- #--
1039
- # misc
1040
- #++
1041
-
1042
- describe '.singleton / .s' do
1043
-
1044
- before(:each) do
1045
-
1046
- Rufus::Scheduler.class_eval { @singleton = nil } # ;-)
1047
- end
1048
-
1049
- it 'returns a singleton instance of the scheduler' do
1050
-
1051
- s0 = Rufus::Scheduler.singleton
1052
- s1 = Rufus::Scheduler.s
1053
-
1054
- expect(s0.class).to eq(Rufus::Scheduler)
1055
- expect(s1.object_id).to eq(s0.object_id)
1056
- end
1057
-
1058
- it 'accepts initialization parameters' do
1059
-
1060
- s = Rufus::Scheduler.singleton(:max_work_threads => 77)
1061
- s = Rufus::Scheduler.singleton(:max_work_threads => 42)
1062
-
1063
- expect(s.max_work_threads).to eq(77)
1064
- end
1065
- end
1066
- end
1067
-