1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-09-16 08:36:30 +02:00

fixing #2235 win10 install failed issue (#2239)

This commit is contained in:
Johnson C
2021-09-03 14:49:39 +08:00
committed by GitHub
parent e080791787
commit f444dadd50
2 changed files with 68 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
// +build !windows
package run
import (

View File

@@ -0,0 +1,66 @@
// +build windows
package run
import (
"os"
"os/exec"
"syscall"
)
// Service is the interface that wraps the service that should run.
//
// Start starts the service and exits on error.
//
// Stop stops the service and exits on error.
//
// Wait waits for the service to exit and exits on error.
type Service interface {
Start() error
Stop() error
Wait() error
}
type service struct {
cmd *exec.Cmd
running bool
}
// Start starts the service and exits on error.
func (s *service) Start() error {
if s.running {
return nil
}
s.cmd = exec.Command("go", "run", ".")
s.cmd.SysProcAttr = &syscall.SysProcAttr{}
s.cmd.Stdout = os.Stdout
s.cmd.Stderr = os.Stderr
s.running = true
return s.cmd.Start()
}
// Stop stops the service and exits on error.
func (s *service) Stop() error {
if !s.running {
return nil
}
s.running = false
pro, err := os.FindProcess(s.cmd.Process.Pid)
if err != nil {
return err
}
return pro.Signal(syscall.SIGTERM)
}
// Wait waits for the service to exit and exits on error.
func (s *service) Wait() error {
return s.cmd.Wait()
}
func newService() *service {
service := new(service)
return service
}