ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m23s
ci / test (pull_request) Successful in 12m59s
ci / build (pull_request) Canceled after 7m17s
ci / image (pull_request) Canceled after 0s
The API serves Prometheus metrics on a listener of its own (UPTIME_API_METRICS_LISTEN, default :9090, "off" disables it), so the public ingress never exposes them: - requests by route and status, with durations except for long polls and agent streams - dispatched checks, stored and rejected results per region, how late results arrive, live checkers and overdue checks per platform region - notification and subscriber mail deliveries, incidents opened and resolved - runs, duration and last success of the background jobs - platform totals (orgs, users, monitors by type, status pages, subscribers, open incidents, pending notifications), read at most every 15 seconds when scraped - the Postgres and Redis connection pools, Go runtime and build info The Helm chart exposes the port on the API service and can add a ServiceMonitor for the Prometheus Operator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
120 lines
3.3 KiB
Go
120 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
// orgPurgeEvery is how often an instance looks for deleted organizations
|
|
// whose check data is left, besides right after a deletion.
|
|
const orgPurgeEvery = time.Minute
|
|
|
|
// purgeOrg deletes an organization with everything in it at once, except
|
|
// its check results and aggregates: they carry no foreign key and can run
|
|
// to gigabytes, more than a request can delete in time, so it queues them
|
|
// for PurgeDeletedOrgs. Nothing shows them once the organization is gone.
|
|
func purgeOrg(ctx context.Context, q *store.Queries, org uuid.UUID) error {
|
|
if err := q.QueueOrgPurge(ctx, org); err != nil {
|
|
return err
|
|
}
|
|
if err := q.DeleteOrgStorage(ctx, org); err != nil {
|
|
return err
|
|
}
|
|
_, err := q.DeleteOrg(ctx, org)
|
|
return err
|
|
}
|
|
|
|
// wakeOrgPurges has PurgeDeletedOrgs run soon after a deletion instead of
|
|
// at the next tick.
|
|
func (s *Server) wakeOrgPurges() {
|
|
select {
|
|
case s.orgPurges <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// runOrgPurges purges the check data of deleted organizations every
|
|
// orgPurgeEvery and when woken, until ctx ends.
|
|
func (s *Server) runOrgPurges(ctx context.Context) {
|
|
tick := time.NewTicker(orgPurgeEvery)
|
|
defer tick.Stop()
|
|
for {
|
|
if err := s.metrics.Job("org_purge", func() error { return s.PurgeDeletedOrgs(ctx) }); err != nil && ctx.Err() == nil {
|
|
s.log.ErrorContext(ctx, "purging deleted organizations failed", "error", err)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-tick.C:
|
|
case <-s.orgPurges:
|
|
}
|
|
}
|
|
}
|
|
|
|
// PurgeDeletedOrgs deletes the check data of deleted organizations a chunk
|
|
// at a time, each in a transaction of its own, until none is left. An
|
|
// instance leases an organization while it purges it; if it stops, another
|
|
// takes over once the lease runs out and repeats the chunks, which then
|
|
// delete nothing.
|
|
func (s *Server) PurgeDeletedOrgs(ctx context.Context) error {
|
|
for ctx.Err() == nil {
|
|
var org uuid.UUID
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
org, err = q.ClaimOrgPurge(ctx)
|
|
return err
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := s.purgeOrgCheckData(ctx, org); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return ctx.Err()
|
|
}
|
|
|
|
func (s *Server) purgeOrgCheckData(ctx context.Context, org uuid.UUID) error {
|
|
start := time.Now()
|
|
var chunks []string
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
chunks, err = q.OrgCheckDataChunks(ctx)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var rows int64
|
|
for _, chunk := range chunks {
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
n, err := q.PurgeOrgCheckChunk(ctx, store.PurgeOrgCheckChunkParams{OrgID: org, Chunk: chunk})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rows += n
|
|
return q.ExtendOrgPurge(ctx, org)
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { return q.FinishOrgPurge(ctx, org) })
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.log.InfoContext(ctx, "purged check data of deleted organization",
|
|
"org", org, "rows", rows, "chunks", len(chunks), "duration", time.Since(start).Round(time.Millisecond))
|
|
return nil
|
|
}
|