Documentation
¶
Overview ¶
Package heartbeat writes a small last-run.json "I started" breadcrumb to the install dir at the very top of a telemetry run — before the enterprise-config gate and before the singleton lock is acquired.
Why this exists, separate from agent.error.log and scan-state.json: those only appear once a run gets far enough to log a line or finish an upload. Several failure modes never reach that point — a process killed mid-startup (e.g. the Windows GUI-launcher teardown), a run that fails the enterprise gate, a lock it can never acquire. The heartbeat captures "this binary started at time T, pid P, triggered by X" independent of any of that, so a stale file means "the agent isn't being invoked at all" (scheduler not firing — battery policy, missing task) while a fresh file alongside missing server-side telemetry means "the agent runs but dies/fails before upload."
The write is durable against the abrupt termination it is meant to record: marshal to a temp sibling, fsync, then atomically rename over last-run.json (same pattern as internal/state). A kill at any point leaves either the previous heartbeat or the new one — never a truncated file.
Index ¶
Constants ¶
const Filename = "last-run.json"
Filename is the basename written into the install dir. Exported so callers and diagnostics can reference it without duplicating the literal.
const SchemaVersion = 1
SchemaVersion is the on-disk format version for last-run.json. Bump when the Record shape changes incompatibly; readers treat a mismatch as "no usable heartbeat" rather than failing.
Variables ¶
This section is empty.
Functions ¶
func UpdateRunGate ¶ added in v1.15.0
UpdateRunGate applies a read-modify-write to the RunGate cache in the file at path, preserving the breadcrumb fields and any RunGate fields the apply func leaves untouched. internal/rungate uses it to stamp the last full run and record each check-in. Best-effort by contract: on failure the next gated invocation simply re-probes / re-checks. Empty path errors so the caller can log it.
func Write ¶
Write stamps last-run.json at path with this run's start metadata. An empty path is a no-op returning nil — callers pass paths.HeartbeatFile(), which is "" when the install dir is disabled (--install-dir=""), and treat the heartbeat as off in that case. Best-effort: callers should log a write error at debug/warn and continue, never fail the run on it.
It read-modify-writes so the RunGate cache survives: the heartbeat is stamped at the very top of every run, before the gate reads that cache on the offline path, so a blind overwrite here would erase it every wakeup.
Types ¶
type Record ¶
type Record struct {
SchemaVersion int `json:"schema_version"`
WrittenAt time.Time `json:"written_at"`
PID int `json:"pid"`
AgentVersion string `json:"agent_version"`
Command string `json:"command"` // subcommand that started the run, e.g. "send-telemetry"
InvocationMethod string `json:"invocation_method"` // scheduler footprint vs manual; see telemetry.DetectInvocationMethod
OS string `json:"os"`
RunGate *RunGate `json:"run_gate,omitempty"`
}
Record is the last-run.json envelope. The top-level fields are a point-in-time stamp that a run began — start-of-run facts only; the scan outcome lives in scan-state.json (LastSuccessfulExecutionID) and agent.error.log. RunGate is the run gate's small cache, folded into this same file rather than a sibling run-gate-state.json: it is the same "last run" kind of data written to the same install dir, and is consulted only on the offline fallback path.
func Load ¶
Load reads last-run.json. A missing file, parse error, or schema mismatch returns (nil, err) with err nil for the missing/mismatch cases (expected fall-throughs) so callers can treat a nil record as "no usable heartbeat" without distinguishing causes. Exposed for diagnostics and any future fleet-view that folds the last-run summary into the telemetry payload.
type RunGate ¶ added in v1.15.0
type RunGate struct {
ConfiguredDeviceID string `json:"configured_device_id,omitempty"`
DeviceID string `json:"device_id,omitempty"`
LastFullRunAt int64 `json:"last_full_run_at,omitempty"` // unix sec; stamped on upload success
GatingEnabled bool `json:"gating_enabled,omitempty"`
EffectiveIntervalMinutes int `json:"effective_interval_minutes,omitempty"`
DirectiveFetchedAt int64 `json:"directive_fetched_at,omitempty"` // unix sec of the last successful check-in
}
RunGate is the run gate's cached memory: the resolved device id (so skipped wakeups never re-probe the serial), the last completed full run (stamped after upload), and the last directive's gating fields. Nil until the first successful check-in. Everything here is advisory — a missing, corrupt, or future-schema file only costs one serial probe and one fail-open run, never a wrong skip. Fields mirror the wire directive; see internal/rungate.