Store app PID in sidecar metadata. (#1200)

* Store app PID in sidecar metadata.

Signed-off-by: Phillip Hoff <phillip@orst.edu>

* Move process check under command check.

Signed-off-by: Phillip Hoff <phillip@orst.edu>

* Add minimal list test appId changes.

Signed-off-by: Phillip Hoff <phillip@orst.edu>

* Reverse params.

Signed-off-by: Phillip Hoff <phillip@orst.edu>

* Proper type conversion.

Signed-off-by: Phillip Hoff <phillip@orst.edu>

---------

Signed-off-by: Phillip Hoff <phillip@orst.edu>
Co-authored-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
This commit is contained in:
Phillip Hoff 2023-02-01 22:15:23 -08:00 committed by GitHub
parent 42f9519eb2
commit 50d5af4cd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View File

@ -360,6 +360,14 @@ dapr run --run-file /path/to/directory
}
if output.AppCMD != nil {
if output.AppCMD.Process != nil {
print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Updating metadata for appPID: %d", output.AppCMD.Process.Pid))
err = metadata.Put(output.DaprHTTPPort, "appPID", strconv.Itoa(output.AppCMD.Process.Pid), appID, unixDomainSocket)
if err != nil {
print.WarningStatusEvent(os.Stdout, "Could not update sidecar metadata for appPID: %s", err.Error())
}
}
appCommand := strings.Join(args, " ")
print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Updating metadata for app command: %s", appCommand))
err = metadata.Put(output.DaprHTTPPort, "appCommand", appCommand, appID, unixDomainSocket)
@ -526,7 +534,12 @@ func executeRun(runFilePath string, apps []runfileconfig.App) (bool, error) {
if runState.AppCMD.Command != nil {
putAppCommandInMeta(runConfig, runState)
if runState.AppCMD.Command.Process != nil {
putAppProcessIDInMeta(runState)
}
}
print.StatusEvent(runState.DaprCMD.OutputWriter, print.LogSuccess, "You're up and running! Dapr logs will appear here.\n")
logInformationalStatusToStdout(app)
}
@ -907,6 +920,14 @@ func putCLIProcessIDInMeta(runE *runExec.RunExec, pid int) {
}
}
func putAppProcessIDInMeta(runE *runExec.RunExec) {
print.StatusEvent(runE.DaprCMD.OutputWriter, print.LogInfo, "Updating metadata for appPID: %d", runE.AppCMD.Command.Process.Pid)
err := metadata.Put(runE.DaprHTTPPort, "appPID", strconv.Itoa(runE.AppCMD.Command.Process.Pid), runE.AppID, unixDomainSocket)
if err != nil {
print.StatusEvent(runE.DaprCMD.OutputWriter, print.LogWarning, "Could not update sidecar metadata for appPID: %s", err.Error())
}
}
// putAppCommandInMeta puts the app command in metadata so that it can be used by the CLI to stop the app.
func putAppCommandInMeta(runConfig standalone.RunConfig, runE *runExec.RunExec) {
appCommand := strings.Join(runConfig.Command, " ")
@ -916,7 +937,6 @@ func putAppCommandInMeta(runConfig standalone.RunConfig, runE *runExec.RunExec)
print.StatusEvent(runE.DaprCMD.OutputWriter, print.LogWarning, "Could not update sidecar metadata for appCommand: %s", err.Error())
return
}
print.StatusEvent(runE.DaprCMD.OutputWriter, print.LogSuccess, "You're up and running! Dapr logs will appear here.\n")
}
// putRunFilePathInMeta puts the absolute path of run file in metadata so that it can be used by the CLI to stop all apps started by this run file.

View File

@ -39,6 +39,7 @@ type ListOutput struct {
Created string `csv:"CREATED" json:"created" yaml:"created"`
DaprdPID int `csv:"DAPRD PID" json:"daprdPid" yaml:"daprdPid"`
CliPID int `csv:"CLI PID" json:"cliPid" yaml:"cliPid"`
AppPID int `csv:"APP PID" json:"appPid" yaml:"appPid"`
MaxRequestBodySize int `csv:"-" json:"maxRequestBodySize" yaml:"maxRequestBodySize"` // Additional field, not displayed in table.
HTTPReadBufferSize int `csv:"-" json:"httpReadBufferSize" yaml:"httpReadBufferSize"` // Additional field, not displayed in table.
RunTemplatePath string `csv:"RUN_TEMPLATE_PATH" json:"runTemplatePath" yaml:"runTemplatePath"`
@ -99,16 +100,23 @@ func List() ([]ListOutput, error) {
appID := argumentsMap["--app-id"]
appCmd := ""
appPIDString := ""
cliPIDString := ""
runTemplatePath := ""
socket := argumentsMap["--unix-domain-socket"]
appMetadata, err := metadata.Get(httpPort, appID, socket)
if err == nil {
appCmd = appMetadata.Extended["appCommand"]
appPIDString = appMetadata.Extended["appPID"]
cliPIDString = appMetadata.Extended["cliPID"]
runTemplatePath = appMetadata.Extended["runTemplatePath"]
}
appPID, err := strconv.Atoi(appPIDString)
if err != nil {
appPID = 0
}
// Parse functions return an error on bad input.
cliPID, err := strconv.Atoi(cliPIDString)
if err != nil {
@ -130,6 +138,7 @@ func List() ([]ListOutput, error) {
DaprdPID: daprPID,
CliPID: cliPID,
AppID: appID,
AppPID: appPID,
HTTPPort: httpPort,
GRPCPort: grpcPort,
AppPort: appPort,

View File

@ -123,7 +123,7 @@ func listOutputCheck(t *testing.T, output string, isCli bool) {
// only one app is runnning at this time
fields := strings.Fields(lines[0])
// Fields splits on space, so Created time field might be split again
assert.GreaterOrEqual(t, len(fields), 4, "expected at least 4 fields in components output")
assert.GreaterOrEqual(t, len(fields), 10, "expected at least 10 fields in components output")
if isCli {
assert.Equal(t, "dapr_e2e_list", fields[0], "expected name to match")
} else {
@ -132,6 +132,7 @@ func listOutputCheck(t *testing.T, output string, isCli bool) {
assert.Equal(t, "3555", fields[1], "expected http port to match")
assert.Equal(t, "4555", fields[2], "expected grpc port to match")
assert.Equal(t, "0", fields[3], "expected app port to match")
assert.NotEmpty(t, fields[9], "expected an app PID (a real value or zero)")
}
func listJsonOutputCheck(t *testing.T, output string) {
@ -146,6 +147,7 @@ func listJsonOutputCheck(t *testing.T, output string) {
assert.Equal(t, 3555, int(result[0]["httpPort"].(float64)), "expected http port to match")
assert.Equal(t, 4555, int(result[0]["grpcPort"].(float64)), "expected grpc port to match")
assert.Equal(t, 0, int(result[0]["appPort"].(float64)), "expected app port to match")
assert.GreaterOrEqual(t, int(result[0]["appPid"].(float64)), 0, "expected an app PID (a real value or zero)")
}
func listYamlOutputCheck(t *testing.T, output string) {
@ -160,4 +162,5 @@ func listYamlOutputCheck(t *testing.T, output string) {
assert.Equal(t, 3555, result[0]["httpPort"], "expected http port to match")
assert.Equal(t, 4555, result[0]["grpcPort"], "expected grpc port to match")
assert.Equal(t, 0, result[0]["appPort"], "expected app port to match")
assert.GreaterOrEqual(t, result[0]["appPid"], 0, "expected an app PID (a real value or zero)")
}