/
taildb.py
60 lines (52 loc) · 1.73 KB
/
taildb.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
import MySQLdb
import loaddb
import time
import crawl_utils
import sys
import logging
from logging import debug, info, warn, error
# Can run as a daemon and tail a number of logfiles and milestones and
# update the db.
def interval_work(cursor, interval, master):
master.tail_all(cursor)
def tail_logfiles(logs, milestones, interval):
db = loaddb.connect_db()
loaddb.init_listeners(db)
cursor = db.cursor()
loaddb.support_mysql57(cursor)
loaddb.set_active_cursor(cursor)
elapsed_time = 0
master = loaddb.create_master_reader()
try:
while True:
try:
interval_work(cursor, interval, master)
if not interval:
break
loaddb.run_timers(cursor, elapsed_time)
except IOError as e:
error("IOError: %s" % e)
info("All updates completed. Sleeping for {} seconds.".format(interval))
time.sleep(interval)
elapsed_time += interval
if crawl_utils.taildb_stop_requested():
info("Exit due to taildb stop request.")
break
finally:
loaddb.set_active_cursor(None)
cursor.close()
loaddb.cleanup_listeners(db)
db.close()
info("Bye!")
if __name__ == '__main__':
if crawl_utils.taildb_stop_requested():
print("""The taildb sentinel %s exists. The Nemelex' Choice script may be active."""
% crawl_utils.TAILDB_STOP_REQUEST_FILE)
print ("""If you're sure it is not, please remove the file and restart taildb.py""")
sys.exit(1)
logging.basicConfig(level=logging.DEBUG,
format=crawl_utils.LOGFORMAT,
filename = (crawl_utils.BASEDIR + '/taildb.log'))
loaddb.load_extensions()
crawl_utils.daemonize()
tail_logfiles(loaddb.LOGS, loaddb.MILESTONES, crawl_utils.UPDATE_INTERVAL)