Documentation
¶
Overview ¶
Package power lets sqi-server put the machine a worker runs on to sleep or power it off, and asks a worker to wake a peer, on behalf of a capacity group.
The server sends a protocol.ControlMsg as a core-NATS request to worker.control.<workerID> and the worker answers with a protocol.ControlReplyMsg. Sleep and shutdown refuse unless the operator set worker.power.allow_remote_sleep, and always refuse while a task is running. Wake needs no opt-in: the worker only sends UDP broadcasts for a peer that is asleep and so cannot receive a command itself. Reregister needs none either: the server sends it to a worker it holds offline whose heartbeats still arrive, and the worker registers again unless it is retiring.
Leaving and coming back ¶
A worker that accepts a sleep or shutdown deregisters as protocol.DeregisterRetired and then runs the command. If the command fails the worker re-registers at once. After a sleep that succeeded it re-registers when WatchResume sees the machine wake, or 60 seconds after the command returned, whichever is first, so a sleep that never took effect cannot leave the worker out of the farm. A shutdown that succeeds ends the process; one whose command returned success but left the machine running re-registers 60 seconds after the command returned, as a sleep does.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Subscribe ¶
Subscribe subscribes to worker.control.<WorkerID> and answers each request as the package documentation describes. Like cancel.Subscribe it returns once the server has the subscription, so a worker that calls it before its first lease request never misses a command for it. ctx ends any wait the controller is in; it does not unsubscribe.
func WatchResume ¶
func WatchResume(ctx context.Context, interval time.Duration, now func() time.Time, onResume func())
WatchResume calls onResume each time the machine appears to have been suspended and woken, until ctx is done. It samples every interval (2 s in production) and compares how far the wall clock advanced with how far the monotonic clock did, both read from now.
The comparison works because the monotonic clock stops while the machine is suspended on Linux, macOS and Windows, while the wall clock is corrected on wake. An advance of the wall clock that outruns the monotonic one by more than 3 x interval + 5 s is a suspend; anything shorter is a scheduling stall or ordinary clock slew. A manual step of the wall clock forward by that much triggers it too, which only costs a harmless re-registration.
now must return times that carry a monotonic reading, as time.Now does: a time without one compares the wall clock with itself and never fires.
Types ¶
type Config ¶
type Config struct {
// AllowRemoteSleep permits the server to sleep or shut down the machine.
AllowRemoteSleep bool
// SleepCommand and ShutdownCommand are argv lists. Empty means the
// platform default, if there is one.
SleepCommand, ShutdownCommand []string
// RefuseWake refuses every wake request, for a worker that cannot send
// packets on its peers' behalf, such as a simulated one. The worker
// configuration does not set it.
RefuseWake bool
}
Config is the operator's power policy.
type Deps ¶
type Deps struct {
WorkerID string
// Busy returns the number of tasks the worker is running.
Busy func() int
// Deregister tells the server the worker is leaving.
Deregister func(reason string, kind protocol.DeregisterKind)
// Reregister registers the worker again.
Reregister func(ctx context.Context) error
// Run runs a command to completion. The default runs it with os/exec.
Run func(ctx context.Context, argv []string) error
// SendWake sends magic packets for macs to targets. The default is
// [wol.Send] with its default burst and gap.
SendWake func(ctx context.Context, macs []net.HardwareAddr, targets []string) error
// Now returns the time. The default is [time.Now].
Now func() time.Time
Logger *slog.Logger
}
Deps are the worker's parts the controller drives. Busy, Deregister and Reregister are required; the rest default to the real thing.