473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Seeking ideas for a cron implementation

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
Aug 22 '08 #1
1 2273
On Aug 22, 1:30*pm, Karthik Gurusamy <kar1...@gmail.comwrote:
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
I only scanned your message, but maybe datetime.timedelta() will
help..
>>import datetime
now = datetime.datetime.now()
print now
2008-08-22 13:48:49.335225
>>day = datetime.timedelta(1)
print day
1 day, 0:00:00
>>print now + day
2008-08-23 13:48:49.335225

~Sean
Aug 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simonc | last post by:
I've been programming in assembler and C/C++ for a number of years, but I'm only just starting down the road of PHP & MYSQL. I have a couple of questions: (1) Before I start writing my own...
6
by: sherryonline | last post by:
i have a page and i need that page to be called every day at 12:00 that page is going to connect to the database fetch some emails and will start mailing them how i am goning to do this.
4
by: vagrantbrad | last post by:
I'm using python 2.4 running on Fedora Core 4. I have written a python program called ipscan.py that checks the external ip address of my cable internet connection, and on change, will update the...
3
by: the.natalie | last post by:
Hi. I am a newbie to mysql, cron, and shell scripting, so please bear with me. I have a script that is used for updating an image directory based on contents in a database. The script does the...
3
by: Jia Lu | last post by:
Hello all I see there are lots of flat db or db-like modules in the standard python modules. What about the keywords seeking speed of them ? (I want to put about 10000 articles with 10000...
5
by: jcor | last post by:
Hi, I'm trying to run my first cron job, probably it's missing something. I created a perl script just to test, it's called "teste" !/usr/bin/perl open(DATA,">>/home/joao/files/output"); @Now...
4
by: Phil | last post by:
I have a php script that queries some Oracle DB and outputs a single line of plain text with <brat the end for each query. This is Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.