service: block on windows stop so restart is more robust.

Previous behavior simply signalled the service to stop.
The new behavior actually waits for the service to stop.
This makes the Restart method for windows to be more robust
when a service takes more time to clean up.
This commit is contained in:
Daniel Theophanes
2015-06-05 09:22:14 -07:00
parent 3a58801094
commit 1ab12303aa
2 changed files with 129 additions and 5 deletions
+62
View File
@@ -0,0 +1,62 @@
// 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
// simple does nothing except block while running the service.
package main
import (
"log"
"os"
"time"
"github.com/kardianos/service"
)
var logger service.Logger
type program struct{}
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}
func (p *program) run() {
// Do work here
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
<-time.After(time.Second * 13)
return nil
}
func main() {
svcConfig := &service.Config{
Name: "GoServiceExampleStopPause",
DisplayName: "Go Service Example: Stop Pause",
Description: "This is an example Go service that pauses on stop.",
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
if len(os.Args) > 1 {
err = service.Control(s, os.Args[1])
if err != nil {
log.Fatal(err)
}
return
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}