Skip to content

Commit

Permalink
Close #91. Fix checkrr not recovering cleanly from any SIGKILL or SIG…
Browse files Browse the repository at this point in the history
…SEGV calls
  • Loading branch information
aetaric committed Oct 8, 2024
1 parent 1d130af commit 58827e0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2022 aetaric <[email protected]>
package main

import (
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -12,6 +13,7 @@ import (
"syscall"

"github.com/aetaric/checkrr/check"
"github.com/aetaric/checkrr/features"
"github.com/aetaric/checkrr/webserver"
"github.com/common-nighthawk/go-figure"
"github.com/robfig/cron/v3"
Expand Down Expand Up @@ -129,13 +131,42 @@ func main() {
return nil
})

testRunning := false
statsCleanup := features.Stats{}

DB.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("Checkrr-stats"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}

b := tx.Bucket([]byte("Checkrr-stats"))
statdata := b.Get([]byte("current-stats"))
json.Unmarshal(statdata, &statsCleanup)

if statsCleanup.Running {
log.WithFields(log.Fields{"startup": true}).Warn("Cleaing up previous crash or improper termination of checkrr.")
statsCleanup.Running = false
testRunning = true
}

return nil
})

if testRunning {
err := DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Checkrr-stats"))
json, er := json.Marshal(statsCleanup)
if er != nil {
return er
}
err := b.Put([]byte("current-stats"), json)
return err
})
if err != nil {
log.WithFields(log.Fields{"Module": "Stats", "DB Update": "Failure"}).Warnf("Error: %v", err.Error())
}
}
} else {
log.WithFields(log.Fields{"startup": true}).Fatal("Database file path missing or unset, please check your config file.")
}
Expand Down

0 comments on commit 58827e0

Please sign in to comment.