RedFlag/server/cmd/server/service_windows.go
Fimeg 765ec4188f publish: carry the Windows product into the projection
The tree adds the installer, service and test paths admitted by this source commit. Nothing else changes about what may cross.

Source-Sha: 3e4a3aa4ce092e9e3f51bc78daf3ddc14d9638f8

Policy-Sha: 3e4a3aa4ce092e9e3f51bc78daf3ddc14d9638f8

Tree-Digest: 89d6fce3828a0e0de0c8eeb41ee6750f4462217e2a04b5190297134d7f4a50c3
2026-09-09 12:47:34 -04:00

69 lines
1.7 KiB
Go

//go:build windows
package main
import (
"context"
"time"
"golang.org/x/sys/windows/svc"
)
const serverServiceName = "RedFlagServer"
type windowsServerService struct {
run func(context.Context, func())
}
func runWindowsService(run func(context.Context, func())) (bool, error) {
service, err := svc.IsWindowsService()
if err != nil || !service {
return service, err
}
return true, svc.Run(serverServiceName, &windowsServerService{run: run})
}
func (s *windowsServerService) Execute(_ []string, requests <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ready := make(chan struct{})
done := make(chan struct{})
status := svc.Status{State: svc.StartPending, CheckPoint: 1, WaitHint: 10000}
changes <- status
go func() {
defer close(done)
s.run(ctx, func() { close(ready) })
}()
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
select {
case <-ready:
ready = nil
if ctx.Err() == nil {
status = svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
changes <- status
}
case request := <-requests:
switch request.Cmd {
case svc.Interrogate:
changes <- status
case svc.Stop, svc.Shutdown:
status = svc.Status{State: svc.StopPending, CheckPoint: 1, WaitHint: 10000}
changes <- status
cancel()
}
case <-ticker.C:
if status.State == svc.StartPending || status.State == svc.StopPending {
status.CheckPoint++
changes <- status
}
case <-done:
if ctx.Err() == nil {
return true, 1 // An unsolicited exit is not a successful service stop.
}
return false, 0
}
}
}