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

83
main.go
View File

@ -18,12 +18,7 @@ import (
var db *qbdb.DB var db *qbdb.DB
func main() { func main() {
defer func() { defer recoverFunc()
v := recover()
if v != nil {
flog.Critical(`panic: `, v)
}
}()
setLogger() setLogger()
flog.Info(`Starting`) flog.Info(`Starting`)
@ -31,41 +26,59 @@ func main() {
initDB() initDB()
start := GetStartDate()
for { for {
end := start.Add(time.Hour) fetchData()
}
}
data := GetFortopData(start, end) func recoverFunc() {
ranges := map[string][]Range{} v := recover()
for _, v := range data { if v != nil {
ranges[v.Naam] = v.Range flog.Critical(`panic: `, v)
} }
}
sets := Sets{} var start time.Time
for k, v := range ranges {
for _, r := range v {
date := time.Time(r.Date)
set, ok := sets[date]
if !ok {
sets[date] = Set{}
set = sets[date]
}
set[k] = r.Value func fetchData() {
} defer recoverFunc()
}
insertSets(sets)
if end.Before(time.Now()) {
start = end.Add(time.Second)
continue
}
time.Sleep(time.Minute)
if start.IsZero() {
start = GetStartDate() start = GetStartDate()
} }
end := start.Add(time.Hour)
data := GetFortopData(start, end)
ranges := map[string][]Range{}
for _, v := range data {
ranges[v.Naam] = v.Range
}
sets := Sets{}
for k, v := range ranges {
for _, r := range v {
date := time.Time(r.Date)
set, ok := sets[date]
if !ok {
sets[date] = Set{}
set = sets[date]
}
set[k] = r.Value
}
}
insertSets(sets)
if end.Before(time.Now()) {
start = end.Add(time.Second)
return
}
time.Sleep(time.Minute)
start = GetStartDate()
} }
// GetStartDate gets the last measurement date // GetStartDate gets the last measurement date
@ -75,7 +88,7 @@ func GetStartDate() time.Time {
q := m.Select(qf.Max(m.Time)) q := m.Select(qf.Max(m.Time))
err := db.QueryRow(q).Scan(&t) 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) return time.Date(2018, 4, 1, 0, 0, 0, 0, time.Local)
} }
if err != nil { if err != nil {