Documentation
¶
Overview ¶
Package service defines portable per-user OS service definitions and their registration lifecycle. Rendering is pure; publication never replaces an existing definition; registration, start, stop, and status are delegated to a caller-selected manager.
The package owns no product name, installation layout, data directory, port, key provider, endpoint, or readiness protocol. The consumer supplies every value that identifies its service.
Index ¶
- Constants
- Variables
- func ExecRunner(ctx context.Context, directory, executable string, args ...string) ([]byte, error)
- func OutputText(output []byte) string
- func Remove(def Definition, directory string) error
- type CommandRunner
- type Definition
- type Inspection
- type Manager
- type Platform
- type Publication
- type Readiness
- type Registration
- type RestartPolicy
- type Service
- func (s *Service) Register(ctx context.Context) (Registration, error)
- func (s *Service) Spec() Spec
- func (s *Service) Start(ctx context.Context) error
- func (s *Service) Status(ctx context.Context) (Status, error)
- func (s *Service) Stop(ctx context.Context) error
- func (s *Service) Unregister(ctx context.Context) error
- type Spec
- type Status
Constants ¶
const MaxCommandOutput = 64 << 10
MaxCommandOutput bounds captured manager output. Manager output is diagnostic text, never a protocol, and is truncated rather than streamed to the host.
Variables ¶
var ( // ErrInvalid reports a malformed specification or definition. ErrInvalid = errors.New("invalid service definition") // ErrExists reports a different definition already at the target path. ErrExists = errors.New("a different service definition already exists") // ErrStale reports that the published definition no longer matches the // specification the caller asked to act on. ErrStale = errors.New("published service definition changed") // ErrUnsafe reports a link, non-directory, or broadly writable location. ErrUnsafe = errors.New("unsafe service path") // ErrUnsupported reports a platform or capability the backend cannot honor. ErrUnsupported = errors.New("unsupported service platform") // ErrRegistered reports a service the manager already knows. ErrRegistered = errors.New("service is already registered") // ErrNotInstalled reports lifecycle work on a definition nobody published. ErrNotInstalled = errors.New("service definition is not installed") )
Functions ¶
func ExecRunner ¶
ExecRunner runs one native service command with captured, bounded output. It is the default CommandRunner for the platform backends.
func OutputText ¶
OutputText returns trimmed command output for a status detail.
func Remove ¶
func Remove(def Definition, directory string) error
Remove deletes only the regular definition file this specification names.
Types ¶
type CommandRunner ¶
type CommandRunner func(ctx context.Context, directory, executable string, args ...string) ([]byte, error)
CommandRunner is the native command boundary. Output is captured, not inherited, so a manager cannot write into the host application's streams.
type Definition ¶
type Definition struct {
Platform Platform
Identity string
FileName string
Content string
Digest string
Path string
}
Definition is a rendered native service definition. Path is empty until the definition is published.
type Inspection ¶
Inspection is the on-disk state of one expected definition.
func CheckDefinition ¶
func CheckDefinition(def Definition, directory string) (Inspection, error)
CheckDefinition rejects a status operation when the on-disk definition is missing or no longer the one the caller rendered.
func Inspect ¶
func Inspect(def Definition, directory string) (Inspection, error)
Inspect reports whether the expected definition is installed and unchanged.
type Manager ¶
type Manager interface {
Platform() Platform
Name() string
Register(ctx context.Context, spec Spec) (Registration, error)
Unregister(ctx context.Context, spec Spec) error
Start(ctx context.Context, spec Spec) error
Stop(ctx context.Context, spec Spec) error
Status(ctx context.Context, spec Spec) (Status, error)
}
Manager is implemented by a per-user service backend. It never selects its own identity, directory, or executable.
type Platform ¶
type Platform string
Platform selects the native definition format. It is never inferred from the runtime inside this package.
type Publication ¶
Publication reports a bounded, non-overwriting publish attempt.
func Publish ¶
func Publish(def Definition, directory string) (Publication, error)
Publish writes a complete definition into an existing directory without replacing anything. A hard link makes publication atomic and fails closed on filesystems that cannot link. An identical existing file is reported rather than rewritten.
type Readiness ¶
Readiness is an optional caller probe. The library has no protocol, port, or endpoint of its own.
type Registration ¶
type Registration struct {
Definition Definition
Publication Publication
Registered bool
}
Registration reports publication and manager registration separately so a caller can recover when the manager rejects an otherwise valid definition.
type RestartPolicy ¶
type RestartPolicy string
RestartPolicy is the caller's restart choice. Platforms that cannot express a policy exactly fail instead of silently substituting another behavior.
const ( RestartNever RestartPolicy = "never" RestartOnFailure RestartPolicy = "on-failure" RestartAlways RestartPolicy = "always" )
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service binds one specification to one manager and an optional readiness probe.
type Spec ¶
type Spec struct {
// Name is the service identity: a launchd label, a systemd unit name, or a
// Task Scheduler task name.
Name string
// Description is human-readable metadata; it defaults to Name.
Description string
// Executable is an absolute local path.
Executable string
// Arguments are passed verbatim to the executable.
Arguments []string
// Environment is rendered only on platforms that support it. Windows Task
// Scheduler has no environment field, so a non-empty map fails there.
Environment map[string]string
// WorkingDirectory is an optional absolute local path.
WorkingDirectory string
// Restart selects the manager's restart behavior.
Restart RestartPolicy
// RestartSeconds delays a restart. Zero selects five seconds.
RestartSeconds int
// UserID is the explicit Windows task account name or SID.
UserID string
}
Spec is the complete caller-supplied service definition.
type Status ¶
type Status struct {
Name string `json:"name"`
Manager string `json:"manager"`
DefinitionPath string `json:"definition_path,omitempty"`
DefinitionDigest string `json:"definition_digest,omitempty"`
Installed bool `json:"installed"`
Stale bool `json:"stale"`
Registered bool `json:"registered"`
Active bool `json:"active"`
Detail string `json:"detail"`
CheckedAt time.Time `json:"checked_at"`
Ready *bool `json:"ready,omitempty"`
Attributes map[string]string `json:"attributes,omitempty"`
}
Status is a point-in-time projection. Ready is filled by a Service wrapper's readiness callback; a manager never infers readiness from being active.