forked from dfns/terraform-provider-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Ezzahhh/sig-terminate
feat: cleaner terminate with signals
- Loading branch information
Showing
6 changed files
with
94 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package provider | ||
package libs | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package libs | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"runtime" | ||
"strconv" | ||
"syscall" | ||
"time" | ||
|
||
ps "github.com/shirou/gopsutil/v4/process" | ||
) | ||
|
||
func WatchProcess(pid string) (err error) { | ||
pidInt, err := strconv.Atoi(pid) | ||
if err != nil { | ||
return fmt.Errorf("invalid PID: %v", err) | ||
} | ||
parent, err := ps.NewProcess(int32(pidInt)) | ||
if err != nil { | ||
return err | ||
} | ||
child, err := ps.NewProcess(int32(os.Getpid())) | ||
if err != nil { | ||
return err | ||
} | ||
// pool for parent process liveliness every 2 seconds | ||
go func() { | ||
for { | ||
_, err := parent.Status() | ||
if err != nil { | ||
log.Printf("parent process exited: %v\n", err) | ||
if runtime.GOOS == "windows" { | ||
err = child.Terminate() | ||
} else { | ||
err = child.SendSignal(syscall.SIGINT) | ||
} | ||
if err != nil { | ||
log.Printf("failed to terminate process: %v\n", err) | ||
} | ||
} | ||
time.Sleep(2 * time.Second) | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func CheckProcessExists(pid int) error { | ||
cmd, err := ps.NewProcess(int32(pid)) | ||
if err != nil { | ||
return err | ||
} | ||
stats, err := cmd.Status() | ||
if err != nil { | ||
return err | ||
} | ||
if stats[0] == "zombie" { | ||
return fmt.Errorf("process died") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Interrupt(pid int) error { | ||
cmd, err := ps.NewProcess(int32(pid)) | ||
if err != nil { | ||
return err | ||
} | ||
if runtime.GOOS == "windows" { | ||
err = cmd.Terminate() | ||
} else { | ||
err = cmd.SendSignal(syscall.SIGINT) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters