I'm currently using python to loop through heavy processing tasks on tables in our database. In order to be friendly to other users I want this processing to only happen during nocturnal hours. When the early risers start using the database, I want to be able to program the premature termination of this script.
I'm aware of interrupting cow so I could have something like this:
from interruptingcow import timeout
from dbsettings import dbsetting
con = connect(database=dbset['database'],
host=dbset['host'],
user=dbset['user'],
password=dbset['password'])
cursor = con.cursor()
YEARS = {"2014":range(1, 13),
"2015":range(1, 13)}
def update_table(yyyymm, con, cursor)
table = 'public.tablename'+yyyymm
cursor.execute('Do something on {table}'.format(table))
con.commit()
try:
with timeout(57600, exception=RuntimeError):
for year in YEARS:
for month in years[year]:
yyyymm = get_yyyymm(year, month)
update_table(yyyymm, con, cursor)
except RuntimeError:
print "It's morning, killing database processing"
But is this a best practice? Is there a better way?