Skip to content

Instantly share code, notes, and snippets.

@jmettraux
Created March 22, 2019 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmettraux/5e998fa0ed4981e4239243a034903bb0 to your computer and use it in GitHub Desktop.
Save jmettraux/5e998fa0ed4981e4239243a034903bb0 to your computer and use it in GitHub Desktop.
patch 0 for the investigation of https://github.com/floraison/fugit/issues/15
class Fugit::Cron
def previous_time(from=::EtOrbi::EoTime.now)
from = ::EtOrbi.make_time(from)
ti = 0
ifrom = from.to_i
t = TimeCursor.new(from.translate(@timezone))
stalling = false
i = -1
loop do
i = i + 1
ti1 = t.to_i
if i % 10_000 == 0
log({
i: i, t: t_to_s(t.time), d: ti1 - ti,
from: t_to_s(from), c: original, tz: @timezone,
df: from.to_i - ti1 })
end
fail RuntimeError.new(
"loop stalled for #{@original.inspect} #previous_time, breaking"
) if stalling && ti == ti1
stalling = (ti == ti1)
ti = ti1
fail RuntimeError.new(
"too many loops for #{@original.inspect} #previous_time, breaking"
) if (ifrom - ti).abs > BREAKER_S
(ifrom == ti) && (t.inc(-1); next)
month_match?(t) || (t.dec_month; next)
day_match?(t) || (t.dec_day; next)
hour_match?(t) || (t.dec_hour; next)
min_match?(t) || (t.dec_min; next)
sec_match?(t) || (t.dec_sec(@seconds); next)
break
end
t.time.translate(from.zone)
end
protected
def t_to_s(time=Time.now)
time.strftime('%Y%m%dT%H%M%S') + sprintf('.%06dZ', time.usec)
end
def log(o)
#puts "#{t_to_s} #{Thread.current.name} Fugit#previous_time \\ #{o.inspect}"
#
# I hope the following works... If not, @godfat will probably help
#
Rails.logger.warn { "Fugit#previous_time \\ #{o.inspect}" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment