mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
connection handling / block controller handling (#326)
This commit is contained in:
@@ -2,7 +2,9 @@ package shellexec
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/creack/pty"
|
||||
"golang.org/x/crypto/ssh"
|
||||
@@ -10,6 +12,7 @@ import (
|
||||
|
||||
type ConnInterface interface {
|
||||
Kill()
|
||||
KillGraceful(time.Duration)
|
||||
Wait() error
|
||||
Start() error
|
||||
StdinPipe() (io.WriteCloser, error)
|
||||
@@ -32,6 +35,22 @@ func (cw CmdWrap) Wait() error {
|
||||
return cw.Cmd.Wait()
|
||||
}
|
||||
|
||||
func (cw CmdWrap) KillGraceful(timeout time.Duration) {
|
||||
if cw.Cmd.Process == nil {
|
||||
return
|
||||
}
|
||||
if cw.Cmd.ProcessState != nil && cw.Cmd.ProcessState.Exited() {
|
||||
return
|
||||
}
|
||||
cw.Cmd.Process.Signal(os.Interrupt)
|
||||
go func() {
|
||||
time.Sleep(timeout)
|
||||
if cw.Cmd.ProcessState == nil || !cw.Cmd.ProcessState.Exited() {
|
||||
cw.Cmd.Process.Kill() // force kill if it is already not exited
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (cw CmdWrap) Start() error {
|
||||
defer func() {
|
||||
for _, extraFile := range cw.Cmd.ExtraFiles {
|
||||
@@ -75,6 +94,10 @@ func (sw SessionWrap) Kill() {
|
||||
sw.Session.Close()
|
||||
}
|
||||
|
||||
func (sw SessionWrap) KillGraceful(timeout time.Duration) {
|
||||
sw.Kill()
|
||||
}
|
||||
|
||||
func (sw SessionWrap) Wait() error {
|
||||
return sw.Session.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user