Rufus::CronLine.last_time() method

32 views
Skip to first unread message

Matteo Cerutti

unread,
May 27, 2013, 5:25:14 PM5/27/13
to rufus...@googlegroups.com
Hi all,

I quickly wanted a way of finding out the last cron that should've triggered in the past. I re-used the next_time() method to achieve this, however not sure if I may be missing something here.

Below the code I've used:

Module Rufus
Class CronLine
def last_time(now=Time.now)

      time = @timezone ? @timezone.utc_to_local(now.getutc) : now

      time = time - time.usec * 1e-6 + 1
        # little adjustment before starting

      loop do

        unless sub_match?(time, :sec, @seconds)
          time -= 1
          next
        end
        unless sub_match?(time, :min, @minutes)
          time -= 60 - time.sec
          next
        end
        unless sub_match?(time, :hour, @hours)
          time -= (60 - time.min) * 60 - time.sec
          next
        end
        unless date_match?(time)
          time -= (24 - time.hour) * 3600 - time.min * 60 - time.sec
          next
        end

        break
      end

      if @timezone
        time = @timezone.local_to_utc(time)
        time = time.getlocal unless now.utc?
      end
end
end


Would be nice to have this in the original CronLine class :)

Cheers,
Matteo

John Mettraux

unread,
May 27, 2013, 5:49:50 PM5/27/13
to rufus...@googlegroups.com

On Mon, May 27, 2013 at 02:25:14PM -0700, Matteo Cerutti wrote:
>
> I quickly wanted a way of finding out the last cron that should've
> triggered in the past. I re-used the next_time() method to achieve this,
> however not sure if I may be missing something here.
>
> Below the code I've used:
>
> Module Rufus
> Class CronLine
> def last_time(now=Time.now)
>
> (...)
> end
> end
>
> Would be nice to have this in the original CronLine class :)

Hello Matteo,

welcome to the rufus mailing list.

OK, I'll try and add it. I think I'll simply add an optional "direction"
parameter to next_time to specify the past as the direction.

https://github.com/jmettraux/rufus-scheduler/issues/55

By the way, would that help as well?

https://github.com/jmettraux/rufus-scheduler/blob/624eddd1d501adbf603356e5ca07698e64150ad5/lib/rufus/sc/jobs.rb#L55-L64

It's useful for "every" and "cron" jobs.


Best regards,

--
John Mettraux - http://lambda.io/jmettraux

Matteo Cerutti

unread,
May 28, 2013, 2:11:21 AM5/28/13
to rufus...@googlegroups.com
Hi John,

Thanks for the reply.

Sounds good the idea of passing the direction to next_time :)

Regarding the last attribute, it is helpful to some extent as that tells when a scheduled job was last triggered. However, that's not good enough if I just want to parse a cron line and understand when that should've last triggered in the past. That's why this extended version of next_time? would help here.

Keep the good work up.

Cheers,
Matteo

John Mettraux

unread,
May 30, 2013, 6:28:27 PM5/30/13
to rufus...@googlegroups.com
Hello Matteo,

I've integrated a first version of last_time, although I named it
"previous_time".

It works OK

https://github.com/jmettraux/rufus-scheduler/blob/59d3b8903234c0c91d5a6852677d4b498d3c48e5/spec/cronline_spec.rb#L284-L296

I noticed that your implementation returns different results:

* previous_time:

pt('* * * * sun', lo(1970, 1, 1)).should == lo(1969, 12, 28, 23, 59, 00)

* last_time:

pt('* * * * sun', lo(1970, 1, 1)).should == lo(1969, 12, 28, 00, 00, 00)

I think that this cron "last time" should be at 23:59:00.

I'm still not happy with the performance of previous_time as it is now. I'd
prefer have something similar to what you did (inverse next_time).

I'll try harder in the coming days.


Many thanks,

Matteo Cerutti

unread,
Jun 1, 2013, 3:26:26 PM6/1/13
to rufus...@googlegroups.com
Hi John,

That's really good as a start and indeed there's definitely something wrong with last_time. It should be 23:59:00 indeed.

I'll be eagerly waiting to see the the final previous_time :)

Have a great weekend.

Thanks,
Matteo

John Mettraux

unread,
Jun 1, 2013, 6:06:13 PM6/1/13
to rufus...@googlegroups.com

On Sat, Jun 01, 2013 at 12:26:26PM -0700, Matteo Cerutti wrote:
>
> That's really good as a start and indeed there's definitely something wrong with last_time. It should be 23:59:00 indeed.
>
> I'll be eagerly waiting to see the the final previous_time :)

Hello Matteo,

not sure if it's the final version, at least it has a decent speed for
average cases: I pushed a version that searches back by slices of two hours.

https://github.com/jmettraux/rufus-scheduler/commit/6a63847c73c0b289a6149c64b21a7366d8315df5

Should be OK for now.

You too have a grand week-end!

John

Reply all
Reply to author
Forward
0 new messages