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

Make CLI exit with error code if dapr run or invoke fails #905

Merged
merged 14 commits into from
Mar 9, 2022

Conversation

shubham1172
Copy link
Member

@shubham1172 shubham1172 commented Mar 2, 2022

Signed-off-by: Shubham Sharma shubhash@microsoft.com

Description

This PR looks out for errors from Dapr command and app command, and bubbles them to the Dapr CLI.
It also adds error code for dapr invoke failures.

Scenario 1

Taken from the original issue, run Dapr without installing it.

Before

$ dapr run test
❌  stat /home/shubham1172/.dapr/components: no such file or directory
$ echo $>
0

Now

$ dapr run test
❌  stat /home/shubham1172/.dapr/components: no such file or directory
$ echo $>
1

Scenario 2

Run an app that exits with a error code.

Say an example script:

#!/usr/bin/env bash
exit 1

Before

CLI logs
dapr run --log-level debug ./test.sh
ℹ️  Starting Dapr with id Browstream-Fancier. HTTP Port: 35199. gRPC Port: 35617
ℹ️  Checking if Dapr sidecar is listening on HTTP port 35199
INFO[0000] starting Dapr Runtime -- version 1.6.0 -- commit 4bb25fab444c4f1a1bf0ffd74293dbd4fdcea580  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] log level set to: info                        app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] metrics server started on :41729/             app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.metrics type=log ver=1.6.0
INFO[0000] standalone mode configured                    app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] app id: Browstream-Fancier                    app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] mTLS is disabled. Skipping certificate request and tls validation  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] local service entry announced: Browstream-Fancier -> 172.17.71.111:36935  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.contrib type=log ver=1.6.0
INFO[0000] Initialized name resolution to mdns           app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] loading components                            app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] component loaded. name: pubsub, type: pubsub.redis/v1  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] waiting for all outstanding components to be processed  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] detected actor state store: statestore        app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] component loaded. name: statestore, type: state.redis/v1  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] all outstanding components processed          app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] enabled gRPC tracing middleware               app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.api type=log ver=1.6.0
INFO[0000] enabled gRPC metrics middleware               app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.api type=log ver=1.6.0
INFO[0000] API gRPC server is running on port 35617      app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] enabled metrics http middleware               app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.http type=log ver=1.6.0
INFO[0000] enabled tracing http middleware               app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.http type=log ver=1.6.0
INFO[0000] http server is running on port 35199          app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] The request body size parameter is: 4         app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] enabled gRPC tracing middleware               app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.internal type=log ver=1.6.0
INFO[0000] enabled gRPC metrics middleware               app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.internal type=log ver=1.6.0
INFO[0000] internal gRPC server is running on port 36935  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
WARN[0000] app channel is not initialized. did you make sure to configure an app-port?  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor type=log ver=1.6.0
WARN[0000] app channel not initialized, make sure -app-port is specified if pubsub subscription is required  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
WARN[0000] failed to read from bindings: app channel not initialized   app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] dapr initialized. Status: Running. Init Elapsed 8.2562ms  app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ
 scope=dapr.runtime type=log ver=1.6.0
INFO[0000] placement tables updated, version: 0          app_id=Browstream-Fancier instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor.internal.placement type=log ver=1.6.0
ℹ️  Checking if Dapr sidecar is listening on GRPC port 35617
ℹ️  Dapr sidecar is up and running.
❌  The App process exited with error code: exit status 1
ℹ️  Updating metadata for app command: ./test.sh
✅  You're up and running! Both Dapr and your app logs will appear here.

ℹ️
terminated signal received: shutting down
✅  Exited Dapr successfully
echo $? 
0

Now

CLI logs
dapr run --log-level debug ./test.sh
ℹ️  Starting Dapr with id Racerindigo-Guardian. HTTP Port: 43825. gRPC Port: 39355
ℹ️  Checking if Dapr sidecar is listening on HTTP port 43825
INFO[0000] starting Dapr Runtime -- version 1.6.0 -- commit 4bb25fab444c4f1a1bf0ffd74293dbd4fdcea580  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] log level set to: debug                       app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] metrics server started on :39307/             app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.metrics type=log ver=1.6.0
INFO[0000] standalone mode configured                    app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] app id: Racerindigo-Guardian                  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] mTLS is disabled. Skipping certificate request and tls validation  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] local service entry announced: Racerindigo-Guardian -> 172.17.71.111:38227  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.contrib type=log ver=1.6.0
INFO[0000] Initialized name resolution to mdns           app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] loading components                            app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
DEBU[0000] found component. name: pubsub, type: pubsub.redis/v1  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
DEBU[0000] found component. name: statestore, type: state.redis/v1  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
DEBU[0000] loading component. name: pubsub, type: pubsub.redis/v1  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] component loaded. name: pubsub, type: pubsub.redis/v1  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
DEBU[0000] loading component. name: statestore, type: state.redis/v1  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] waiting for all outstanding components to be processed  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] detected actor state store: statestore        app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] component loaded. name: statestore, type: state.redis/v1  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] all outstanding components processed          app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] enabled gRPC tracing middleware               app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.api type=log ver=1.6.0
INFO[0000] enabled gRPC metrics middleware               app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.api type=log ver=1.6.0
INFO[0000] API gRPC server is running on port 39355      app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] enabled metrics http middleware               app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.http type=log ver=1.6.0
INFO[0000] enabled tracing http middleware               app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.http type=log ver=1.6.0
INFO[0000] http server is running on port 43825          app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] The request body size parameter is: 4         app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] enabled gRPC tracing middleware               app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.internal type=log ver=1.6.0
INFO[0000] enabled gRPC metrics middleware               app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.grpc.internal type=log ver=1.6.0
INFO[0000] internal gRPC server is running on port 38227  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
WARN[0000] app channel is not initialized. did you make sure to configure an app-port?  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor type=log ver=1.6.0
WARN[0000] app channel not initialized, make sure -app-port is specified if pubsub subscription is required  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
DEBU[0000] try to connect to placement service: dns:///localhost:50005  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor.internal.placement type=log ver=1.6.0
WARN[0000] failed to read from bindings: app channel not initialized   app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
INFO[0000] dapr initialized. Status: Running. Init Elapsed 5.9643999999999995ms  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime type=log ver=1.6.0
DEBU[0000] established connection to placement service at dns:///localhost:50005  app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor.internal.placement type=log ver=1.6.0
DEBU[0000] placement order received: lock                app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor.internal.placement type=log ver=1.6.0
DEBU[0000] placement order received: update              app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor.internal.placement type=log ver=1.6.0
INFO[0000] placement tables updated, version: 0          app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor.internal.placement type=log ver=1.6.0
DEBU[0000] placement order received: unlock              app_id=Racerindigo-Guardian instance=LAPTOP-G7VJUBHJ scope=dapr.runtime.actor.internal.placement type=log ver=1.6.0
ℹ️  Checking if Dapr sidecar is listening on GRPC port 39355
ℹ️  Dapr sidecar is up and running.
❌  The App process exited with error code: exit status 1
ℹ️  Updating metadata for app command: ./test.sh
✅  You're up and running! Both Dapr and your app logs will appear here.

ℹ️
terminated signal received: shutting down
✅  Exited Dapr successfully
❌  Error exiting App: exit status 1
echo $? 
1

Issue reference

Please reference the issue this PR will close: #684

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
@shubham1172 shubham1172 requested review from a team as code owners March 2, 2022 07:45
@codecov
Copy link

codecov bot commented Mar 2, 2022

Codecov Report

Merging #905 (a952ad1) into master (050eb55) will increase coverage by 0.09%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #905      +/-   ##
==========================================
+ Coverage   22.89%   22.98%   +0.09%     
==========================================
  Files          29       29              
  Lines        1612     1614       +2     
==========================================
+ Hits          369      371       +2     
  Misses       1193     1193              
  Partials       50       50              
Impacted Files Coverage Δ
pkg/standalone/run.go 60.24% <100.00%> (+0.50%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 249d8f7...a952ad1. Read the comment docs.

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
@mukundansundar
Copy link
Collaborator

@shubham1172 if you are modifying the run command exit flow ... please make sure that all processes and child processes that have been spun are closed on exit.

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
@shubham1172
Copy link
Member Author

@mukundansundar I have added os.Exit to two flows.

  1. Errors thrown after validating configuration and commands - it used to simply return earlier, and now it exits with error code. Here, no other processes have been spun up yet.
  2. Whenever we get an event in sigCh. The flow itself is unaltered and we only check at the end if any error was thrown, and exit with error code. This only happens at the end when resources have already been cleaned up.

No premature exits have been added in place.

@shubham1172 shubham1172 changed the title [WIP] Make CLI exit with error code if dapr or app run fail Make CLI exit with error code if dapr or app run fail Mar 2, 2022
@pravinpushkar
Copy link
Contributor

pravinpushkar commented Mar 2, 2022

@shubham1172 I think we can add exit here also -

return

@shubham1172
Copy link
Member Author

@pravinpushkar nice, let me add it too. Thanks for the suggestion!

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
@shubham1172
Copy link
Member Author

Added the changes. /cc @mukundansundar @pravinpushkar can you please review it?

@shubham1172 shubham1172 changed the title Make CLI exit with error code if dapr or app run fail Make CLI exit with error code if dapr run or invoke fails Mar 2, 2022
if output.AppErr != nil {
exitWithError = true
print.FailureStatusEvent(os.Stderr, fmt.Sprintf("Error exiting App: %s", output.AppErr))
} else if output.AppCMD != nil && (output.AppCMD.ProcessState == nil || !output.AppCMD.ProcessState.Exited()) {
err = output.AppCMD.Process.Kill()
if err != nil {
print.FailureStatusEvent(os.Stderr, fmt.Sprintf("Error exiting App: %s", err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubham1172 If it errors out here and line number 316 then exitWithError will not be set. Is this expected ? or it is not possible to have this behavior. Just wanted to bring it your notice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @pravinpushkar, that was an existing behavior that I haven't changed with this PR. Now that I think more about it I think we should exit with error in that scenario too. What do you think?
cc @mukundansundar

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added the same to this PR!

@@ -83,7 +83,7 @@ dapr invoke --unix-domain-socket --app-id target --method sample --verb GET
if err != nil {
err = fmt.Errorf("error invoking app %s: %s", invokeAppID, err)
print.FailureStatusEvent(os.Stderr, err.Error())
return
os.Exit(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubham1172 Can you add an e2e for this scenario?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukundansundar added the test.

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
Copy link
Contributor

@pravinpushkar pravinpushkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@mukundansundar mukundansundar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mukundansundar mukundansundar merged commit 9472e6d into dapr:master Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix exit with error for run command
3 participants