service.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. // Package debug provides facilities to execute svc.Handler on console.
  6. //
  7. package debug
  8. import (
  9. "os"
  10. "os/signal"
  11. "syscall"
  12. "golang.org/x/sys/windows/svc"
  13. )
  14. // Run executes service name by calling appropriate handler function.
  15. // The process is running on console, unlike real service. Use Ctrl+C to
  16. // send "Stop" command to your service.
  17. func Run(name string, handler svc.Handler) error {
  18. cmds := make(chan svc.ChangeRequest)
  19. changes := make(chan svc.Status)
  20. sig := make(chan os.Signal)
  21. signal.Notify(sig)
  22. go func() {
  23. status := svc.Status{State: svc.Stopped}
  24. for {
  25. select {
  26. case <-sig:
  27. cmds <- svc.ChangeRequest{Cmd: svc.Stop, CurrentStatus: status}
  28. case status = <-changes:
  29. }
  30. }
  31. }()
  32. _, errno := handler.Execute([]string{name}, cmds, changes)
  33. if errno != 0 {
  34. return syscall.Errno(errno)
  35. }
  36. return nil
  37. }