ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m29s
ci / test (pull_request) Successful in 12m55s
ci / build (pull_request) Successful in 16m45s
ci / image (pull_request) Successful in 5m41s
Checkers and status page servers keep one WebSocket open to the API at /agent/v1/stream and send their requests over it. The API answers them through the agent API as if they had come over HTTP, each authenticated again, so the protocol stays the same and a revoked token ends the stream. While the stream is down agents fall back to plain HTTP, and an API without it is asked again every five minutes. Over the stream the API tells status page servers which of their pages changed, so they fetch them at once instead of on their next refresh: when a region of a monitor changes between up and down, an incident opens or resolves, or anything of the organization is edited. Changes are announced through Redis to every API instance and sent at most every five seconds per stream. A page that changes while it is fetched is fetched once more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
// Command checker executes uptime checks for one region. It talks only to
|
|
// the API, never to the database or Redis, so it can run anywhere the API is
|
|
// reachable, including customer networks. Any number of instances may share
|
|
// a region and token; each check goes to a randomly chosen live instance.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/buildinfo"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/checker"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/platform"
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) == 2 && os.Args[1] == "version" {
|
|
fmt.Println(buildinfo.Version)
|
|
return
|
|
}
|
|
if len(os.Args) == 2 && os.Args[1] == "health" {
|
|
if err := health(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
log, level := platform.NewLogger("checker")
|
|
// Route the standard log package and dependencies through the same handler.
|
|
slog.SetDefault(log)
|
|
if err := run(log, level); err != nil {
|
|
log.Error("exit", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run(log *slog.Logger, level *slog.LevelVar) error {
|
|
cfg, err := config.Load[config.Checker]()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
level.Set(cfg.LogLevel)
|
|
if err := cfg.Validate(); err != nil {
|
|
return err
|
|
}
|
|
ctx, stop := platform.SignalContext()
|
|
defer stop()
|
|
|
|
instance := uuid.New()
|
|
client := agentapi.NewClient(cfg.APIURL, cfg.Token, instance, buildinfo.Version)
|
|
hello, err := platform.Hello(ctx, client, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hello.Kind != "checker" {
|
|
return fmt.Errorf("token belongs to a %s agent, not a checker", hello.Kind)
|
|
}
|
|
log = log.With("region", hello.Region, "agent", hello.Name, "instance", instance)
|
|
log.Info("connected to api")
|
|
|
|
client.UseStream(ctx, agentapi.StreamOptions{Log: log})
|
|
runner := checker.New(client, cfg.Concurrency, log)
|
|
health := platform.NewHealth(map[string]platform.Probe{"api": runner.Healthy})
|
|
r := platform.NewRouter(log)
|
|
health.Register(r)
|
|
|
|
serveErr := make(chan error, 1)
|
|
go func() {
|
|
serveErr <- platform.Serve(ctx, log, platform.NewHTTPServer(cfg.HealthListen, r), health, 0, cfg.ShutdownTimeout)
|
|
}()
|
|
runErr := runner.Run(ctx, cfg.ShutdownTimeout)
|
|
if errors.Is(runErr, agentapi.ErrUnauthorized) {
|
|
stop()
|
|
}
|
|
return errors.Join(runErr, <-serveErr)
|
|
}
|
|
|
|
// health probes the running checker in this container.
|
|
func health() error {
|
|
cfg, err := config.Load[config.Checker]()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return platform.ProbeLocal(context.Background(), cfg.HealthListen, "/healthz")
|
|
}
|