connection handling / block controller handling (#326)

This commit is contained in:
Mike Sawka
2024-09-05 00:21:08 -07:00
committed by GitHub
parent b796ec9729
commit 3e0ca6b41e
28 changed files with 690 additions and 313 deletions
+23
View File
@@ -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()
}