Improve panic handling

This commit is contained in:
Nise Void 2018-05-28 13:45:26 +02:00
parent aa1b88a8f3
commit 810992c368
Signed by: NiseVoid
GPG Key ID: FBA14AC83EA602F3

33
main.go
View File

@ -18,12 +18,7 @@ import (
var db *qbdb.DB
func main() {
defer func() {
v := recover()
if v != nil {
flog.Critical(`panic: `, v)
}
}()
defer recoverFunc()
setLogger()
flog.Info(`Starting`)
@ -31,8 +26,27 @@ func main() {
initDB()
start := GetStartDate()
for {
fetchData()
}
}
func recoverFunc() {
v := recover()
if v != nil {
flog.Critical(`panic: `, v)
}
}
var start time.Time
func fetchData() {
defer recoverFunc()
if start.IsZero() {
start = GetStartDate()
}
end := start.Add(time.Hour)
data := GetFortopData(start, end)
@ -59,14 +73,13 @@ func main() {
if end.Before(time.Now()) {
start = end.Add(time.Second)
continue
return
}
time.Sleep(time.Minute)
start = GetStartDate()
}
}
// GetStartDate gets the last measurement date
func GetStartDate() time.Time {
@ -75,7 +88,7 @@ func GetStartDate() time.Time {
q := m.Select(qf.Max(m.Time))
err := db.QueryRow(q).Scan(&t)
if t == nil || err == sql.ErrNoRows {
if (err == nil && t == nil) || err == sql.ErrNoRows {
return time.Date(2018, 4, 1, 0, 0, 0, 0, time.Local)
}
if err != nil {