Update
This commit is contained in:
parent
d37503c863
commit
1509789cde
19 changed files with 1534 additions and 112 deletions
63
main.go
63
main.go
|
|
@ -16,6 +16,7 @@ import (
|
|||
"git.gsdistance.org/tyuXX/RUD/internal/checker"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/config"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/events"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/history"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/server"
|
||||
"git.gsdistance.org/tyuXX/RUD/internal/store"
|
||||
)
|
||||
|
|
@ -67,11 +68,62 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
// Long-term history: daily JSONL logs (zstd-compressed on day rollover) and
|
||||
// the SQLite aggregate stats database used by the /monitor page.
|
||||
monitorMeta := func(id string) history.MonitorMeta {
|
||||
for _, m := range cfg.Monitors {
|
||||
if m.ID == id {
|
||||
return history.MonitorMeta{Name: m.Name, Method: m.Method, Target: m.DisplayTarget()}
|
||||
}
|
||||
}
|
||||
return history.MonitorMeta{}
|
||||
}
|
||||
|
||||
var dailyLog *history.DailyLogger
|
||||
if dir := cfg.Storage.LogsDir; dir != "" && dir != "off" && dir != "none" {
|
||||
l, err := history.NewDailyLogger(dir, monitorMeta)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not initialize daily history log in %s: %v", dir, err)
|
||||
} else {
|
||||
dailyLog = l
|
||||
log.Printf("Daily history logging enabled (%s)", dir)
|
||||
}
|
||||
}
|
||||
|
||||
var statsDB *history.StatsDB
|
||||
if path := cfg.Storage.StatsDB; path != "" && path != "off" && path != "none" {
|
||||
db, err := history.NewStatsDB(path, cfg.Storage.StatsRetentionDays)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not initialize stats database %s: %v", path, err)
|
||||
} else {
|
||||
statsDB = db
|
||||
log.Printf("Stats database enabled (%s, retention %d days)", path, cfg.Storage.StatsRetentionDays)
|
||||
}
|
||||
}
|
||||
|
||||
if dailyLog != nil || statsDB != nil {
|
||||
st.SetOnRecord(func(monitorID string, res store.CheckResult) {
|
||||
if dailyLog != nil {
|
||||
if err := dailyLog.Record(monitorID, res); err != nil {
|
||||
log.Printf("history log write error: %v", err)
|
||||
}
|
||||
}
|
||||
if statsDB != nil {
|
||||
if err := statsDB.Record(monitorID, res); err != nil {
|
||||
log.Printf("stats db write error: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
evEngine := events.NewEngine(cfg.Events)
|
||||
srvHandler := server.New(cfg, st)
|
||||
if statsDB != nil {
|
||||
srvHandler.SetStats(statsDB)
|
||||
}
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: cfg.Server.Listen,
|
||||
|
|
@ -126,6 +178,17 @@ func main() {
|
|||
sched.Wait()
|
||||
evEngine.Wait()
|
||||
|
||||
if dailyLog != nil {
|
||||
if err := dailyLog.Close(); err != nil {
|
||||
log.Printf("Error closing history log: %v", err)
|
||||
}
|
||||
}
|
||||
if statsDB != nil {
|
||||
if err := statsDB.Close(); err != nil {
|
||||
log.Printf("Error closing stats database: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.Storage.PersistenceFile != "" {
|
||||
if err := st.SaveToDisk(); err != nil {
|
||||
log.Printf("Error saving final state to disk: %v", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue