feat!: let checkers and status page servers reach data only through the api
Only cmd/api connects to Postgres and Redis. Agents authenticate with tokens created by 'api agent create' and use /agent/v1: checkers long poll for jobs, which doubles as heartbeat, and report results in batches. Organizations manage monitors through /api/v1/org/monitors. BREAKING CHANGE: cmd/checker and cmd/statuspage no longer read UPTIME_DATABASE_URL or UPTIME_REDIS_*; they need UPTIME_API_URL and UPTIME_AGENT_TOKEN. UPTIME_CHECKER_REGION is replaced by the region bound to the agent token. Closes #10 Closes #11 Closes #12 Closes #13 Closes #49 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
56862dce84
commit
d9891bb050
23 files changed
+1361
-45
No files matched your search
+33
-7
@@ -1,11 +1,20 @@
|
||||
// Command checker executes uptime checks for one region and pulls data from
|
||||
// external sources. Any number of instances per region may run.
|
||||
// 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 (
|
||||
"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"
|
||||
)
|
||||
@@ -26,19 +35,36 @@ func run(log *slog.Logger, level *slog.LevelVar) error {
|
||||
return err
|
||||
}
|
||||
level.Set(cfg.LogLevel)
|
||||
if err := cfg.Common.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, stop := platform.SignalContext()
|
||||
defer stop()
|
||||
|
||||
log = log.With("region", cfg.Region)
|
||||
deps, err := platform.Connect(ctx, cfg.Common, log)
|
||||
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
|
||||
}
|
||||
defer deps.Close()
|
||||
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")
|
||||
|
||||
health := platform.NewHealth(deps.Probes())
|
||||
runner := checker.New(client, cfg.Concurrency, log)
|
||||
health := platform.NewHealth(map[string]platform.Probe{"api": runner.Healthy})
|
||||
r := platform.NewRouter(log)
|
||||
health.Register(r)
|
||||
|
||||
return platform.Serve(ctx, log, platform.NewHTTPServer(cfg.HealthListen, r), health, 0, cfg.ShutdownTimeout)
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user