diff options
author | Andrey Nering <andrey.nering@gmail.com> | 2017-01-23 20:40:11 -0200 |
---|---|---|
committer | Andrey Nering <andrey.nering@gmail.com> | 2017-01-23 20:40:11 -0200 |
commit | 44d4863ecf3d0eb63540f4f10fa2d4760e4daa74 (patch) | |
tree | ccdbfb6b2d5c225797b708e84c7fa1a659a80004 /modules/minwinsvc/svc_windows.go | |
parent | 8b87be63c53bc76ba0b764d2bd6b2df1ed9f6243 (diff) | |
parent | af636848622c8ad27cace63be5f9dd9aaa565502 (diff) | |
download | gitea-44d4863ecf3d0eb63540f4f10fa2d4760e4daa74.tar.gz gitea-44d4863ecf3d0eb63540f4f10fa2d4760e4daa74.zip |
Merge commit 'af636848622c8ad27cace63be5f9dd9aaa565502' as 'modules/minwinsvc'
Diffstat (limited to 'modules/minwinsvc/svc_windows.go')
-rw-r--r-- | modules/minwinsvc/svc_windows.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/modules/minwinsvc/svc_windows.go b/modules/minwinsvc/svc_windows.go new file mode 100644 index 0000000000..91e2b6a4dd --- /dev/null +++ b/modules/minwinsvc/svc_windows.go @@ -0,0 +1,70 @@ +// Copyright 2015 Daniel Theophanes. +// Use of this source code is governed by a zlib-style +// license that can be found in the LICENSE file.package service + +//+build windows + +package minwinsvc + +import ( + "os" + "sync" + + "golang.org/x/sys/windows/svc" +) + +var ( + onExit func() + guard sync.Mutex +) + +func init() { + interactive, err := svc.IsAnInteractiveSession() + if err != nil { + panic(err) + } + if interactive { + return + } + go func() { + _ = svc.Run("", runner{}) + + guard.Lock() + f := onExit + guard.Unlock() + + // Don't hold this lock in user code. + if f != nil { + f() + } + // Make sure we exit. + os.Exit(0) + }() +} + +func setOnExit(f func()) { + guard.Lock() + onExit = f + guard.Unlock() +} + +type runner struct{} + +func (runner) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) { + const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown + changes <- svc.Status{State: svc.StartPending} + + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} + for { + c := <-r + switch c.Cmd { + case svc.Interrogate: + changes <- c.CurrentStatus + case svc.Stop, svc.Shutdown: + changes <- svc.Status{State: svc.StopPending} + return false, 0 + } + } + + return false, 0 +} |