jmettraux/rufus-scheduler

Is there a way to access when a job "would have" run

Closed this issue ยท 11 comments

jjb commented

I'm looking for a way to see the last time [edit: the last time a job would have run]

require "rufus-scheduler"
scheduler = Rufus::Scheduler.new
j = scheduler.schedule_cron("* * 1 * *"){puts "hello"}
Time.at(j.next_time.seconds)
#=> 2021-10-01 00:00:00 +0100
Time.at(j.previous_theoretical_run_time.seconds) # this is what i want
#=> 2021-09-01 00:00:00 +0100

Is there any existing functionality that can get me this?

Related to this, I don't know what the difference is between previous_time and last_time ๐Ÿ˜… when I experiment they produce the same thing... almost.

[20] pry(main)> Time.at(j.previous_time.seconds)
=> 2021-09-02 02:42:32 902629/1048576 +0100
[21] pry(main)> Time.at(j.last_time.seconds)
=> 2021-09-02 02:42:32 924841/1048576 +0100

You seem to want #previous_time. #last_time means "last time it triggered". If the job triggered, then it is expected that the last trigger time is equal to the previous time.

OK, now I think I understand your question:

require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

j = scheduler.schedule_cron('* * 1 * *') {}

p j.next_time.to_s
  # => "2021-10-01 00:00:00 +0900"
p j.previous_time.to_s
  # => ""

This is a workaround:

require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

j = scheduler.schedule_cron('* * 1 * *') {}

p j.next_time.to_s
  # => "2021-10-01 00:00:00 +0900"

p j.previous_time.to_s
  # => ""
p j.cron_line.previous_time.to_s
  # => "2021-09-01 23:59:00 +0900"
jjb commented

๐Ÿคฉ thanks!

jjb commented

Do you know why it's 23:59:00 instead of 00:00:00?

@jjb asked:

Do you know why it's 23:59:00 instead of 00:00:00?

Yes, it's because "* * 1 * *" means every minute of every hour on the first of the month. The last of those is 23:59.

Please consider:

require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

p Time.now
  # => 2021-09-02 19:24:20 +0900


j = scheduler.schedule_cron('0 0 1 * *') {}

p j.cron_line.previous_time.to_s
  # => "2021-09-01 00:00:00 +0900"

j = scheduler.schedule_cron('* * 1 * *') {}

p j.cron_line.previous_time.to_s
  # => "2021-09-01 23:59:00 +0900"
jjb commented

Ahh ๐Ÿ˜† ๐Ÿคฆ sorry, indeed I thought i was doing 0 0 1 * *. thanks for all your help! i hope to do something cool with this in the near/medium future, where jobs can "catch up"' if they were missed during e.g. a restart

jjb commented

discovered this new gem for calculating this sort of thing: https://github.com/mizinsky/cron_calc

@jjb OK, thanks for the link, I read Ruby Weekly as well.

Now, what has it to do with the closed issue here?

jjb commented

This is a discussion about doing the thing that cron_calc is purpose-built to do, so I thought it is interesting and relevant for future finders of this thread, and maybe you ๐Ÿ™‚

Rufus-scheduler uses fugit.