Seeking ideas for a cron implementation

Karthik Gurusamy kar1107 at gmail.com
Fri Aug 22 16:30:33 EDT 2008


Hi,

I'm working on a cron like functionality for my application.
The outer loops runs continuously waking every x seconds (say x=180,
300, ..).
It needs to know what events in cron has expired and for each event do
the work needed.

It's basically like unix cron or like a calendar application with some
restrictions. The outer loop may come back a lot later and many events
might have missed their schedule -- but this is okay.. We don't have
to worry about missed events (if there were n misses, we just need to
execute call back once).

Let's take some examples [Let e denotes an event]
e1: hour=1  min=30                             # Run every day once at
1:30 AM
e2: wday=0, hour=1  min=0                   # run every Monday at 1 AM
e3: month=10, day=10, hour=10 min=0  # run on October 10th, 10 AM
every year

class Cron_Event (object):
    def __init__ (year=None, month=None, day=None, hour=None ..etc)
      #  do init

class Cron (object):
    def __init__ ():
        # do init
    def event_add (e):
        # add an event
    def execute()
        # see if any events has "expired" .. call it's callback
        # I'm looking for ideas on how to manage the events here

>From outer loop
cron = Cron()
# create various events like
e1 = Cron_Event(hour=1)
cron.event_add(e1)
e2 = Cron_Event(wday=0, hour=1)
cron.event_add(e2)

while True:
    sleep x seconds (or wait until woken up)
    cron.execute()
    # do other work.. x may change here

If I can restrict to hour and minute, it seems manageable as the
interval between two occurrences is a constant. But allowing days like
every Monday or 1st of every month makes things complicated. Moreover
I would like each constraint in e to take on multiple possibilities
(like every day at 1AM,  2 AM and 4 AM do this).

I'm looking for solutions that can leverage datetime.datetime
routines.
My current ideas include for each e, track the next time it will fire
(in seconds since epoch as given by time.time()). Once current time
has passed that time, we execute the event. e.g.
>>> datetime.datetime.now()
datetime.datetime(2008, 8, 22, 13, 19, 54, 5567)
>>> time.time()
1219436401.741966    <--- compute event's next firing in a format like
this
>>>

The problem seems to be how to compute that future point in time (in
seconds since epoch)  for a generic Cron_Event.

Say how do I know the exact time in future  that will satisfy a
constraint like:
 month=11, wday=1, hour=3, min=30    # At 3:30 AM on a Tuesday in
November

Thanks for your thoughts.

Karthik



More information about the Python-list mailing list