lib/rufus/scheduler/jobs.rb in rufus-scheduler-3.0.6 vs lib/rufus/scheduler/jobs.rb in rufus-scheduler-3.0.7
- old
+ new
@@ -43,10 +43,13 @@
attr_reader :original
attr_reader :scheduled_at
attr_reader :last_time
attr_reader :unscheduled_at
attr_reader :tags
+ attr_reader :count
+ attr_reader :last_work_time
+ attr_reader :mean_work_time
# next trigger time
#
attr_accessor :next_time
@@ -94,10 +97,14 @@
caller[2..-1]
) unless @callable
@tags = Array(opts[:tag] || opts[:tags]).collect { |t| t.to_s }
+ @count = 0
+ @last_work_time = 0.0
+ @mean_work_time = 0.0
+
# tidy up options
if @opts[:allow_overlap] == false || @opts[:allow_overlapping] == false
@opts[:overlap] = false
end
@@ -118,10 +125,12 @@
callback(:confirm_lock, time) &&
callback(:on_pre_trigger, time)
return if r == false
+ @count += 1
+
if opts[:blocking]
do_trigger(time)
else
do_trigger_in_thread(time)
end
@@ -247,10 +256,15 @@
do_call(time, true)
ensure
+ @last_work_time =
+ Time.now - Thread.current[:rufus_scheduler_time]
+ @mean_work_time =
+ ((@count - 1) * @mean_work_time + @last_work_time) / @count
+
post_trigger(time)
Thread.current[:rufus_scheduler_job] = nil
Thread.current[:rufus_scheduler_time] = nil
Thread.current[:rufus_scheduler_timeout] = nil
@@ -321,10 +335,15 @@
class OneTimeJob < Job
alias time next_time
+ def occurrences(time0, time1)
+
+ time >= time0 && time <= time1 ? [ time ] : []
+ end
+
protected
def determine_id
[
@@ -426,11 +445,11 @@
#
# TODO: rework that, jobs are thus kept 1 step too much in @jobs
super
- @times = @times - 1 if @times
+ @times -= 1 if @times
end
def pause
@paused_at = Time.now
@@ -452,10 +471,31 @@
self.class.name.split(':').last.downcase[0..-4],
@scheduled_at.to_f,
opts.hash.abs
].map(&:to_s).join('_')
end
+
+ def occurrences(time0, time1)
+
+ a = []
+
+ nt = @next_time
+ ts = @times
+
+ loop do
+
+ break if nt > time1
+ break if ts && ts <= 0
+
+ a << nt if nt >= time0
+
+ nt = next_time_from(nt)
+ ts = ts - 1 if ts
+ end
+
+ a
+ end
end
#
# A parent class of EveryJob and IntervalJob
#
@@ -500,10 +540,15 @@
Time.now + @frequency
else
@first_at
end
end
+
+ def next_time_from(time)
+
+ time + @frequency
+ end
end
class IntervalJob < EvInJob
attr_reader :interval
@@ -537,10 +582,15 @@
end
else
false
end
end
+
+ def next_time_from(time)
+
+ time + @mean_work_time + @interval
+ end
end
class CronJob < RepeatJob
def initialize(scheduler, cronline, opts, block)
@@ -564,9 +614,14 @@
protected
def set_next_time(is_post, trigger_time)
@next_time = @cron_line.next_time
+ end
+
+ def next_time_from(time)
+
+ @cron_line.next_time(time)
end
end
end
end