openwferu-scheduler 0.9.15.1172 → 0.9.16

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.
Files changed (2) hide show
  1. data/lib/openwfe/util/scheduler.rb +152 -35
  2. metadata +2 -2
@@ -298,16 +298,6 @@ module OpenWFE
298
298
  # Schedules a job in a loop. After an execution, it will not execute
299
299
  # before the time specified in 'freq'.
300
300
  #
301
- # Note that if your job takes 2s to execute and the freq is set to
302
- # 10s, it will in fact execute every 12s.
303
- # You can however wrap the code within its own thread :
304
- #
305
- # scheduler.schedule_every("12s") do
306
- # Thread.new do
307
- # do_the_job()
308
- # end
309
- # end
310
- #
311
301
  # This method returns a job identifier which can be used to unschedule()
312
302
  # the job.
313
303
  #
@@ -317,20 +307,30 @@ module OpenWFE
317
307
 
318
308
  params = prepare_params params
319
309
  schedulable = params[:schedulable]
320
- params[:every] = true
310
+ params[:every] = freq
321
311
 
322
- sschedule_at Time.new.to_f + f, params do |job_id, at|
312
+ last_at = params[:last_at]
313
+ next_at = if last_at
314
+ last_at + f
315
+ else
316
+ Time.now.to_f + f
317
+ end
323
318
 
324
- params[:job_id] = job_id
319
+ sschedule_at next_at, params do |job_id, at|
325
320
 
326
321
  if schedulable
327
322
  schedulable.trigger(params)
328
323
  else
329
324
  block.call job_id, at
330
325
  end
326
+
327
+ params[:job_id] = job_id
328
+ params[:last_at] = at
331
329
 
332
- schedule_every(f, params, &block) \
330
+ schedule_every(freq, params, &block) \
333
331
  unless @dont_reschedule_every
332
+ #
333
+ # yes, this is a kind of recursion
334
334
 
335
335
  job_id
336
336
  end
@@ -405,13 +405,11 @@ module OpenWFE
405
405
 
406
406
  unschedule(cron_id) if cron_id
407
407
 
408
- tags = params[:tags]
409
-
410
408
  #
411
409
  # schedule
412
410
 
413
411
  b = to_block(params, &block)
414
- job = CronJob.new(self, cron_id, cron_line, tags, &b)
412
+ job = CronJob.new(self, cron_id, cron_line, params, &b)
415
413
  @cron_jobs[job.job_id] = job
416
414
 
417
415
  job.job_id
@@ -427,8 +425,10 @@ module OpenWFE
427
425
  job = @cron_jobs[job_id]
428
426
  return job if job
429
427
 
430
- @pending_jobs.find do |job|
431
- job.job_id == job_id
428
+ synchronize do
429
+ @pending_jobs.find do |job|
430
+ job.job_id == job_id
431
+ end
432
432
  end
433
433
  end
434
434
 
@@ -456,8 +456,10 @@ module OpenWFE
456
456
  job.has_tag?(tag)
457
457
  end
458
458
 
459
- result + @pending_jobs.find_all do |job|
460
- job.has_tag?(tag)
459
+ synchronize do
460
+ result + @pending_jobs.find_all do |job|
461
+ job.has_tag?(tag)
462
+ end
461
463
  end
462
464
  end
463
465
 
@@ -554,10 +556,10 @@ module OpenWFE
554
556
  end
555
557
 
556
558
  job_id = params[:job_id]
557
- tags = params[:tags]
558
559
 
559
560
  b = to_block(params, &block)
560
- job = jobClass.new(self, at, job_id, tags, &b)
561
+
562
+ job = jobClass.new(self, at, job_id, params, &b)
561
563
 
562
564
  unschedule(job_id) if job_id
563
565
 
@@ -670,6 +672,8 @@ module OpenWFE
670
672
 
671
673
  # TODO : eventually consider running cron / pending
672
674
  # job triggering in two different threads
675
+ #
676
+ # but well... there's the synchronization issue...
673
677
 
674
678
  #
675
679
  # cron jobs
@@ -710,13 +714,21 @@ module OpenWFE
710
714
  #
711
715
  # obviously
712
716
 
713
- trigger(job)
717
+ trigger job
714
718
 
715
719
  @pending_jobs.delete_at(0)
716
720
  end
717
721
  end
718
722
  end
719
723
 
724
+ #
725
+ # Triggers the job (in a dedicated thread).
726
+ #
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
+ #
720
732
  def trigger (job)
721
733
 
722
734
  Thread.new do
@@ -758,6 +770,9 @@ module OpenWFE
758
770
  # would it be better to use a Mutex instead of a full-blown
759
771
  # Monitor ?
760
772
 
773
+ #
774
+ # The parent class for scheduled jobs.
775
+ #
761
776
  class Job
762
777
 
763
778
  @@last_given_id = 0
@@ -765,10 +780,33 @@ module OpenWFE
765
780
  # as a scheduler is fully transient, no need to
766
781
  # have persistent ids, a simple counter is sufficient
767
782
 
768
- attr_accessor :job_id, :tags, :block
783
+ #
784
+ # The identifier for the job
785
+ #
786
+ attr_accessor :job_id
787
+
788
+ #
789
+ # An array of tags
790
+ #
791
+ attr_accessor :tags
792
+
793
+ #
794
+ # The block to execute at trigger time
795
+ #
796
+ attr_accessor :block
797
+
798
+ #
799
+ # A reference to the scheduler
800
+ #
769
801
  attr_reader :scheduler
802
+
803
+ #
804
+ # Keeping a copy of the initialization params of the job.
805
+ #
806
+ attr_reader :params
770
807
 
771
- def initialize (scheduler, job_id, tags, &block)
808
+
809
+ def initialize (scheduler, job_id, params, &block)
772
810
 
773
811
  @scheduler = scheduler
774
812
  @block = block
@@ -782,10 +820,12 @@ module OpenWFE
782
820
  end
783
821
  end
784
822
 
823
+ @params = params
824
+
785
825
  #@tags = Array(tags).collect { |tag| tag.to_s }
786
826
  # making sure we have an array of String tags
787
827
 
788
- @tags = Array(tags)
828
+ @tags = Array(params[:tags])
789
829
  # any tag is OK
790
830
  end
791
831
 
@@ -793,6 +833,7 @@ module OpenWFE
793
833
  # Returns true if this job sports the given tag
794
834
  #
795
835
  def has_tag? (tag)
836
+
796
837
  @tags.include?(tag)
797
838
  end
798
839
 
@@ -800,53 +841,118 @@ module OpenWFE
800
841
  # Removes (cancels) this job from its scheduler.
801
842
  #
802
843
  def unschedule
844
+
803
845
  @scheduler.unschedule(@job_id)
804
846
  end
805
847
  end
806
848
 
849
+ #
850
+ # An 'at' job.
807
851
  class AtJob < Job
808
852
 
853
+ #
854
+ # The float representation (Time.to_f) of the time at which
855
+ # the job should be triggered.
856
+ #
809
857
  attr_accessor :at
810
858
 
811
- def initialize (scheduler, at, at_id, tags, &block)
812
- super(scheduler, at_id, tags, &block)
859
+ #
860
+ # The constructor.
861
+ #
862
+ def initialize (scheduler, at, at_id, params, &block)
863
+
864
+ super(scheduler, at_id, params, &block)
813
865
  @at = at
814
866
  end
815
867
 
868
+ #
869
+ # Triggers the job (calls the block)
870
+ #
816
871
  def trigger
872
+
817
873
  @block.call @job_id, @at
818
874
  end
875
+
876
+ #
877
+ # Returns the Time instance at which this job is scheduled.
878
+ #
879
+ def schedule_info
880
+
881
+ Time.at(@at)
882
+ end
819
883
  end
820
884
 
885
+ #
886
+ # An 'every' job is simply an extension of an 'at' job.
887
+ #
821
888
  class EveryJob < AtJob
889
+
890
+ #
891
+ # Returns the frequency string used to schedule this EveryJob,
892
+ # like for example "3d" or "1M10d3h".
893
+ #
894
+ def schedule_info
895
+
896
+ @params[:every]
897
+ end
822
898
  end
823
899
 
900
+ #
901
+ # A cron job.
902
+ #
824
903
  class CronJob < Job
825
904
 
905
+ #
906
+ # The CronLine instance representing the times at which
907
+ # the cron job has to be triggered.
908
+ #
826
909
  attr_accessor :cron_line
827
910
 
828
- def initialize (scheduler, cron_id, line, tags, &block)
911
+ def initialize (scheduler, cron_id, line, params, &block)
829
912
 
830
- super(scheduler, cron_id, tags, &block)
913
+ super(scheduler, cron_id, params, &block)
914
+
915
+ if line.is_a?(String)
831
916
 
832
- if line.kind_of?(String)
833
917
  @cron_line = CronLine.new(line)
834
- elsif line.kind_of?(CronLine)
918
+
919
+ elsif line.is_a?(CronLine)
920
+
835
921
  @cron_line = line
922
+
836
923
  else
924
+
837
925
  raise \
838
926
  "Cannot initialize a CronJob " +
839
927
  "with a param of class #{line.class}"
840
928
  end
841
929
  end
842
930
 
931
+ #
932
+ # This is the method called by the scheduler to determine if it
933
+ # has to fire this CronJob instance.
934
+ #
843
935
  def matches? (time)
936
+
844
937
  @cron_line.matches? time
845
938
  end
846
939
 
940
+ #
941
+ # As the name implies.
942
+ #
847
943
  def trigger
944
+
848
945
  @block.call @job_id, @cron_line
849
946
  end
947
+
948
+ #
949
+ # Returns the original cron tab string used to schedule this
950
+ # Job. Like for example "60/3 * * * Sun".
951
+ #
952
+ def schedule_info
953
+
954
+ @cron_line.original
955
+ end
850
956
  end
851
957
 
852
958
  #
@@ -855,6 +961,11 @@ module OpenWFE
855
961
  #
856
962
  class CronLine
857
963
 
964
+ #
965
+ # The string used for creating this cronline instance.
966
+ #
967
+ attr_reader :original
968
+
858
969
  attr_reader \
859
970
  :seconds,
860
971
  :minutes,
@@ -867,6 +978,8 @@ module OpenWFE
867
978
 
868
979
  super()
869
980
 
981
+ @original = line
982
+
870
983
  items = line.split
871
984
 
872
985
  unless [ 5, 6 ].include?(items.length)
@@ -891,6 +1004,9 @@ module OpenWFE
891
1004
  #adjust_arrays()
892
1005
  end
893
1006
 
1007
+ #
1008
+ # Returns true if the given time matches this cron line.
1009
+ #
894
1010
  def matches? (time)
895
1011
 
896
1012
  if time.kind_of?(Float) or time.kind_of?(Integer)
@@ -918,7 +1034,7 @@ module OpenWFE
918
1034
 
919
1035
  private
920
1036
 
921
- #
1037
+ #--
922
1038
  # adjust values to Ruby
923
1039
  #
924
1040
  #def adjust_arrays()
@@ -934,7 +1050,8 @@ module OpenWFE
934
1050
  # } if @weekdays
935
1051
  #end
936
1052
  #
937
- # dead code
1053
+ # dead code, keeping as a reminder
1054
+ #++
938
1055
 
939
1056
  WDS = [ "mon", "tue", "wed", "thu", "fri", "sat", "sun" ]
940
1057
  #
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: openwferu-scheduler
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.9.15.1172
7
- date: 2007-10-18 00:00:00 +09:00
6
+ version: 0.9.16
7
+ date: 2007-11-19 00:00:00 +09:00
8
8
  summary: OpenWFEru scheduler for Ruby (at, cron and every)
9
9
  require_paths:
10
10
  - lib