github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/windows/svc/debug/service.go (about)

     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  
     5  // +build windows
     6  
     7  // Package debug provides facilities to execute svc.Handler on console.
     8  //
     9  package debug
    10  
    11  import (
    12  	"os"
    13  	"os/signal"
    14  	"syscall"
    15  
    16  	"github.com/Andyfoo/golang/x/sys/windows/svc"
    17  )
    18  
    19  // Run executes service name by calling appropriate handler function.
    20  // The process is running on console, unlike real service. Use Ctrl+C to
    21  // send "Stop" command to your service.
    22  func Run(name string, handler svc.Handler) error {
    23  	cmds := make(chan svc.ChangeRequest)
    24  	changes := make(chan svc.Status)
    25  
    26  	sig := make(chan os.Signal)
    27  	signal.Notify(sig)
    28  
    29  	go func() {
    30  		status := svc.Status{State: svc.Stopped}
    31  		for {
    32  			select {
    33  			case <-sig:
    34  				cmds <- svc.ChangeRequest{Cmd: svc.Stop, CurrentStatus: status}
    35  			case status = <-changes:
    36  			}
    37  		}
    38  	}()
    39  
    40  	_, errno := handler.Execute([]string{name}, cmds, changes)
    41  	if errno != 0 {
    42  		return syscall.Errno(errno)
    43  	}
    44  	return nil
    45  }