rufus-scheduler 2.0.13 → 2.0.14

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,13 @@
2
2
  = rufus-scheduler CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-scheduler - 2.0.14 released 2011/12/11
6
+
7
+ - Job #pause and #resume
8
+ - Scheduler #pause(job_or_job_id) and #resume(job_or_job_id)
9
+ - Scheduler#unschedule(job_or_job_id)
10
+
11
+
5
12
  == rufus-scheduler - 2.0.13 released 2011/11/14
6
13
 
7
14
  - cron lines now OK with "09". Thanks Anthony Lewis
data/CREDITS.txt CHANGED
@@ -15,6 +15,7 @@
15
15
 
16
16
  == Feedback
17
17
 
18
+ - Henrique G. Testa - pause/resume concept
18
19
  - Sam Gibson - https://github.com/samfoo - exception handling hardening
19
20
  - sha1dy - https://github.com/sha1dy - every and overlapping exception issue
20
21
  - Defusal - unschedule_by_tag
data/README.rdoc CHANGED
@@ -295,6 +295,49 @@ Note : setting a :first_at/:first_in in the past will get rufus-scheduler to tri
295
295
  In this example, the 'every' job will unschedule itself when the crop is ready.
296
296
 
297
297
 
298
+ == #running?
299
+
300
+ job = scheduler.every '3d' do
301
+ # ...
302
+ end
303
+
304
+ # ...
305
+
306
+ p job.running?
307
+
308
+ Job#running? will return true when the job got triggered and is actually performing.
309
+
310
+ Please note, that #running? is not related to the #paused? which is detailed in the next section.
311
+
312
+
313
+ == #pause, #resume and #paused?
314
+
315
+ Jobs, as well as the scheduler itself have a pair of #pause and #resume methods.
316
+
317
+ job = scheduler.every '2h' do
318
+ # ...
319
+ end
320
+
321
+ # ...
322
+
323
+ job.pause # the job will be scheduled but won't trigger
324
+
325
+ # ...
326
+
327
+ puts job.paused?
328
+
329
+ Pausing / resuming a job doesn't affect the scheduling of a job, it merely "silences" it, its block won't get executed. Calling resume will not reset the schedule of the job. If you schedule a job to trigger every 10 minutes at 10am and pause it from 1020 to 1025, it's next triggering time will be 1030 approximately.
330
+
331
+ One can pause an "at" or "in" job. If it's still paused at trigger time, it will simply become a dud.
332
+
333
+ As said, the scheduler has a #pause(job_or_job_id) and a #resume(job_or_job_id) pair of methods:
334
+
335
+ scheduler.pause(job)
336
+ scheduler.pause(job_id)
337
+ scheduler.resume(job)
338
+ scheduler.resume(job_id)
339
+
340
+
298
341
  == schedulables
299
342
 
300
343
  Sometimes passing a block isn't that convenient :
data/lib/rufus/sc/jobs.rb CHANGED
@@ -77,6 +77,7 @@ module Scheduler
77
77
  @block = block || params[:schedulable]
78
78
 
79
79
  @running = false
80
+ @paused = false
80
81
 
81
82
  raise ArgumentError.new(
82
83
  'no block or :schedulable passed, nothing to schedule'
@@ -91,6 +92,8 @@ module Scheduler
91
92
 
92
93
  # Returns true if this job is currently running (in the middle of #trigger)
93
94
  #
95
+ # Note : paused? is not related to running?
96
+ #
94
97
  def running
95
98
 
96
99
  @running
@@ -98,6 +101,39 @@ module Scheduler
98
101
 
99
102
  alias running? running
100
103
 
104
+ # Returns true if this job is paused, false else.
105
+ #
106
+ # A paused job is still scheduled, but does not trigger.
107
+ #
108
+ # Note : paused? is not related to running?
109
+ #
110
+ def paused?
111
+
112
+ @paused
113
+ end
114
+
115
+ # Pauses this job (sets the paused flag to true).
116
+ #
117
+ # Note that it will not pause the execution of a block currently 'running'.
118
+ # Future triggering of the job will not occur until #resume is called.
119
+ #
120
+ # Note too that, during the pause time, the schedule kept the same. Calling
121
+ # #resume will not force old triggers in.
122
+ #
123
+ def pause
124
+
125
+ @paused = true
126
+ end
127
+
128
+ # Resumes this job (sets the paused flag to false).
129
+ #
130
+ # This job will trigger again.
131
+ #
132
+ def resume
133
+
134
+ @paused = false
135
+ end
136
+
101
137
  # Returns the list of tags attached to the job.
102
138
  #
103
139
  def tags
@@ -125,6 +161,8 @@ module Scheduler
125
161
  #
126
162
  def trigger(t=Time.now)
127
163
 
164
+ return if @paused
165
+
128
166
  @last = t
129
167
  job_thread = nil
130
168
  to_job = nil
@@ -353,6 +391,8 @@ module Scheduler
353
391
 
354
392
  def trigger_if_matches(time)
355
393
 
394
+ return if @paused
395
+
356
396
  trigger(time) if @cron_line.matches?(time)
357
397
  end
358
398
 
@@ -177,11 +177,13 @@ module Rufus::Scheduler
177
177
  end
178
178
  alias :schedule :cron
179
179
 
180
- # Unschedules a job (cron or at/every/in job) given its id.
180
+ # Unschedules a job (cron or at/every/in job).
181
181
  #
182
182
  # Returns the job that got unscheduled.
183
183
  #
184
- def unschedule(job_id)
184
+ def unschedule(job_or_id)
185
+
186
+ job_id = job_or_id.respond_to?(:job_id) ? job_or_id.job_id : job_or_id
185
187
 
186
188
  @jobs.unschedule(job_id) || @cron_jobs.unschedule(job_id)
187
189
  end
@@ -196,6 +198,22 @@ module Rufus::Scheduler
196
198
  jobs
197
199
  end
198
200
 
201
+ # Pauses a given job. If the argument is an id (String) and the
202
+ # corresponding job cannot be found, an ArgumentError will get raised.
203
+ #
204
+ def pause(job_or_id)
205
+
206
+ find(job_or_id).pause
207
+ end
208
+
209
+ # Resumes a given job. If the argument is an id (String) and the
210
+ # corresponding job cannot be found, an ArgumentError will get raised.
211
+ #
212
+ def resume(job_or_id)
213
+
214
+ find(job_or_id).resume
215
+ end
216
+
199
217
  #--
200
218
  # MISC
201
219
  #++
@@ -270,6 +288,25 @@ module Rufus::Scheduler
270
288
  all_jobs.values.select { |j| j.tags.include?(tag) }
271
289
  end
272
290
 
291
+ # Mostly used to find a job given its id. If the argument is a job, will
292
+ # simply return it.
293
+ #
294
+ # If the argument is an id, and no job with that id is found, it will
295
+ # raise an ArgumentError.
296
+ #
297
+ def find(job_or_id)
298
+
299
+ return job_or_id if job_or_id.respond_to?(:job_id)
300
+
301
+ job = all_jobs[job_or_id]
302
+
303
+ raise ArgumentError.new(
304
+ "couldn't find job #{job_or_id.inspect}"
305
+ ) unless job
306
+
307
+ job
308
+ end
309
+
273
310
  # Returns the current list of trigger threads (threads) dedicated to
274
311
  # the execution of jobs.
275
312
  #
@@ -386,7 +423,7 @@ module Rufus::Scheduler
386
423
  @thread = Thread.new do
387
424
  loop do
388
425
  sleep(@frequency)
389
- self.step
426
+ step
390
427
  end
391
428
  end
392
429
 
@@ -26,7 +26,7 @@
26
26
  module Rufus
27
27
  module Scheduler
28
28
 
29
- VERSION = '2.0.13'
29
+ VERSION = '2.0.14'
30
30
  end
31
31
  end
32
32
 
data/spec/at_spec.rb CHANGED
@@ -60,12 +60,12 @@ describe "#{SCHEDULER_CLASS}#schedule_at" do
60
60
  @s.jobs.should == {}
61
61
  end
62
62
 
63
- it 'unschedules' do
63
+ it 'unschedules (job_id)' do
64
64
 
65
65
  job = @s.at Time.now + 3 * 3600 do
66
66
  end
67
67
 
68
- wait_next_tick
68
+ sleep 0.300
69
69
 
70
70
  @s.jobs.size.should == 1
71
71
 
@@ -74,6 +74,20 @@ describe "#{SCHEDULER_CLASS}#schedule_at" do
74
74
  @s.jobs.size.should == 0
75
75
  end
76
76
 
77
+ it 'unschedules (job)' do
78
+
79
+ job = @s.at Time.now + 3 * 3600 do
80
+ end
81
+
82
+ sleep 0.300
83
+
84
+ @s.jobs.size.should == 1
85
+
86
+ @s.unschedule(job)
87
+
88
+ @s.jobs.size.should == 0
89
+ end
90
+
77
91
  it 'accepts tags for jobs' do
78
92
 
79
93
  job = @s.at Time.now + 3 * 3600, :tags => 'spec' do
data/spec/cron_spec.rb CHANGED
@@ -37,7 +37,7 @@ describe "#{SCHEDULER_CLASS}#cron" do
37
37
  seconds.uniq.size.should == seconds.size
38
38
  end
39
39
 
40
- it 'unschedules' do
40
+ it 'unschedules (self)' do
41
41
 
42
42
  second = nil
43
43
 
@@ -60,6 +60,29 @@ describe "#{SCHEDULER_CLASS}#cron" do
60
60
  second.should == after
61
61
  end
62
62
 
63
+ it 'unschedules (job)' do
64
+
65
+ second = nil
66
+
67
+ job = @s.cron '* * * * * *' do |job|
68
+ second = job.last.sec
69
+ end
70
+
71
+ second.should == nil
72
+
73
+ sleep 2
74
+
75
+ second.should_not == nil
76
+
77
+ @s.unschedule(job)
78
+
79
+ after = second
80
+
81
+ sleep 2
82
+
83
+ second.should == after
84
+ end
85
+
63
86
  it 'keeps track of cron jobs' do
64
87
 
65
88
  j0 = @s.cron '7 10-12 * * * *' do
data/spec/job_spec.rb CHANGED
@@ -154,6 +154,40 @@ describe 'job classes' do
154
154
  job.next_time.to_i.should == t.to_i
155
155
  end
156
156
  end
157
+
158
+ describe '#paused?' do
159
+
160
+ it 'returns false initially' do
161
+
162
+ job = @s.every '3h' do; end
163
+
164
+ job.paused?.should == false
165
+ end
166
+ end
167
+
168
+ describe '#pause' do
169
+
170
+ it 'pauses the job' do
171
+
172
+ job = @s.every '3h' do; end
173
+
174
+ job.pause
175
+
176
+ job.paused?.should == true
177
+ end
178
+ end
179
+
180
+ describe '#resume' do
181
+
182
+ it 'resumes the job' do
183
+
184
+ job = @s.every '3h' do; end
185
+
186
+ job.resume
187
+
188
+ job.paused?.should == false
189
+ end
190
+ end
157
191
  end
158
192
 
159
193
  describe Rufus::Scheduler::CronJob do
@@ -169,6 +203,40 @@ describe 'job classes' do
169
203
  (job.next_time.to_i - Time.now.to_i).should satisfy { |v| v < 60 }
170
204
  end
171
205
  end
206
+
207
+ describe '#paused?' do
208
+
209
+ it 'returns false initially' do
210
+
211
+ job = @s.cron '* * * * *' do; end
212
+
213
+ job.paused?.should == false
214
+ end
215
+ end
216
+
217
+ describe '#pause' do
218
+
219
+ it 'pauses the job' do
220
+
221
+ job = @s.cron '* * * * *' do; end
222
+
223
+ job.pause
224
+
225
+ job.paused?.should == true
226
+ end
227
+ end
228
+
229
+ describe '#resume' do
230
+
231
+ it 'resumes the job' do
232
+
233
+ job = @s.cron '* * * * *' do; end
234
+
235
+ job.resume
236
+
237
+ job.paused?.should == false
238
+ end
239
+ end
172
240
  end
173
241
  end
174
242
 
@@ -64,6 +64,98 @@ describe SCHEDULER_CLASS do
64
64
 
65
65
  stop_scheduler(s)
66
66
  end
67
+
68
+ context 'pause/resume' do
69
+
70
+ before(:each) do
71
+ @s = start_scheduler
72
+ end
73
+ after(:each) do
74
+ stop_scheduler(@s)
75
+ end
76
+
77
+ describe '#pause' do
78
+
79
+ it 'pauses a job (every)' do
80
+
81
+ $count = 0
82
+
83
+ j = @s.every '1s' do
84
+ $count = $count + 1
85
+ end
86
+
87
+ @s.pause(j.job_id)
88
+
89
+ sleep 2.5
90
+
91
+ j.paused?.should == true
92
+ $count.should == 0
93
+ end
94
+
95
+ it 'pauses a job (cron)' do
96
+
97
+ $count = 0
98
+
99
+ j = @s.cron '* * * * * *' do
100
+ $count = $count + 1
101
+ end
102
+
103
+ @s.pause(j.job_id)
104
+
105
+ sleep 2.5
106
+
107
+ j.paused?.should == true
108
+ $count.should == 0
109
+ end
110
+ end
111
+
112
+ describe '#resume' do
113
+
114
+ it 'resumes a job (every)' do
115
+
116
+ $count = 0
117
+
118
+ j = @s.every '1s' do
119
+ $count = $count + 1
120
+ end
121
+
122
+ @s.pause(j.job_id)
123
+
124
+ sleep 2.5
125
+
126
+ c = $count
127
+
128
+ @s.resume(j.job_id)
129
+
130
+ sleep 1.5
131
+
132
+ j.paused?.should == false
133
+ ($count > c).should == true
134
+ end
135
+
136
+ it 'pauses a job (cron)' do
137
+
138
+ $count = 0
139
+
140
+ j = @s.cron '* * * * * *' do
141
+ $count = $count + 1
142
+ end
143
+
144
+ @s.pause(j.job_id)
145
+
146
+ sleep 2.5
147
+
148
+ c = $count
149
+
150
+ @s.resume(j.job_id)
151
+
152
+ sleep 1.5
153
+
154
+ j.paused?.should == false
155
+ ($count > c).should == true
156
+ end
157
+ end
158
+ end
67
159
  end
68
160
 
69
161
  describe 'Rufus::Scheduler#start_new' do
metadata CHANGED
@@ -1,79 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rufus-scheduler
3
- version: !ruby/object:Gem::Version
4
- hash: 21
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.14
5
5
  prerelease:
6
- segments:
7
- - 2
8
- - 0
9
- - 13
10
- version: 2.0.13
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - John Mettraux
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-11-14 00:00:00 +09:00
12
+ date: 2011-12-11 00:00:00.000000000 +09:00
19
13
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: tzinfo
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2160139540 !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 61
30
- segments:
31
- - 0
32
- - 3
33
- - 23
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
34
22
  version: 0.3.23
35
23
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: rake
39
24
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *2160139540
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &2160138880 !ruby/object:Gem::Requirement
41
29
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
- version: "0"
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
49
34
  type: :development
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: rspec
53
35
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *2160138880
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &2160138220 !ruby/object:Gem::Requirement
55
40
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 19
60
- segments:
61
- - 2
62
- - 7
63
- - 0
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
64
44
  version: 2.7.0
65
45
  type: :development
66
- version_requirements: *id003
46
+ prerelease: false
47
+ version_requirements: *2160138220
67
48
  description: job scheduler for Ruby (at, cron, in and every jobs).
68
- email:
49
+ email:
69
50
  - jmettraux@gmail.com
70
51
  executables: []
71
-
72
52
  extensions: []
73
-
74
53
  extra_rdoc_files: []
75
-
76
- files:
54
+ files:
77
55
  - Rakefile
78
56
  - lib/rufus/otime.rb
79
57
  - lib/rufus/sc/cronline.rb
@@ -105,45 +83,32 @@ files:
105
83
  - rufus-scheduler.gemspec
106
84
  - CHANGELOG.txt
107
85
  - CREDITS.txt
108
- - dump.txt
109
86
  - LICENSE.txt
110
- - release.txt
111
- - t.txt
112
87
  - TODO.txt
113
88
  - README.rdoc
114
89
  has_rdoc: true
115
90
  homepage: http://github.com/jmettraux/rufus-scheduler
116
91
  licenses: []
117
-
118
92
  post_install_message:
119
93
  rdoc_options: []
120
-
121
- require_paths:
94
+ require_paths:
122
95
  - lib
123
- required_ruby_version: !ruby/object:Gem::Requirement
96
+ required_ruby_version: !ruby/object:Gem::Requirement
124
97
  none: false
125
- requirements:
126
- - - ">="
127
- - !ruby/object:Gem::Version
128
- hash: 3
129
- segments:
130
- - 0
131
- version: "0"
132
- required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
103
  none: false
134
- requirements:
135
- - - ">="
136
- - !ruby/object:Gem::Version
137
- hash: 3
138
- segments:
139
- - 0
140
- version: "0"
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
141
108
  requirements: []
142
-
143
109
  rubyforge_project: rufus
144
110
  rubygems_version: 1.6.2
145
111
  signing_key:
146
112
  specification_version: 3
147
113
  summary: job scheduler for Ruby (at, cron, in and every jobs)
148
114
  test_files: []
149
-
data/dump.txt DELETED
@@ -1,18 +0,0 @@
1
-
2
- jmettraux@sanma ~/tmp/billen/bill 円 script/server
3
- => Booting WEBrick
4
- => Rails 2.3.10 application starting on http://0.0.0.0:3000
5
- => Call with -d to detach
6
- => Ctrl-C to shutdown server
7
- [2010-12-15 09:41:55] INFO WEBrick 1.3.1
8
- [2010-12-15 09:41:55] INFO ruby 1.8.7 (2010-04-19) [i686-darwin10.5.0]
9
- [2010-12-15 09:41:55] INFO WEBrick::HTTPServer#start: pid=15393 port=3000
10
- "Wed Dec 15 09:43:00 +0900 2010 - run 0"
11
- "Wed Dec 15 10:43:00 +0900 2010 - run 1"
12
- "Wed Dec 15 11:43:00 +0900 2010 - run 2"
13
- "Wed Dec 15 12:43:00 +0900 2010 - run 3"
14
- "Wed Dec 15 13:43:00 +0900 2010 - run 4"
15
- "Wed Dec 15 14:43:00 +0900 2010 - run 5"
16
- "Wed Dec 15 15:43:00 +0900 2010 - run 6"
17
- "Wed Dec 15 16:43:00 +0900 2010 - run 7"
18
-
data/release.txt DELETED
@@ -1,18 +0,0 @@
1
- Rufus-scheduler is a thread-based scheduler written in Ruby. It lets you write code like
2
-
3
- [sourcecode language="ruby"]
4
- s = Rufus::Scheduler.start_new
5
-
6
- s.every '10m' do
7
- puts 'open the window for the cat'
8
- end
9
-
10
- s.at '2012-01-01 12:00' do
11
- puts 'reminder: wife's birthday'
12
- end
13
-
14
- s.cron '0 22 * * 1-5' do
15
- puts 'activate the security system'
16
- end
17
- [/sourcecode]
18
-
data/t.txt DELETED
@@ -1,2 +0,0 @@
1
- $a = []; require 'rubygems'; load 'lib/rufus/scheduler.rb'; s = Rufus::Scheduler.start_new; s.cron('* * * * *') { $a << Time.now.strftime('%T.%3N') }
2
- s.join