-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitor.py
executable file
·52 lines (41 loc) · 1.27 KB
/
monitor.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
#!/usr/bin/python
import os
import sys
import time
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))[:-12])
from coursemonitor.models import CourseMonitor
from coursepagemonitor.models import CoursePageMonitor
from common.utils import errorlog
from settings import DEBUG
SLEEP_INTERVAL = 600
LOG_FILE = 'log_monitor'
def log(s):
if DEBUG:
print s
if LOG_FILE:
log_file = open(LOG_FILE, 'a')
log_file.write(s + '\n')
log_file.close()
else:
print s
while True:
try:
log("Fetch all again...")
course_monitor_list = CourseMonitor.objects.all()
for course in course_monitor_list:
try:
course.fetch()
except Exception as e:
errorlog(e, course.ccn)
course_page_monitor_list = CoursePageMonitor.objects.all()
for course_page in course_page_monitor_list:
try:
course_page.fetch()
except Exception as e:
errorlog(e, course_page.url)
except Exception as e:
errorlog(e)
log("Sleep for %d second(s)..." % SLEEP_INTERVAL)
time.sleep(SLEEP_INTERVAL)