Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: world cardinal dev graceful shutdown #12

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions cmd/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package cardinal

import (
"fmt"
"github.com/magefile/mage/sh"
"github.com/spf13/cobra"
"os"
"os/exec"
"os/signal"
"pkg.world.dev/world-cli/tea/style"
"syscall"
"time"

"github.com/magefile/mage/sh"
"github.com/spf13/cobra"
"pkg.world.dev/world-cli/tea/style"
)

const (
Expand Down Expand Up @@ -46,7 +48,7 @@ var devCmd = &cobra.Command{
}

// Run Cardinal
execCmd, err := runCardinal()
cardinalExecCmd, err := runCardinal()
if err != nil {
return err
}
Expand All @@ -59,7 +61,7 @@ var devCmd = &cobra.Command{
cmdErr := make(chan error, 1)

go func() {
err := execCmd.Wait()
err := cardinalExecCmd.Wait()
cmdErr <- err
}()

Expand All @@ -71,9 +73,23 @@ var devCmd = &cobra.Command{
return errCleanup
}

err = execCmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return err
isProcessRunning := func(cmd *exec.Cmd) bool {
return cardinalExecCmd.ProcessState == nil && cardinalExecCmd.Process != nil
}

//wait up to 10 seconds for it to quit.
for i := 0; i < 10; i++ {
if isProcessRunning(cardinalExecCmd) {
time.Sleep(1 * time.Second)
} else {
break
}
}
if isProcessRunning(cardinalExecCmd) {
err = cardinalExecCmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return err
}
}

return nil
Expand Down