close DB on exit, this MAY cause SLIGHT GC badness

This commit is contained in:
osmarks 2021-01-28 20:23:32 +00:00
parent 013c072bd7
commit c1138fba8d
2 changed files with 14 additions and 8 deletions

View File

@ -1,6 +1,5 @@
import tiny_sqlite import tiny_sqlite
import options import options
import times
let migrations: seq[string] = @[ let migrations: seq[string] = @[
""" """

View File

@ -10,6 +10,7 @@ import sugar
import net import net
import sequtils import sequtils
import strformat import strformat
import std/exitprocs
import ./db import ./db
@ -21,10 +22,15 @@ migrate(database)
close(database) close(database)
var threadDB {.threadvar.}: Option[DbConn] var threadDB {.threadvar.}: Option[DbConn]
proc getDB(): DbConn = proc getDB(): DbConn {.gcsafe.} =
if isNone threadDB: if isNone threadDB:
let x = openDatabase("./monitoring.sqlite3") let x = openDatabase("./monitoring.sqlite3")
x.exec("PRAGMA journal_mode=WAL") x.exec("PRAGMA journal_mode=WAL")
proc closeDB() =
try: close(x)
except: discard
addExitProc(closeDB)
when declared(onThreadDestroy): onThreadDestroy(closeDB)
threadDB = some x threadDB = some x
get threadDB get threadDB
@ -36,11 +42,11 @@ proc fromDbValue(value: DbValue, T: typedesc[Time]): Time = timestampToTime(valu
type type
ResponseType = enum ResponseType = enum
rtHttpTeapot = -1
rtOk = 0 rtOk = 0
rtHttpError = 1 rtHttpError = 1
rtHttpTeapot = 2 rtTimeout = 2
rtTimeout = 3 rtFetchError = 3
rtFetchError = 4
Response = object Response = object
rtype: ResponseType rtype: ResponseType
latency: int64 # microseconds latency: int64 # microseconds
@ -52,7 +58,7 @@ type
uptimePercent: float uptimePercent: float
proc uptimeSince(sid: int, time: Time): float = proc uptimeSince(sid: int, time: Time): float =
let okPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ? AND (status = 0 OR status = 2)", sid), int) let okPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ? AND status <= 0", sid), int)
let totalPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ?", sid), int) let totalPings = fromDbValue(get getDB().value("SELECT COUNT(*) FROM reqs WHERE site = ?", sid), int)
okPings / totalPings okPings / totalPings
@ -66,10 +72,11 @@ proc fetchLatest(row: ResultRow): Option[SiteStatus] =
proc mainPage(): string = proc mainPage(): string =
let sites = getDB().all("SELECT * FROM sites ORDER BY sid").map(fetchLatest).filter(x => isSome x).map(x => get x) let sites = getDB().all("SELECT * FROM sites ORDER BY sid").map(fetchLatest).filter(x => isSome x).map(x => get x)
let up = sites.filter(x => (x.lastResponse == rtOk) or (x.lastResponse == rtHttpTeapot)).len() let up = sites.filter(x => int(x.lastResponse) <= 0).len()
let vnode = buildHtml(html()): let vnode = buildHtml(html()):
head: head:
meta(charset="utf8") meta(charset="utf8")
meta(http-equiv="refresh", content="60")
title: text &"{up}/{sites.len} up - OnStat" title: text &"{up}/{sites.len} up - OnStat"
style: text css style: text css
body: body:
@ -132,6 +139,6 @@ proc timerCallback(fd: AsyncFD): bool =
echo "Starting up" echo "Starting up"
asyncCheck pollTargets() asyncCheck pollTargets()
addTimer(5000, false, timerCallback) addTimer(30000, false, timerCallback)
var server = newAsyncHttpServer() var server = newAsyncHttpServer()
waitFor server.serve(Port(7800), onRequest) waitFor server.serve(Port(7800), onRequest)