add windows support (#867)

* add windows support

* add windows support

* add windows support

Co-authored-by: 78552423@qq.com <chenyz0812>
This commit is contained in:
eshun 2022-02-18 15:55:00 +08:00 committed by GitHub
parent f8482601a8
commit f009c43878
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 1 deletions

33
src/pkg/sys/cmd_unix.go Normal file
View File

@ -0,0 +1,33 @@
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris plan9
// Unix environment variables.
package sys
import (
"os/exec"
"syscall"
"time"
)
func WrapTimeout(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
var err error
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(timeout):
go func() {
<-done // allow goroutine to exit
}()
// IMPORTANT: cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} is necessary before cmd.Start()
err = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
return err, true
case err = <-done:
return err, false
}
}

View File

@ -0,0 +1,30 @@
// Windows environment variables.
package sys
import (
"os/exec"
"syscall"
"time"
)
func WrapTimeout(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
var err error
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(timeout):
go func() {
<-done // allow goroutine to exit
}()
err = cmd.Process.Signal(syscall.SIGKILL)
return err, true
case err = <-done:
return err, false
}
}

View File

@ -16,9 +16,9 @@ import (
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/runner"
"github.com/toolkits/pkg/sys"
"github.com/didi/nightingale/v5/src/models"
"github.com/didi/nightingale/v5/src/pkg/sys"
"github.com/didi/nightingale/v5/src/server/config"
"github.com/didi/nightingale/v5/src/server/memsto"
"github.com/didi/nightingale/v5/src/storage"