2021-08-24 04:37:44 +08:00
|
|
|
//go:build !windows
|
2020-06-05 07:09:22 +08:00
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
|
|
import (
|
2020-07-02 23:59:29 +08:00
|
|
|
"context"
|
2020-06-05 07:09:22 +08:00
|
|
|
"os/exec"
|
|
|
|
|
"syscall"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-13 04:23:53 +08:00
|
|
|
func (p *Process) gracefulStop(ctx context.Context, cmd *exec.Cmd, timeout time.Duration) {
|
2020-07-02 23:59:29 +08:00
|
|
|
select {
|
|
|
|
|
case <-time.After(timeout):
|
2022-10-13 04:23:53 +08:00
|
|
|
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
|
|
|
|
|
p.Log.Errorf("Error after sending SIGTERM signal to process: %v", err)
|
|
|
|
|
}
|
2020-07-02 23:59:29 +08:00
|
|
|
case <-ctx.Done():
|
|
|
|
|
}
|
|
|
|
|
select {
|
|
|
|
|
case <-time.After(timeout):
|
2022-10-13 04:23:53 +08:00
|
|
|
if err := cmd.Process.Kill(); err != nil {
|
|
|
|
|
p.Log.Errorf("Error after killing process: %v", err)
|
|
|
|
|
}
|
2020-07-02 23:59:29 +08:00
|
|
|
case <-ctx.Done():
|
|
|
|
|
}
|
2020-06-05 07:09:22 +08:00
|
|
|
}
|