Skip to content

Commit

Permalink
Merge pull request #3084 from buildkite/step-cancel-grace-period-arg
Browse files Browse the repository at this point in the history
Add `force-grace-period-seconds` argument to `step cancel` command
  • Loading branch information
mitchbne authored Nov 12, 2024
2 parents 8205ace + 9a50348 commit 2a013d1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
5 changes: 3 additions & 2 deletions api/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func (c *Client) StepUpdate(ctx context.Context, stepIdOrKey string, stepUpdate
}

type StepCancel struct {
Build string `json:"build_id"`
Force bool `json:"force,omitempty"`
Build string `json:"build_id"`
Force bool `json:"force"`
ForceGracePeriodSeconds int64 `json:"force_grace_period"`
}

type StepCancelResponse struct {
Expand Down
30 changes: 22 additions & 8 deletions clicommand/step_cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ Cancel all unfinished jobs for a step
Example:
$ buildkite-agent step cancel --step "key"
$ buildkite-agent step cancel --step "key" --force`
$ buildkite-agent step cancel --step "key" --force
$ buildkite-agent step cancel --step "key" --force --force-grace-period-seconds 30
`

type StepCancelConfig struct {
StepOrKey string `cli:"step" validate:"required"`
Force bool `cli:"force"`
Build string `cli:"build"`
StepOrKey string `cli:"step" validate:"required"`
Force bool `cli:"force"`
ForceGracePeriodSeconds int64 `cli:"force-grace-period-seconds"`
Build string `cli:"build"`

// Global flags
Debug bool `cli:"debug"`
Expand Down Expand Up @@ -63,10 +66,17 @@ var StepCancelCommand = cli.Command{
},
cli.BoolFlag{
Name: "force",
Usage: "Don't wait for the agent to finish before cancelling the jobs",
Usage: "Transition unfinished jobs to a canceled state instead of waiting for jobs to finish uploading artifacts",
EnvVar: "BUILDKITE_STEP_CANCEL_FORCE",
},

cli.Int64Flag{
Name: "force-grace-period-seconds",
Value: defaultCancelGracePeriod,
Usage: "The number of seconds to wait for agents to finish uploading artifacts before transitioning unfinished jobs to a canceled state. ′--force′ must also be supplied for this to take affect",
EnvVar: "BUILDKITE_FORCE_GRACE_PERIOD_SECONDS,BUILDKITE_CANCEL_GRACE_PERIOD",
},

// API Flags
AgentAccessTokenFlag,
EndpointFlag,
Expand All @@ -84,6 +94,10 @@ var StepCancelCommand = cli.Command{
ctx, cfg, l, _, done := setupLoggerAndConfig[StepCancelConfig](context.Background(), c)
defer done()

if cfg.ForceGracePeriodSeconds < 0 {
return fmt.Errorf("The value of ′--force-grace-period-seconds′ must be greater than or equal to 0")
}

return cancelStep(ctx, cfg, l)
},
}
Expand All @@ -92,10 +106,10 @@ func cancelStep(ctx context.Context, cfg StepCancelConfig, l logger.Logger) erro
// Create the API client
client := api.NewClient(l, loadAPIClientConfig(cfg, "AgentAccessToken"))

// Create the value to cancel
cancel := &api.StepCancel{
Build: cfg.Build,
Force: cfg.Force,
Build: cfg.Build,
Force: cfg.Force,
ForceGracePeriodSeconds: cfg.ForceGracePeriodSeconds,
}

// Post the change
Expand Down
20 changes: 11 additions & 9 deletions clicommand/step_cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ func TestStepCancel(t *testing.T) {
}))

cfg := StepCancelConfig{
Force: true,
Build: "1",
StepOrKey: "some-random-key",
AgentAccessToken: "agentaccesstoken",
Endpoint: server.URL,
ForceGracePeriodSeconds: 10,
Force: true,
Build: "1",
StepOrKey: "some-random-key",
AgentAccessToken: "agentaccesstoken",
Endpoint: server.URL,
}

l := logger.NewBuffer()
Expand All @@ -40,10 +41,11 @@ func TestStepCancel(t *testing.T) {
}))

cfg := StepCancelConfig{
Force: true,
StepOrKey: "some-random-key",
AgentAccessToken: "agentaccesstoken",
Endpoint: server.URL,
ForceGracePeriodSeconds: 10,
Force: true,
StepOrKey: "some-random-key",
AgentAccessToken: "agentaccesstoken",
Endpoint: server.URL,
}

l := logger.NewBuffer()
Expand Down

0 comments on commit 2a013d1

Please sign in to comment.