Documentation
¶
Overview ¶
Package servicemgr owns the local metadata and coordination primitives used by the native Windows and Linux service backends.
Index ¶
- Constants
- Variables
- func DefaultWorkerName(id string) string
- func ExitCodeOf(err error) int
- func OwnsListeningPort(pid int, host string, port uint16) (bool, error)
- func RunUpgradeHelper(ctx context.Context, receiptPath string) error
- func RuntimeDirFor(configDir, explicitConfig string) (string, error)
- func SystemdJournal(ctx context.Context, m *Manager, lines int) ([]byte, error)
- func SystemdRegister(ctx context.Context, m *Manager, spec Spec) error
- func SystemdStop(ctx context.Context, m *Manager) error
- func SystemdUninstall(ctx context.Context, m *Manager) error
- type Backend
- type Kind
- type LastExit
- type Lock
- type Manager
- func (m *Manager) AcceptUpgradeControl(id, verifiedSourceJobID string) error
- func (m *Manager) Acquire() (*Lock, error)
- func (m *Manager) InspectServer() (daemon.ProcessIdentity, error)
- func (m *Manager) LoadSpec() (Spec, error)
- func (m *Manager) LoadState() (State, error)
- func (m *Manager) LoadUpgradeControl() (UpgradeControl, error)
- func (m *Manager) LoadUpgradeReceipt(id string) (UpgradeReceipt, error)
- func (m *Manager) LockPath() string
- func (m *Manager) SaveSpec(spec Spec) error
- func (m *Manager) SaveState(state State) error
- func (m *Manager) SaveUpgradeControl(control UpgradeControl) error
- func (m *Manager) SaveUpgradeReceipt(receipt UpgradeReceipt) error
- func (m *Manager) SpecPath() string
- func (m *Manager) StatePath() string
- func (m *Manager) UpgradeControlPath() string
- func (m *Manager) UpgradeReceiptPath(id string) (string, error)
- func (m *Manager) VerifyUpgradeTarget(ctx context.Context) error
- type ServeOptions
- type Spec
- func (s Spec) ApplicationLogName() string
- func (s Spec) CheckFiles() error
- func (s Spec) CommandArgs() ([]string, error)
- func (s Spec) Description() string
- func (s Spec) EffectiveKind() Kind
- func (s Spec) OutLogName() string
- func (s Spec) PIDFileName() string
- func (s Spec) ServerArgs() ([]string, error)
- func (s Spec) SupervisorArgs(specPath string) []string
- func (s Spec) Validate() error
- type State
- type SystemdStatus
- type UpgradeControl
- type UpgradePhase
- type UpgradeReceipt
- type UpgradeRequest
- type WorkerOptions
Constants ¶
const ( SpecSchema = 1 DefaultName = "gofer-serve" EnvManagedSpec = "GOFER_MANAGED_SPEC" )
const StateSchema = 1
const UpgradeSchema = 1
Variables ¶
var ErrIdentityMismatch = errors.New("service process identity mismatch")
var ErrLockBusy = errors.New("service instance is busy")
var ErrRunningSpecChange = errors.New("stop the running service before changing its registration")
Functions ¶
func DefaultWorkerName ¶ added in v0.215.0
DefaultWorkerName is the default managed name of a worker registration. Worker names carry the id so several workers on one host never collide.
func ExitCodeOf ¶ added in v0.217.0
ExitCodeOf turns the error exec.Cmd.Wait returns into the process exit code: 0 for a clean exit, -1 when the process was signalled/debug-stopped and had no code.
func OwnsListeningPort ¶
OwnsListeningPort binds a health probe to the already verified server PID. Both native backends implement the platform-specific socket ownership check.
func RunUpgradeHelper ¶
RunUpgradeHelper is the private process entry. It must see a precommitted receipt and gate, publish its own identity, then wait for the server bridge's durable accepted transition. No old process is stopped before that point.
func RuntimeDirFor ¶
RuntimeDirFor follows the existing serve -c rule: only an explicit -c puts serve.pid beside that file. GOFER_CONFIG affects file selection, not this runtime path. Callers supply the selected ConfigDir independently.
func SystemdJournal ¶
SystemdJournal reads the native journal without routing through a shell.
func SystemdRegister ¶
SystemdRegister installs and enables an owned unit. It never starts it.
func SystemdStop ¶
SystemdStop stops through systemctl so Restart=on-failure cannot revive serve.
Types ¶
type Kind ¶ added in v0.215.0
type Kind string
Kind selects the Gofer process a managed registration runs. A spec file written before Kind existed has no kind field; those registrations all run `gofer serve`, which EffectiveKind reports for them.
type LastExit ¶ added in v0.217.0
type LastExit struct {
// At is when the supervisor saw the process end.
At time.Time `json:"at"`
// ExitCode is what the process returned. Windows reports the raw DWORD, so a crash
// killed mid-teardown shows up as 4294967295 (0xffffffff).
ExitCode int `json:"exit_code"`
// PID / UptimeSec describe the incarnation that ended (how long it had been up).
PID int `json:"pid,omitempty"`
UptimeSec int64 `json:"uptime_sec,omitempty"`
}
LastExit is one abnormal end of the supervised server process.
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock holds an OS file lock for an entire lifecycle operation. The lock file is intentionally retained after release: removing it could let a contender create a different inode while another process still holds the old one.
func AcquireLock ¶
type Manager ¶
Manager locates one instance's metadata. Platform backends extend its lifecycle; the shared core does not write OS registrations.
func NewManager ¶
func (*Manager) AcceptUpgradeControl ¶
AcceptUpgradeControl is the durable CAS used only after the server bridge has verified the source job and drained all other work. The helper has to prove independent liveness first; it never grants itself this transition.
func (*Manager) InspectServer ¶
func (m *Manager) InspectServer() (daemon.ProcessIdentity, error)
InspectServer reads the recorded pid and checks the live process against the spec and the required recorded creation identity. A native backend must separately verify its task/unit command line and config-file ownership.
func (*Manager) LoadUpgradeControl ¶
func (m *Manager) LoadUpgradeControl() (UpgradeControl, error)
func (*Manager) LoadUpgradeReceipt ¶
func (m *Manager) LoadUpgradeReceipt(id string) (UpgradeReceipt, error)
func (*Manager) SaveSpec ¶
SaveSpec is a registration update. Its caller holds Acquire across the full native task/unit transaction; this method also rejects changes while either recorded process remains live.
func (*Manager) SaveUpgradeControl ¶
func (m *Manager) SaveUpgradeControl(control UpgradeControl) error
SaveUpgradeControl atomically updates the gate. The caller holds m.Acquire while creating or transitioning a transaction; readers never see half JSON.
func (*Manager) SaveUpgradeReceipt ¶
func (m *Manager) SaveUpgradeReceipt(receipt UpgradeReceipt) error
func (*Manager) UpgradeControlPath ¶
func (*Manager) UpgradeReceiptPath ¶
type ServeOptions ¶
type ServeOptions struct {
Addr string `json:"addr,omitempty"`
NoWeb bool `json:"no_web,omitempty"`
WebDir string `json:"web_dir,omitempty"`
AllowEmptyToken bool `json:"allow_empty_token,omitempty"`
}
ServeOptions is an allowlist of non-secret arguments. In particular there is no token or arbitrary argument list in the durable spec.
type Spec ¶
type Spec struct {
SchemaVersion int `json:"schema_version"`
Backend Backend `json:"backend"`
Name string `json:"name"`
Owner string `json:"owner"`
Exe string `json:"exe"`
WorkDir string `json:"work_dir"`
ConfigFile string `json:"config_file"`
ConfigDir string `json:"config_dir"`
RuntimeDir string `json:"runtime_dir"`
RunAs string `json:"run_as,omitempty"`
Elevated bool `json:"elevated,omitempty"`
// Kind is the process this registration runs. An empty value is a spec
// written before workers could be registered and reads as serve.
Kind Kind `json:"kind,omitempty"`
Serve ServeOptions `json:"serve"`
Worker WorkerOptions `json:"worker,omitempty"`
RegisteredVersion string `json:"registered_version,omitempty"`
}
Spec is the durable, non-secret description of one managed serve instance. RuntimeDir is recorded separately because an explicit -c puts serve.pid and serve.log beside that config file, which can differ from ConfigDir.
func (Spec) ApplicationLogName ¶ added in v0.215.0
ApplicationLogName is the fallback application log in RuntimeDir, used when the managed config names no log file or directory.
func (Spec) CheckFiles ¶
CheckFiles is the registration preflight. config.Load intentionally accepts a missing explicit -c for config editing; a managed serve cannot use it.
func (Spec) CommandArgs ¶ added in v0.215.0
CommandArgs is the managed process command line, built per kind from an allowlist only. A worker registration runs `gofer worker --worker-config <worker.yaml>`; its hub, projects and runner policy all live in that file.
func (Spec) Description ¶ added in v0.215.0
Description is the human-readable unit/task description for the kind.
func (Spec) EffectiveKind ¶ added in v0.215.0
EffectiveKind reads a legacy spec (no kind field) as serve.
func (Spec) OutLogName ¶ added in v0.215.0
OutLogName is where a Windows supervisor redirects the managed process output.
func (Spec) PIDFileName ¶ added in v0.215.0
PIDFileName is the runtime pidfile of the managed process: serve.pid for a server (its long-standing name) and worker-<id>.pid for a worker, matching the daemon-mode file names of `gofer worker -d`.
func (Spec) ServerArgs ¶
ServerArgs produces only known serve flags; no caller-supplied free-form args or credentials can be serialized into an OS task/unit command line.
func (Spec) SupervisorArgs ¶ added in v0.215.0
SupervisorArgs is the hidden command a Windows task starts. The same entry serves both kinds: it loads the spec and supervises whatever Kind it names, so a worker registration needs no separate hidden command.
type State ¶
type State struct {
SchemaVersion int `json:"schema_version"`
Name string `json:"name"`
StopRequested bool `json:"stop_requested"`
NativeInvocationID string `json:"native_invocation_id,omitempty"`
Supervisor daemon.ProcessIdentity `json:"supervisor,omitempty"`
Server daemon.ProcessIdentity `json:"server,omitempty"`
// LastExit (gofer-opre) is the last exit of the supervised server that NOBODY asked
// for (a requested stop or a recorded upgrade drain is not one). A server that dies
// from a `taskkill /IM gofer.exe` used to leave nothing behind but a line in the
// supervisor log; `serve status` reads this and says when and with which code.
LastExit *LastExit `json:"last_exit,omitempty"`
}
State is local control state, not an OS manager's status source. A backend still has to verify its task/unit and the process before reporting success.
type SystemdStatus ¶
type SystemdStatus struct {
Installed bool
Enabled bool
Active bool
SubState string
MainPID int
Server daemon.ProcessIdentity
UnitPath string
ApplicationLog string
}
SystemdStatus combines the native unit state and verified process identity.
func SystemdRestart ¶
func SystemdRestart(ctx context.Context, m *Manager) (SystemdStatus, error)
func SystemdStart ¶
func SystemdStart(ctx context.Context, m *Manager) (SystemdStatus, error)
SystemdStart starts a previously registered unit and verifies its process.
func SystemdStatusOf ¶
func SystemdStatusOf(ctx context.Context, m *Manager) (SystemdStatus, error)
type UpgradeControl ¶
type UpgradeControl struct {
SchemaVersion int `json:"schema_version"`
UpgradeID string `json:"upgrade_id"`
Name string `json:"name"`
SourceJobID string `json:"source_job_id,omitempty"`
InitiatorPID int `json:"initiator_pid"`
InitiatorStartID string `json:"initiator_start_id"`
Deadline time.Time `json:"deadline"`
Phase UpgradePhase `json:"phase"`
}
UpgradeControl is the local admission/drain gate. SourceJobID is informational until the job bridge checks it against trusted current-job metadata; the helper never treats a caller-supplied ID as authorization to ignore a job.
func (UpgradeControl) Validate ¶
func (c UpgradeControl) Validate() error
type UpgradePhase ¶
type UpgradePhase string
const ( UpgradeDraining UpgradePhase = "draining" UpgradeAccepted UpgradePhase = "accepted" UpgradeSwitching UpgradePhase = "switching" UpgradeSucceeded UpgradePhase = "succeeded" UpgradeRolledBack UpgradePhase = "rolled_back" UpgradeFailed UpgradePhase = "failed" )
type UpgradeReceipt ¶
type UpgradeReceipt struct {
SchemaVersion int `json:"schema_version"`
UpgradeID string `json:"upgrade_id"`
Name string `json:"name"`
Phase UpgradePhase `json:"phase"`
SourceJobID string `json:"source_job_id,omitempty"`
CandidateSHA256 string `json:"candidate_sha256"`
CandidateVersion string `json:"candidate_version"`
PreviousVersion string `json:"previous_version,omitempty"`
CandidatePath string `json:"candidate_path"`
Helper daemon.ProcessIdentity `json:"helper,omitempty"`
StartedAt time.Time `json:"started_at"`
AcceptedAt *time.Time `json:"accepted_at,omitempty"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
Error string `json:"error,omitempty"`
}
UpgradeReceipt survives process restarts and is the source of truth for an upgrade outcome. An interrupted nonterminal phase is never reported as done.
func BeginUpgrade ¶
func BeginUpgrade(ctx context.Context, m *Manager, request UpgradeRequest) (UpgradeReceipt, error)
BeginUpgrade prepares and launches a helper; it returns only after that independent process has recorded its own live identity. The server bridge still has to drain and AcceptUpgradeControl before any stop or switch.
func WaitUpgradeAccepted ¶
WaitUpgradeAccepted waits only for the bridge's durable drain/identity ACK. A job caller must return this ID and exit; its helper waits for that exact initiator process to exit before stopping the old server.
func (UpgradeReceipt) Validate ¶
func (r UpgradeReceipt) Validate() error
type UpgradeRequest ¶
type WorkerOptions ¶ added in v0.215.0
type WorkerOptions struct {
ID string `json:"id,omitempty"`
ConfigDir string `json:"config_dir,omitempty"`
}
WorkerOptions is the worker-specific part of a managed registration. ConfigDir is the worker's own config directory (worker.yaml, .env and run/ inside it); it must equal the spec's ConfigDir because the managed process is started with GOFER_CONFIG_DIR pointing there.