openwferu-scheduler 0.9.16 → 0.9.16.1404

Sign up to get free protection for your applications and to get access to all the features.
@@ -128,7 +128,8 @@ module OpenWFE
128
128
 
129
129
  number = ""
130
130
 
131
- while true
131
+ loop do
132
+
132
133
  index = index + 1
133
134
 
134
135
  if index >= string.length
@@ -53,7 +53,7 @@ module OpenWFE
53
53
  # params (usually an array or nil), either a block, which is more in the
54
54
  # Ruby way.
55
55
  #
56
- # Some examples :
56
+ # == Examples
57
57
  #
58
58
  # scheduler.schedule_in("3d") do
59
59
  # regenerate_monthly_report()
@@ -120,6 +120,8 @@ module OpenWFE
120
120
  # # (the default is 4 times per second (0.250))
121
121
  #
122
122
  #
123
+ # == Tags
124
+ #
123
125
  # Since OpenWFEru 0.9.16, tags can be attached to jobs scheduled :
124
126
  #
125
127
  # scheduler.schedule_in "2h", :tags => "backup" do
@@ -144,6 +146,8 @@ module OpenWFE
144
146
  # by itself, so no serialization issue.
145
147
  #
146
148
  #
149
+ # == Cron up to the second
150
+ #
147
151
  # Since OpenWFEru 0.9.16, a cron schedule can be set at the second level :
148
152
  #
149
153
  # scheduler.schedule "7 * * * * *" do
@@ -154,6 +158,55 @@ module OpenWFE
154
158
  # scheduling. This column can, like for the other columns, specify a
155
159
  # value ("7"), a list of values ("7,8,9,27") or a range ("7-12").
156
160
  #
161
+ # == Exceptions
162
+ #
163
+ # The OpenWFEru scheduler will output a stacktrace to the STDOUT in
164
+ # case of exception. There are two ways to change that behaviour.
165
+ #
166
+ # # 1 - providing a lwarn method to the scheduler instance :
167
+ #
168
+ # class << scheduler
169
+ # def lwarn (&block)
170
+ # puts "oops, something wrong happened : "
171
+ # puts block.call
172
+ # end
173
+ # end
174
+ #
175
+ # # 2 - overriding the [protected] method log_exception(e) :
176
+ #
177
+ # class << scheduler
178
+ # def log_exception (e)
179
+ # puts "something wrong happened : "+e.to_s
180
+ # end
181
+ # end
182
+ #
183
+ # == 'Every jobs' and rescheduling
184
+ #
185
+ # Every jobs can reschedule/unschedule themselves. A reschedule example :
186
+ #
187
+ # schedule.schedule_every "5h" do |job_id, at, params|
188
+ #
189
+ # mails = $inbox.fetch_mails
190
+ # mails.each { |m| $inbox.mark_as_spam(m) if is_spam(m) }
191
+ #
192
+ # params[:every] = if mails.size > 100
193
+ # "1h" # lots of spam, check every hour
194
+ # else
195
+ # "5h" # normal schedule, every 5 hours
196
+ # end
197
+ # end
198
+ #
199
+ # Unschedule example :
200
+ #
201
+ # schedule.schedule_every "10s" do |job_id, at, params|
202
+ # #
203
+ # # polls every 10 seconds until a mail arrives
204
+ #
205
+ # $mail = $inbox.fetch_last_mail
206
+ #
207
+ # params[:dont_reschedule] = true if $mail
208
+ # end
209
+ #
157
210
  class Scheduler
158
211
  include MonitorMixin
159
212
 
@@ -211,7 +264,7 @@ module OpenWFE
211
264
  "openwferu scheduler (Ruby Thread)"
212
265
  end
213
266
 
214
- while true
267
+ loop do
215
268
  break if @stopped
216
269
  step
217
270
  sleep @precision
@@ -301,6 +354,14 @@ module OpenWFE
301
354
  # This method returns a job identifier which can be used to unschedule()
302
355
  # the job.
303
356
  #
357
+ # In case of exception in the job, it will be rescheduled. If you don't
358
+ # want the job to be rescheduled, set the parameter :try_again to false.
359
+ #
360
+ # scheduler.schedule_every "500", :try_again => false do
361
+ # do_some_prone_to_error_stuff()
362
+ # # won't get rescheduled in base of exception
363
+ # end
364
+ #
304
365
  def schedule_every (freq, params={}, &block)
305
366
 
306
367
  f = duration_to_f freq
@@ -318,20 +379,58 @@ module OpenWFE
318
379
 
319
380
  sschedule_at next_at, params do |job_id, at|
320
381
 
321
- if schedulable
322
- schedulable.trigger(params)
323
- else
324
- block.call job_id, at
382
+ #
383
+ # trigger ...
384
+
385
+ hit_exception = false
386
+
387
+ begin
388
+
389
+ if schedulable
390
+ schedulable.trigger params
391
+ else
392
+ block.call job_id, at, params
393
+ end
394
+
395
+ rescue Exception => e
396
+
397
+ log_exception e
398
+
399
+ hit_exception = true
325
400
  end
326
401
 
327
- params[:job_id] = job_id
328
- params[:last_at] = at
329
-
330
- schedule_every(freq, params, &block) \
331
- unless @dont_reschedule_every
402
+ # cannot use a return here !!! (block)
403
+
404
+ #unless (hit_exception and params[:try_again] == false)
405
+ # #
406
+ # # reschedule ...
407
+ # params[:job_id] = job_id
408
+ # params[:last_at] = at
409
+ #
410
+ # schedule_every(params[:every], params, &block) \
411
+ # unless @dont_reschedule_every
412
+ # #
413
+ # # yes, this is a kind of recursion
414
+ #end
415
+ unless \
416
+ @dont_reschedule_every or
417
+ (params[:dont_reschedule] == true) or
418
+ (hit_exception and params[:try_again] == false)
419
+
420
+ #
421
+ # ok, reschedule ...
422
+
423
+ params[:job_id] = job_id
424
+ params[:last_at] = at
425
+
426
+ schedule_every params[:every], params, &block
332
427
  #
333
428
  # yes, this is a kind of recursion
334
429
 
430
+ # note that params[:every] might have been changed
431
+ # by the block/schedulable code
432
+ end
433
+
335
434
  job_id
336
435
  end
337
436
  end
@@ -524,6 +623,7 @@ module OpenWFE
524
623
  # Making sure that params is a Hash.
525
624
  #
526
625
  def prepare_params (params)
626
+
527
627
  params = { :schedulable => params } \
528
628
  if params.is_a?(Schedulable)
529
629
  params
@@ -595,6 +695,7 @@ module OpenWFE
595
695
  # will yields 10.0
596
696
  #
597
697
  def duration_to_f (s)
698
+
598
699
  return s if s.kind_of? Float
599
700
  return OpenWFE::parse_time_string(s) if s.kind_of? String
600
701
  Float(s.to_s)
@@ -697,7 +798,7 @@ module OpenWFE
697
798
  #
698
799
  # that's what at jobs do understand
699
800
 
700
- while true
801
+ loop do
701
802
 
702
803
  #puts "step() job.count is #{@pending_jobs.length}"
703
804
 
@@ -716,7 +817,7 @@ module OpenWFE
716
817
 
717
818
  trigger job
718
819
 
719
- @pending_jobs.delete_at(0)
820
+ @pending_jobs.delete_at 0
720
821
  end
721
822
  end
722
823
  end
@@ -724,28 +825,41 @@ module OpenWFE
724
825
  #
725
826
  # Triggers the job (in a dedicated thread).
726
827
  #
727
- # If an error occurs in the job, it well get caught and an error
728
- # message will be displayed to STDOUT.
729
- # If this scheduler provides a lwarn(message) method, it will
730
- # be used insted.
731
- #
732
828
  def trigger (job)
733
829
 
734
830
  Thread.new do
735
831
  begin
832
+
736
833
  job.trigger
834
+
737
835
  rescue Exception => e
738
- message =
739
- "trigger() caught exception\n" +
740
- OpenWFE::exception_to_s(e)
741
- if self.respond_to? :lwarn
742
- lwarn { message }
743
- else
744
- puts message
745
- end
836
+
837
+ log_exception e
746
838
  end
747
839
  end
748
840
  end
841
+
842
+ #
843
+ # If an error occurs in the job, it well get caught and an error
844
+ # message will be displayed to STDOUT.
845
+ # If this scheduler provides a lwarn(message) method, it will
846
+ # be used insted.
847
+ #
848
+ # Of course, one can override this method.
849
+ #
850
+ def log_exception (e)
851
+
852
+ message =
853
+ "trigger() caught exception\n" +
854
+ e.to_s + "\n" +
855
+ e.backtrace.join("\n")
856
+
857
+ if self.respond_to?(:lwarn)
858
+ lwarn { message }
859
+ else
860
+ puts message
861
+ end
862
+ end
749
863
  end
750
864
 
751
865
  #
@@ -1009,9 +1123,8 @@ module OpenWFE
1009
1123
  #
1010
1124
  def matches? (time)
1011
1125
 
1012
- if time.kind_of?(Float) or time.kind_of?(Integer)
1013
- time = Time.at(time)
1014
- end
1126
+ time = Time.at(time) \
1127
+ if time.kind_of?(Float) or time.kind_of?(Integer)
1015
1128
 
1016
1129
  return false if no_match?(time.sec, @seconds)
1017
1130
  return false if no_match?(time.min, @minutes)
@@ -1132,7 +1245,7 @@ module OpenWFE
1132
1245
  result = []
1133
1246
 
1134
1247
  value = istart
1135
- while true
1248
+ loop do
1136
1249
 
1137
1250
  result << value
1138
1251
  value = value + inc
metadata CHANGED
@@ -1,47 +1,54 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: openwferu-scheduler
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.9.16
7
- date: 2007-11-19 00:00:00 +09:00
8
- summary: OpenWFEru scheduler for Ruby (at, cron and every)
9
- require_paths:
10
- - lib
11
- email: john at openwfe dot org
12
- homepage: http://openwferu.rubyforge.org/scheduler.html
13
- rubyforge_project:
14
- description:
15
- autorequire: openwferu-scheduler
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.9.16.1404
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - John Mettraux
31
- files:
32
- - lib/openwfe/util/otime.rb
33
- - lib/openwfe/util/scheduler.rb
34
- test_files: []
35
-
36
- rdoc_options: []
8
+ autorequire: openwferu-scheduler
9
+ bindir: bin
10
+ cert_chain: []
37
11
 
38
- extra_rdoc_files: []
12
+ date: 2007-12-28 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
39
15
 
16
+ description:
17
+ email: john at openwfe dot org
40
18
  executables: []
41
19
 
42
20
  extensions: []
43
21
 
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/openwfe/util/otime.rb
26
+ - lib/openwfe/util/scheduler.rb
27
+ has_rdoc: false
28
+ homepage: http://openwferu.rubyforge.org/scheduler.html
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
44
46
  requirements: []
45
47
 
46
- dependencies: []
48
+ rubyforge_project:
49
+ rubygems_version: 0.9.5
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: OpenWFEru scheduler for Ruby (at, cron and every)
53
+ test_files: []
47
54