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