winsvc

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package winsvc lets sqi-server and sqi-worker run as Windows services and installs them as such.

Run is the only thing the binaries' run commands call. When the Windows Service Control Manager (SCM) started the process, Run hosts the command under svc.Run and cancels its context on Stop or PreShutdown. Otherwise — a console on Windows, or any other OS — it cancels on SIGINT/SIGTERM as signal.NotifyContext does. Either way context.Cause names what stopped the command: the signal ("interrupt", "terminated"), or "service stop" / "service preshutdown".

This package is a leaf: it must not import internal/config, internal/server or internal/worker/...; callers pass in whatever configuration it needs.

Index

Constants

View Source
const ShutdownMargin = 15 * time.Second

ShutdownMargin is added to a binary's own drain timeout to form the service's PreShutdown timeout, covering process exit after the drain.

Variables

This section is empty.

Functions

func DefaultLogPath

func DefaultLogPath(serviceName string) string

DefaultLogPath is where a service logs when log.file is not configured: %ProgramData%\sqi\logs\<name>\<name>.log. Each service has a directory of its own, so the account of one never gains access to another's logs.

func ExitCode

func ExitCode(err error) int

ExitCode is the exit code for a run command's error: 0 for nil, the code of the first ExitCoder in its chain, and 1 for any other error. The console exit and the Windows service's service-specific exit code both use it, so a supervisor sees the same code either way.

func IsService

func IsService() bool

IsService is always false off Windows.

func LogOutput

func LogOutput(ctx context.Context, file string, maxSizeMB, maxBackups int) (io.WriteCloser, error)

LogOutput opens the log destination for a run command: ResolveLogFile then sqilog.Output.

func NormalizeAccount

func NormalizeAccount(user string) string

NormalizeAccount maps --user to the SCM's account syntax: "" and "LocalSystem" mean LocalSystem (""); a bare name is a local account (.\name); DOMAIN\name and UPNs pass through.

func PowerShellLiteral

func PowerShellLiteral(s string) string

PowerShellLiteral is s as a PowerShell single-quoted string, with every quote that would end one, the typographic single quotes included, doubled.

func ProgramDataDir

func ProgramDataDir() string

ProgramDataDir returns %ProgramData%, falling back to C:\ProgramData.

func ResolveConfigPath

func ResolveConfigPath(flagValue, binary string) (string, error)

ResolveConfigPath returns the absolute config path `service install` bakes into the service command line: flagValue, or %ProgramData%\sqi\<binary>.yaml when empty. The file must exist — a service cannot prompt for one.

func ResolveLogFile

func ResolveLogFile(ctx context.Context, configured string) (string, error)

ResolveLogFile returns the log file to write: configured when set; in service mode with nothing configured, DefaultLogPath (a missing directory created with a protected ACL — a hand-registered service never ran `service install`); otherwise "" for stderr.

func Run

func Run(_ string, _ *string, fn func(ctx context.Context) error) error

Run calls fn under a SIGINT/SIGTERM-canceled context. name and configPath only matter on Windows.

func ServiceName

func ServiceName(ctx context.Context) string

ServiceName returns the installed service name when running as a service (delivered by the SCM, so it reflects `service install --name`), else "".

func SqiDir

func SqiDir() string

SqiDir is %ProgramData%\sqi, the directory every sqi service shares.

func TailLines

func TailLines(path string, n int) []string

TailLines returns up to the last n lines of path, or nil if it cannot be read. Used to show why a freshly installed service failed to start. Only the last tailWindow bytes are read, less the partial line they start in.

Types

type ExitCoder

type ExitCoder interface {
	error
	ExitCode() int
}

ExitCoder is an error that names the exit code its process should end with, such as a demoted HA server's.

type InstallSpec

type InstallSpec struct {
	Name, DisplayName, Description string
	ExePath                        string // absolute path of the binary to register
	RunVerb                        string // "serve" or "start"
	ConfigPath                     string // absolute; baked into the command line
	Account                        string // NormalizeAccount'd; "" = LocalSystem
	Password                       string
	DelayedAutoStart               bool
	DrainTimeout                   time.Duration // the binary's own graceful-shutdown bound
}

InstallSpec is what `service install` knows after parsing its flags.

type PathRef

type PathRef struct {
	// Key names the path's source: "config", "binary", or a configuration key
	// such as "nats.tls_ca_file".
	Key string
	// Path is the absolute path.
	Path string
}

PathRef is one path a service depends on, with the name of where it came from, so a refusal can say which setting to fix.

type ServiceConfig

type ServiceConfig struct {
	Name, DisplayName, Description string
	ExePath                        string
	Args                           []string // mgr.CreateService quotes and escapes these
	Account, Password              string
	DelayedAutoStart               bool
	Recovery                       []time.Duration
	RecoveryReset                  time.Duration
	PreShutdownTimeout             time.Duration
}

ServiceConfig is the exact service registration Install creates.

func BuildServiceConfig

func BuildServiceConfig(spec InstallSpec) (ServiceConfig, error)

BuildServiceConfig validates spec and derives the registration from it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL