Move monitoring PF infrastructure to Go
This commit is contained in:
61
cmd/scheduler/main.go
Normal file
61
cmd/scheduler/main.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"monitoring-pf/internal/pf"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := pf.LoadConfig()
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
app, err := pf.OpenApp(ctx, cfg)
|
||||
if err != nil {
|
||||
slog.Error("db_open_failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer app.Close()
|
||||
|
||||
interval := cfg.SchedulerInterval()
|
||||
slog.Info("monitoring_pf_scheduler_started", "interval", interval.String())
|
||||
timer := time.NewTimer(interval)
|
||||
defer timer.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
slog.Info("monitoring_pf_scheduler_stopped")
|
||||
return
|
||||
case <-timer.C:
|
||||
run(ctx, app)
|
||||
timer.Reset(interval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context, app *pf.App) {
|
||||
start := time.Now()
|
||||
slog.Info("scheduled_scan_starting")
|
||||
summary, err := app.Worker.CheckAll(ctx)
|
||||
if err != nil {
|
||||
slog.Error("scheduled_scan_failed", "error", err)
|
||||
return
|
||||
}
|
||||
total := 0
|
||||
for _, changes := range summary {
|
||||
if changes > 0 {
|
||||
total += changes
|
||||
}
|
||||
}
|
||||
slog.Info("scheduled_scan_done", "projects", len(summary), "total_changes", total, "elapsed", time.Since(start).String())
|
||||
}
|
||||
Reference in New Issue
Block a user