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

Adjust timeout messaging to be more correct on timeout errors with 0 retries #2012

Merged
merged 4 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions examples/component-actions/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ components:
- thing=stuff
# maxRetries is the number of times to retry the action if it fails
maxRetries: 0
# maxTotalSeconds is the maximum amount of times the action can run before it is killed, including retries
# maxTotalSeconds is the maximum amount of times the action can run before it is killed, over all retries
maxTotalSeconds: 30
# mute determine if actions output should be printed to the console
mute: false
Expand Down Expand Up @@ -145,12 +145,10 @@ components:
actions:
# runs during "zarf package deploy"
onDeploy:
# defaults allow you to specify default values for the actions in that acitonSet
# defaults allow you to specify default values for the actions in that actionSet
defaults:
# maxTotalSeconds is the maximum amount of time the action can run before it is killed, including retries
# maxTotalSeconds is the maximum amount of time the action can run before it is killed, over all retries
maxTotalSeconds: 1
# maxRetries is the maximum number of times the action will be retried on failure
maxRetries: 3
before:
# this action will fail after 1 second
- cmd: sleep 10
Expand Down
25 changes: 17 additions & 8 deletions src/pkg/packager/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (p *Packager) runAction(defaultCfg types.ZarfComponentActionDefaults, actio
timeout := time.After(duration)

// Keep trying until the max retries is reached.
retryCmd:
for remaining := cfg.MaxRetries + 1; remaining > 0; remaining-- {

// Perform the action run.
Expand Down Expand Up @@ -135,7 +136,7 @@ func (p *Packager) runAction(defaultCfg types.ZarfComponentActionDefaults, actio
if cfg.MaxTotalSeconds < 1 {
spinner.Updatef("Waiting for \"%s\" (no timeout)", cmdEscaped)
if err := tryCmd(context.TODO()); err != nil {
continue
continue retryCmd
}

return nil
Expand All @@ -144,23 +145,31 @@ func (p *Packager) runAction(defaultCfg types.ZarfComponentActionDefaults, actio
// Run the command on repeat until success or timeout.
spinner.Updatef("Waiting for \"%s\" (timeout: %ds)", cmdEscaped, cfg.MaxTotalSeconds)
select {
// On timeout abort.
// On timeout break the loop to abort.
case <-timeout:
cancel()
return fmt.Errorf("command \"%s\" timed out", cmdEscaped)
break retryCmd

// Otherwise, try running the command.
default:
ctx, cancel = context.WithTimeout(context.Background(), duration)
defer cancel()
if err := tryCmd(ctx); err == nil {
return nil
if err := tryCmd(ctx); err != nil {
continue retryCmd
}

return nil
}
}

// If we've reached this point, the retry limit has been reached.
return fmt.Errorf("command \"%s\" failed after %d retries", cmdEscaped, cfg.MaxRetries)
select {
case <-timeout:
// If we reached this point, the timeout was reached.
return fmt.Errorf("command \"%s\" timed out after %d seconds", cmdEscaped, cfg.MaxTotalSeconds)

default:
// If we reached this point, the retry limit was reached.
return fmt.Errorf("command \"%s\" failed after %d retries", cmdEscaped, cfg.MaxRetries)
}
}

// convertWaitToCmd will return the wait command if it exists, otherwise it will return the original command.
Expand Down
1 change: 1 addition & 0 deletions src/test/e2e/02_component_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestComponentActions(t *testing.T) {
// Deploy the simple action that should fail the timeout.
stdOut, stdErr, err = e2e.Zarf("package", "deploy", path, "--components=on-deploy-with-timeout", "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "after 1 second")
require.Contains(t, stdErr, "😭😭😭 this action failed because it took too long to run 😭😭😭")
})

Expand Down
Loading