-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
executable file
·76 lines (57 loc) · 2.03 KB
/
service.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
from operations import Operations
from datetime import datetime
import sys
import time
def get_week():
# Start from Sunday so long operations that we do weekly start on
# Sundays
return int(datetime.now().strftime('%U'))
def get_day():
return int(datetime.now().strftime('%j'))
def service():
repeat_interval = 4 * 3600 # Run every 4 hours
hashes_frequency = 3 # Check hashes every 3 days
fail_sleep = 1800 # If failed, sleep for half an hour
# Avoid doing long operations when starting the service, postpone
# them to the next iteration
db_recreated = get_week()
hashes_checked = get_day()
last_vacuum = get_day() - 1
while True:
# Notice that we recreate each time a new instance: in this way
# we are sure that the Oauth session is refreshed at each run,
# which should resolve some problems that I encountered
# originally, when I created a single client before the while
o = Operations()
today = get_day()
this_week = get_week()
if last_vacuum != today:
o.db.vacuum()
last_vacuum = today
try:
if db_recreated != this_week:
o.populate_db()
db_recreated = this_week
except:
# You should think to something more clever
print('Something failed', sys.exc_info())
time.sleep(fail_sleep)
continue
check_hashes = (today - hashes_checked) > hashes_frequency
try:
o.compare_trees(check_hashes)
except:
# As above
print('Something failed', sys.exc_info())
time.sleep(fail_sleep)
continue
if check_hashes:
hashes_checked = today
o.db.commit()
# We could check the start time, but some operations, like
# database population are very slow. In that case, just
# wait the usual time.
time.sleep(repeat_interval)
if __name__ == '__main__':
service()