NOISSUE - Stop computation gracefully (#241)

* stop gracefully

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* use constant

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

---------

Signed-off-by: Sammy Oina <sammyoina@gmail.com>
This commit is contained in:
Sammy Kerata Oina
2024-09-17 18:57:42 +03:00
committed by GitHub
parent c14a6338cc
commit 2f4ca414cb
2 changed files with 28 additions and 17 deletions
-11
View File
@@ -62,14 +62,3 @@ func RunCmdOutput(command string, args ...string) (string, error) {
return string(output), nil
}
// RunCmdStart starts the specified command and returns the *exec.Cmd for the running process.
func RunCmdStart(command string, args ...string) (*exec.Cmd, error) {
cmd := exec.Command(command, args...)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("error starting command '%s': %s", cmd.String(), err)
}
return cmd, nil
}
+28 -6
View File
@@ -17,11 +17,12 @@ import (
)
const (
firmwareVars = "OVMF_VARS"
KernelFile = "bzImage"
rootfsFile = "rootfs.cpio"
tmpDir = "/tmp"
interval = 5 * time.Second
firmwareVars = "OVMF_VARS"
KernelFile = "bzImage"
rootfsFile = "rootfs.cpio"
tmpDir = "/tmp"
interval = 5 * time.Second
shutdownTimeout = 30 * time.Second
)
type qemuVM struct {
@@ -78,7 +79,28 @@ func (v *qemuVM) Start() (err error) {
}
func (v *qemuVM) Stop() error {
return v.cmd.Process.Kill()
err := v.cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return fmt.Errorf("failed to send SIGTERM: %v", err)
}
done := make(chan error, 1)
go func() {
_, err := v.cmd.Process.Wait()
done <- err
}()
select {
case err := <-done:
return err
case <-time.After(shutdownTimeout):
err := v.cmd.Process.Kill()
if err != nil {
return fmt.Errorf("failed to kill process: %v", err)
}
}
return nil
}
func (v *qemuVM) SetProcess(pid int) error {