Skip to content

Commit ae94b5e

Browse files
committed
Graceful stop app and daprd
Signed-off-by: Albert Callarisa <[email protected]>
1 parent cef4d47 commit ae94b5e

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

cmd/run.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -819,24 +819,25 @@ func stopDaprdAndAppProcesses(runState *runExec.RunExec) bool {
819819

820820
exitWithError := false
821821

822-
daprErr := runState.DaprCMD.CommandErr
822+
appErr := runState.AppCMD.CommandErr
823823

824-
if daprErr != nil {
824+
if appErr != nil {
825825
exitWithError = true
826-
print.StatusEvent(runState.DaprCMD.ErrorWriter, print.LogFailure, "Error exiting Dapr: %s", daprErr)
827-
} else if runState.DaprCMD.Command.ProcessState == nil || !runState.DaprCMD.Command.ProcessState.Exited() {
828-
err = killDaprdProcess(runState)
826+
print.StatusEvent(runState.AppCMD.ErrorWriter, print.LogFailure, "Error exiting App: %s", appErr)
827+
} else if runState.AppCMD.Command != nil && (runState.AppCMD.Command.ProcessState == nil || !runState.AppCMD.Command.ProcessState.Exited()) {
828+
err = stopOrKillAppProcess(runState)
829829
if err != nil {
830830
exitWithError = true
831831
}
832832
}
833-
appErr := runState.AppCMD.CommandErr
834833

835-
if appErr != nil {
834+
daprErr := runState.DaprCMD.CommandErr
835+
836+
if daprErr != nil {
836837
exitWithError = true
837-
print.StatusEvent(runState.AppCMD.ErrorWriter, print.LogFailure, "Error exiting App: %s", appErr)
838-
} else if runState.AppCMD.Command != nil && (runState.AppCMD.Command.ProcessState == nil || !runState.AppCMD.Command.ProcessState.Exited()) {
839-
err = killAppProcess(runState)
838+
print.StatusEvent(runState.DaprCMD.ErrorWriter, print.LogFailure, "Error exiting Dapr: %s", daprErr)
839+
} else if runState.DaprCMD.Command.ProcessState == nil || !runState.DaprCMD.Command.ProcessState.Exited() {
840+
err = killDaprdProcess(runState)
840841
if err != nil {
841842
exitWithError = true
842843
}
@@ -992,7 +993,7 @@ func startDaprdProcess(runConfig *standalone.RunConfig, runE *runExec.RunExec,
992993

993994
// killDaprdProcess is used to kill the Daprd process and return error on failure.
994995
func killDaprdProcess(runE *runExec.RunExec) error {
995-
err := runE.DaprCMD.Command.Process.Kill()
996+
err := runE.DaprCMD.StopGracefully()
996997
if err != nil {
997998
print.StatusEvent(runE.DaprCMD.ErrorWriter, print.LogFailure, "Error exiting Dapr: %s", err)
998999
return err
@@ -1001,12 +1002,12 @@ func killDaprdProcess(runE *runExec.RunExec) error {
10011002
return nil
10021003
}
10031004

1004-
// killAppProcess is used to kill the App process and return error on failure.
1005-
func killAppProcess(runE *runExec.RunExec) error {
1005+
// stopOrKillAppProcess is used to kill the App process and return error on failure.
1006+
func stopOrKillAppProcess(runE *runExec.RunExec) error {
10061007
if runE.AppCMD.Command == nil {
10071008
return nil
10081009
}
1009-
err := runE.AppCMD.Command.Process.Kill()
1010+
err := runE.AppCMD.StopGracefully()
10101011
if err != nil {
10111012
print.StatusEvent(runE.DaprCMD.ErrorWriter, print.LogFailure, "Error exiting App: %s", err)
10121013
return err

pkg/runexec/runexec.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ package runexec
1515

1616
import (
1717
"errors"
18+
"fmt"
1819
"io"
1920
"os"
2021
"os/exec"
22+
"syscall"
23+
"time"
2124

2225
"github.com/dapr/cli/pkg/runfileconfig"
2326
"github.com/dapr/cli/pkg/standalone"
@@ -105,6 +108,31 @@ func (c *CmdProcess) SetStderr() error {
105108
return nil
106109
}
107110

111+
func (c *CmdProcess) StopGracefully() error {
112+
waitChan := make(chan struct{}, 1)
113+
go func() {
114+
c.Command.Process.Wait()
115+
close(waitChan)
116+
}()
117+
118+
err := syscall.Kill(c.Command.Process.Pid, syscall.SIGTERM)
119+
if err != nil {
120+
return fmt.Errorf("error sending SIGTERM to App: %w", err)
121+
}
122+
select {
123+
case <-waitChan:
124+
return nil
125+
case <-time.After(10 * time.Second):
126+
}
127+
fmt.Println("ALBERT: Sending SIGKILL to process")
128+
err = c.Command.Process.Kill()
129+
if err != nil {
130+
return fmt.Errorf("error sending SIGKILL to App: %w", err)
131+
}
132+
return nil
133+
134+
}
135+
108136
func NewOutput(config *standalone.RunConfig) (*RunOutput, error) {
109137
// set default values from RunConfig struct's tag.
110138
config.SetDefaultFromSchema()

tests/e2e/spawn/spawn.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ func Command(command string, arguments ...string) (string, error) {
8080
// CommandExecWithContext runs a command with its arguments, kills the command after context is done
8181
// and returns the combined stdout, stderr or the error.
8282
func CommandExecWithContext(ctx context.Context, command string, arguments ...string) (string, error) {
83+
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
84+
defer cancel()
8385
cmd := exec.CommandContext(ctx, command, arguments...)
8486
var b bytes.Buffer
8587
cmd.Stdout = &b
@@ -101,17 +103,7 @@ func CommandExecWithContext(ctx context.Context, command string, arguments ...st
101103
return
102104
case <-time.After(20 * time.Second):
103105
if cmd.ProcessState == nil || !cmd.ProcessState.Exited() {
104-
syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
105-
}
106-
}
107-
select {
108-
case <-waitFinished:
109-
return
110-
case <-ctx.Done():
111-
return
112-
case <-time.After(10 * time.Second):
113-
if cmd.ProcessState == nil || !cmd.ProcessState.Exited() {
114-
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
106+
cmd.Process.Signal(syscall.SIGTERM)
115107
}
116108
}
117109
}()

0 commit comments

Comments
 (0)