lib/rufus/scheduler/jobs.rb in rufus-scheduler-3.2.2 vs lib/rufus/scheduler/jobs.rb in rufus-scheduler-3.3.0
- old
+ new
@@ -84,11 +84,11 @@
@handler.method(:call) rescue nil
else
nil
end
- @scheduled_at = Time.now
+ @scheduled_at = Rufus::Scheduler::ZoTime.now
@unscheduled_at = nil
@last_time = nil
@locals = {}
@local_mutex = Mutex.new
@@ -122,31 +122,26 @@
def trigger(time)
@previous_time = @next_time
set_next_time(time)
- return if (
- opts[:overlap] == false &&
- running?
- )
- return if (
- callback(:confirm_lock, time) &&
- callback(:on_pre_trigger, time)
- ) == false
+ do_trigger(time)
+ end
- @count += 1
+ # Trigger the job right now, off of its schedule.
+ #
+ # Done in collaboration with Piavka in
+ # https://github.com/jmettraux/rufus-scheduler/issues/214
+ #
+ def trigger_off_schedule(time=Rufus::Scheduler::ZoTime.now)
- if opts[:blocking]
- do_trigger(time)
- else
- do_trigger_in_thread(time)
- end
+ do_trigger(time)
end
def unschedule
- @unscheduled_at = Time.now
+ @unscheduled_at = Rufus::Scheduler::ZoTime.now
end
def threads
Thread.list.select { |t| t[:rufus_scheduler_job] == self }
@@ -202,11 +197,11 @@
#
# Warning: error rescueing is the responsibity of the caller.
#
def call(do_rescue=false)
- do_call(Time.now, do_rescue)
+ do_call(Rufus::Scheduler::ZoTime.now, do_rescue)
end
protected
def callback(meth, time)
@@ -249,11 +244,31 @@
# exceptions above StandardError do pass through
end
def do_trigger(time)
- t = Time.now
+ return if (
+ opts[:overlap] == false &&
+ running?
+ )
+ return if (
+ callback(:confirm_lock, time) &&
+ callback(:on_pre_trigger, time)
+ ) == false
+
+ @count += 1
+
+ if opts[:blocking]
+ trigger_now(time)
+ else
+ trigger_queue(time)
+ end
+ end
+
+ def trigger_now(time)
+
+ t = Rufus::Scheduler::ZoTime.now
# if there are mutexes, t might be really bigger than time
Thread.current[:rufus_scheduler_job] = self
Thread.current[:rufus_scheduler_time] = t
Thread.current[:rufus_scheduler_timeout] = compute_timeout
@@ -263,11 +278,11 @@
do_call(time, true)
ensure
@last_work_time =
- Time.now - Thread.current[:rufus_scheduler_time]
+ Rufus::Scheduler::ZoTime.now - Thread.current[:rufus_scheduler_time]
@mean_work_time =
((@count - 1) * @mean_work_time + @last_work_time) / @count
post_trigger(time)
@@ -300,11 +315,11 @@
next if job.unscheduled_at
begin
(job.opts[:mutex] || []).reduce(
- lambda { job.do_trigger(time) }
+ lambda { job.trigger_now(time) }
) do |b, m|
lambda { mutex(m).synchronize { b.call } }
end.call
rescue KillSignal
@@ -319,13 +334,15 @@
#
# same as above (in the thead block),
# but since it has to be done as quickly as possible.
# So, whoever is running first (scheduler thread vs job thread)
# sets this information
+
+ thread
end
- def do_trigger_in_thread(time)
+ def trigger_queue(time)
threads = @scheduler.work_threads
cur = threads.size
vac = threads.select { |t| t[:rufus_scheduler_job] == nil }.size
@@ -421,16 +438,16 @@
def first_at=(first)
return (@first_at = nil) if first == nil
- n0 = Time.now
+ n0 = Rufus::Scheduler::ZoTime.now
n1 = n0 + 0.003
first = n0 if first == :now || first == :immediately || first == 0
- @first_at = Rufus::Scheduler.parse_to_time(first)
+ @first_at = ZoTime.make(first)
@first_at = n1 if @first_at >= n0 && @first_at < n1
fail ArgumentError.new(
"cannot set first[_at|_in] in the past: " +
"#{first.inspect} -> #{@first_at.inspect}"
@@ -439,16 +456,17 @@
@first_at
end
def last_at=(last)
- @last_at = last ? Rufus::Scheduler.parse_to_time(last) : nil
+ #@last_at = last ? Rufus::Scheduler.parse_to_time(last) : nil
+ @last_at = last ? ZoTime.make(last) : nil
fail ArgumentError.new(
"cannot set last[_at|_in] in the past: " +
"#{last.inspect} -> #{@last_at.inspect}"
- ) if last && @last_at < Time.now
+ ) if last && @last_at < Rufus::Scheduler::ZoTime.now
@last_at
end
def trigger(time)
@@ -456,20 +474,20 @@
return if @paused_at
return (@next_time = nil) if @times && @times < 1
return (@next_time = nil) if @last_at && time >= @last_at
#
- # TODO: rework that, jobs are thus kept 1 step too much in @jobs
+ # It keeps jobs one step too much in @jobs, but it's OK
super
@times -= 1 if @times
end
def pause
- @paused_at = Time.now
+ @paused_at = Rufus::Scheduler::ZoTime.now
end
def resume
@paused_at = nil
@@ -516,13 +534,11 @@
#
class EvInJob < RepeatJob
def first_at=(first)
- super
-
- @next_time = @first_at
+ @next_time = super
end
end
class EveryJob < EvInJob
@@ -546,14 +562,14 @@
def set_next_time(trigger_time, is_post=false)
return if is_post
- n = Time.now
+ n = Rufus::Scheduler::ZoTime.now
@next_time =
- if @first_at == nil || @first_at < n
+ if @first_at == nil || @first_at < (n - @scheduler.frequency)
nt = (@next_time || trigger_time || n) + @frequency
nt > n ? nt : (trigger_time || n) + @frequency
else
@first_at
end
@@ -587,13 +603,13 @@
def set_next_time(trigger_time, is_post=false)
@next_time =
if is_post
- Time.now + @interval
+ Rufus::Scheduler::ZoTime.now + @interval
elsif trigger_time.nil?
if @first_at == nil || @first_at < Time.now
- Time.now + @interval
+ Rufus::Scheduler::ZoTime.now + @interval
else
@first_at
end
else
false