Skip to content

Commit

Permalink
fix: world cardinal dev not exit on compilation error (#63)
Browse files Browse the repository at this point in the history
Closes: WORLD-1079

## Overview

Fix `world cardinal dev` is not exited when there is a compilation error, user don't need to send sigint/sigterm to exit the process.

## Brief Changelog

- add step to send signterm when `cmd.Exec` process is done / exited
- skip killing the `cmd.Exec` if process is already exited.

## Testing and Verifying

Manually tested by running `world cardinal dev`
  • Loading branch information
zulkhair committed May 2, 2024
1 parent 034511d commit 0ea5303
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ var devCmd = &cobra.Command{
return err
}

// Start a goroutine to check cmd is stopped
go func() {
err := execCmd.Wait()
if err != nil {
logger.Error(eris.Wrap(err, "Cardinal process stopped"))
}

// if process exited, send signal to StopChan
signalCh <- syscall.SIGTERM
}()

// Start a goroutine to listen for signals
go func() {
<-signalCh
Expand Down Expand Up @@ -255,6 +266,10 @@ func stopCardinal(cmd *exec.Cmd, watch bool) error {
return nil
}

if cmd.ProcessState == nil || cmd.ProcessState.Exited() {
return nil
}

// stop the cardinal process
if runtime.GOOS == "windows" {
err := cmd.Process.Kill()
Expand Down
4 changes: 2 additions & 2 deletions cmd/world/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestDev(t *testing.T) {
assert.NilError(t, err)

// Start cardinal dev
rootCmd.SetArgs([]string{"cardinal", "dev", "--watch"})
rootCmd.SetArgs([]string{"cardinal", "dev"})
go func() {
err := rootCmd.Execute()
assert.NilError(t, err)
Expand All @@ -238,7 +238,7 @@ func TestDev(t *testing.T) {
assert.Assert(t, isGameLoopRunning)

// Shutdown the program
close(cardinal.StopChan)
cardinal.StopChan <- struct{}{}

// Check cardinal health (expected error), trying for 10 times
count := 0
Expand Down

0 comments on commit 0ea5303

Please sign in to comment.