66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
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 func() {
|
|
if err := app.Close(); err != nil {
|
|
slog.Warn("app_close_failed", "error", err)
|
|
}
|
|
}()
|
|
|
|
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())
|
|
}
|