-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstatusDbManager.py
96 lines (66 loc) · 2.66 KB
/
statusDbManager.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pylint: disable-msg=F0401, W0142
from settings import settings
import logging
import sqlite3
import traceback
class StatusResource(object):
log = logging.getLogger("Main.StatusMgr")
def __init__(self):
self.dbPath = settings["dbPath"]
self.openDB()
def __del__(self):
self.closeDB()
def checkInitStatusDatabase(self):
cur = self.conn.cursor()
ret = cur.execute('''SELECT name FROM sqlite_master WHERE type='table';''')
rets = ret.fetchall()
tables = [item for sublist in rets for item in sublist]
if not rets or not "statusDb" in tables: # If the DB doesn't exist, set it up.
self.log.info("Need to setup initial suceeded page database....")
self.conn.execute('''CREATE TABLE statusDb (siteName text NOT NULL,
sectionName text NOT NULL,
statusText text,
UNIQUE(siteName, sectionName) ON CONFLICT REPLACE)''')
self.conn.execute('''CREATE INDEX IF NOT EXISTS statusDb_site_section_index ON statusDb (siteName, sectionName)''')
self.conn.commit()
self.log.info("Status database created")
def openDB(self):
self.log.info("StatusManager Opening DB...")
self.log.info("DB Path = %s", self.dbPath)
self.conn = sqlite3.connect(self.dbPath, check_same_thread=False)
self.log.info("DB opened")
self.log.info("DB opened. Activating 'wal' mode")
rets = self.conn.execute('''PRAGMA journal_mode=wal;''')
# rets = self.conn.execute('''PRAGMA locking_mode=EXCLUSIVE;''')
rets = rets.fetchall()
self.log.info("PRAGMA return value = %s", rets)
self.checkInitStatusDatabase()
def updateNextRunTime(self, name, timestamp):
cur = self.conn.cursor()
cur.execute('''INSERT INTO statusDb (siteName, sectionName, statusText) VALUES (?, 'nextRun', ?);''', (name, timestamp))
self.conn.commit()
def updateLastRunStartTime(self, name, timestamp):
cur = self.conn.cursor()
cur.execute('''INSERT INTO statusDb (siteName, sectionName, statusText) VALUES (?, 'prevRun', ?);''', (name, timestamp))
self.conn.commit()
def updateLastRunDuration(self, name, timeDelta):
cur = self.conn.cursor()
cur.execute('''INSERT INTO statusDb (siteName, sectionName, statusText) VALUES (?, 'prevRunTime', ?);''', (name, timeDelta))
self.conn.commit()
def updateRunningStatus(self, name, state):
cur = self.conn.cursor()
cur.execute('''INSERT INTO statusDb (siteName, sectionName, statusText) VALUES (?, 'isRunning', ?);''', (name, state))
self.conn.commit()
def closeDB(self):
self.log.info("Closing DB...",)
try:
self.conn.close()
except:
self.log.error("wat")
self.log.error(traceback.format_exc())
self.log.info("done")
def go():
wat = StatusResource()
print(wat)
if __name__ == "__main__":
go()