70 lines
2.5 KiB
Go
70 lines
2.5 KiB
Go
// sd_notify.go wires the daemon into internal/sdnotify's implementation of
|
|
// systemd's sd_notify(3) protocol: one READY=1 when the run loop starts
|
|
// serving, and a WATCHDOG=1 heartbeat while the process is alive.
|
|
//
|
|
// The protocol client and the heartbeat sampler live in internal/sdnotify
|
|
// because archie-messaging, a separate process that cannot import this
|
|
// package, is the next process that must be able to announce itself.
|
|
// A run loop that stalls on a slow peer is reported by the run_loop readiness
|
|
// probe, never by withholding the heartbeat.
|
|
//
|
|
// Nothing here is fatal -- a process with no NOTIFY_SOCKET (a plain terminal,
|
|
// Docker, a user unit without WatchdogSec) sends nothing and serves exactly as
|
|
// before, and a socket that cannot be written to is logged and ignored.
|
|
//
|
|
// Consuming the states is the unit's business: READY=1 needs Type=notify, and
|
|
// the heartbeat needs WatchdogSec.
|
|
package archied
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/sdnotify"
|
|
)
|
|
|
|
// announceReady tells systemd the daemon has finished starting. It is called at
|
|
// the moment a process declares itself serving: the daemon's run loop (next to
|
|
// healthSurface.markServing), and the serve steps of archie-gateway and
|
|
// archie-state-store, which have health surfaces of their
|
|
// own and would otherwise hang a Type=notify unit until TimeoutStartSec while
|
|
// perfectly healthy.
|
|
func (b *boot) announceReady(ctx context.Context) {
|
|
sdnotify.Ready(ctx, b.log)
|
|
}
|
|
|
|
// startWatchdog arms the heartbeat. It does
|
|
// nothing unless a manager named a notify socket and asked for a watchdog
|
|
// (WATCHDOG_USEC), so every deployment without systemd is unaffected.
|
|
func (b *boot) startWatchdog(ctx context.Context) {
|
|
n := sdnotify.New(os.Getenv, b.log)
|
|
if !n.Enabled() {
|
|
return
|
|
}
|
|
interval, ok := sdnotify.Heartbeat(ctx, os.Getenv, os.Getpid(), b.log)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
sampler := sdnotify.Watchdog{
|
|
Interval: interval,
|
|
Beat: func(ctx context.Context) { n.Send(ctx, sdnotify.WatchdogState) },
|
|
}
|
|
b.log.InfoContext(ctx, "systemd watchdog armed",
|
|
"heartbeat", interval.String())
|
|
|
|
go func() {
|
|
if err := sampler.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {
|
|
b.log.ErrorContext(ctx, "systemd watchdog stopped", "err", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// loopCadence is the interval at which the run loop begins a poll pass. The
|
|
// shared interval is re-read so live polling changes are honoured.
|
|
func (b *boot) loopCadence() time.Duration {
|
|
return b.cfgHolder.Get().PollInterval.Std()
|
|
}
|