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
- func DefaultLogPath(serviceName string) string
- func ExitCode(err error) int
- func IsService() bool
- func LogOutput(ctx context.Context, file string, maxSizeMB, maxBackups int) (io.WriteCloser, error)
- func NormalizeAccount(user string) string
- func PowerShellLiteral(s string) string
- func ProgramDataDir() string
- func ResolveConfigPath(flagValue, binary string) (string, error)
- func ResolveLogFile(ctx context.Context, configured string) (string, error)
- func Run(_ string, _ *string, fn func(ctx context.Context) error) error
- func ServiceName(ctx context.Context) string
- func SqiDir() string
- func TailLines(path string, n int) []string
- type ExitCoder
- type InstallSpec
- type PathRef
- type ServiceConfig
Constants ¶
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 ¶
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 ¶
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 LogOutput ¶
LogOutput opens the log destination for a run command: ResolveLogFile then sqilog.Output.
func NormalizeAccount ¶
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 ¶
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 ¶
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 ¶
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 ¶
Run calls fn under a SIGINT/SIGTERM-canceled context. name and configPath only matter on Windows.
func ServiceName ¶
ServiceName returns the installed service name when running as a service (delivered by the SCM, so it reflects `service install --name`), else "".
Types ¶
type ExitCoder ¶
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.