-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvacuum.py
48 lines (37 loc) · 1.3 KB
/
vacuum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
from __future__ import print_function
import sys
import time
from pred.config import parse_config, CONFIG_FILENAME
from webserver import create_db_connection
from pred.webserver.customjob import CustomJob
from pred.webserver.sequencelist import SequenceList
DELETE_AFTER_HOURS = "48"
BASE_SQL = "delete from custom_list where current_timestamp - uploaded > interval '{} hours'"
DELETE_OLD_LISTS_SQL = BASE_SQL.format(DELETE_AFTER_HOURS)
def connect_and_delete_old_lists():
config = parse_config(CONFIG_FILENAME)
db = create_db_connection(config.dbconfig)
cur = db.cursor()
delete_old_items(cur)
cur.close()
db.commit()
db.close()
def delete_old_items(cur):
print("Deleting old custom lists.")
cur.execute(DELETE_OLD_LISTS_SQL, [])
print("Deleting old jobs.")
CustomJob.delete_old_jobs(cur, DELETE_AFTER_HOURS)
print("Deleting old custom sequences.")
SequenceList.delete_old_and_unattached(cur, DELETE_AFTER_HOURS)
def delete_and_wait(wait_minutes):
while True:
print("Waiting {} minutes.".format(wait_minutes))
time.sleep(wait_minutes * 60)
connect_and_delete_old_lists()
if __name__ == '__main__':
args = sys.argv[1:]
if args:
delete_and_wait(int(args[0]))
else:
connect_and_delete_old_lists()