CLI run should detect that either daprd or the app has exited and clean up

This commit is contained in:
Charlie Stanley 2021-04-02 14:41:27 -07:00
parent 324fa5bc57
commit 328a6f99f1
2 changed files with 57 additions and 6 deletions

View File

@ -111,6 +111,18 @@ var RunCmd = &cobra.Command{
os.Exit(1)
}
go func() {
daprdErr := output.DaprCMD.Wait()
if daprdErr != nil {
print.FailureStatusEvent(os.Stdout, "The daprd process exited with error code: %s", daprdErr.Error())
} else {
print.SuccessStatusEvent(os.Stdout, "Exited Dapr successfully")
}
sigCh <- os.Interrupt
}()
if appPort <= 0 {
// If app does not listen to port, we can check for Dapr's sidecar health before starting the app.
// Otherwise, it creates a deadlock.
@ -182,6 +194,17 @@ var RunCmd = &cobra.Command{
return
}
go func() {
appErr := output.AppCMD.Wait()
if appErr != nil {
print.FailureStatusEvent(os.Stdout, "The App process exited with error code: %s", appErr.Error())
} else {
print.SuccessStatusEvent(os.Stdout, "Exited App successfully")
}
sigCh <- os.Interrupt
}()
appRunning <- true
}()
@ -219,14 +242,16 @@ var RunCmd = &cobra.Command{
<-sigCh
print.InfoStatusEvent(os.Stdout, "\nterminated signal received: shutting down")
err = output.DaprCMD.Process.Kill()
if err != nil {
print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting Dapr: %s", err))
} else {
print.SuccessStatusEvent(os.Stdout, "Exited Dapr successfully")
if output.DaprCMD.ProcessState == nil || !output.DaprCMD.ProcessState.Exited() {
err = output.DaprCMD.Process.Kill()
if err != nil {
print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting Dapr: %s", err))
} else {
print.SuccessStatusEvent(os.Stdout, "Exited Dapr successfully")
}
}
if output.AppCMD != nil {
if output.AppCMD != nil && (output.AppCMD.ProcessState == nil || !output.AppCMD.ProcessState.Exited()) {
err = output.AppCMD.Process.Kill()
if err != nil {
print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error exiting App: %s", err))

View File

@ -41,6 +41,7 @@ func TestStandaloneInstall(t *testing.T) {
phase func(*testing.T)
}{
{"test install", testInstall},
{"test run", testRun},
{"test uninstall", testUninstall},
}
@ -269,3 +270,28 @@ func testInstall(t *testing.T) {
assert.Empty(t, configs)
}
func testRun(t *testing.T) {
daprPath := getDaprPath()
output, err := spawn.Command(daprPath, "run", "--", "bash", "-c", "echo test")
t.Log(output)
require.NoError(t, err, "run failed")
assert.Contains(t, output, "Exited App successfully")
assert.Contains(t, output, "Exited Dapr successfully")
output, err = spawn.Command(daprPath, "run", "--", "bash", "-c", "exit 1")
t.Log(output)
require.NoError(t, err, "run failed")
assert.Contains(t, output, "The App process exited with error code: exit status 1")
assert.Contains(t, output, "Exited Dapr successfully")
// Test that the CLI exits on a daprd shutdown.
output, err = spawn.Command(daprPath, "run", "--dapr-http-port", "9999", "--", "bash", "-c", "curl -v http://localhost:9999/v1.0/shutdown; sleep 10; exit 1")
t.Log(output)
require.NoError(t, err, "run failed")
assert.Contains(t, output, "Exited App successfully", "App should be shutdown before it has a chance to return non-zero")
assert.Contains(t, output, "Exited Dapr successfully")
}