Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

internal/{server,cli}: Attempt to forcefully cancel a job #3102

Merged
merged 4 commits into from
Mar 15, 2022
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
3 changes: 3 additions & 0 deletions .changelog/3102.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Introduce a `-dangerously-force` flag to attempt to force cancel a job
```
31 changes: 29 additions & 2 deletions internal/cli/job_cancel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"time"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand All @@ -13,7 +15,7 @@ import (
type JobCancelCommand struct {
*baseCommand

flagJson bool
flagForce bool
}

func (c *JobCancelCommand) Run(args []string) int {
Expand All @@ -35,6 +37,15 @@ func (c *JobCancelCommand) Run(args []string) int {
jobId = c.args[0]
}

if c.flagForce {
c.ui.Output("You requested to use force to cancel a job! Be aware that this "+
"operation is dangerous and could result in some bad behavior or failure modes in Waypoint.",
terminal.WithWarningStyle())
c.ui.Output("If this is not your intention, ctrl-c now! The CLI will sleep for 3 seconds...",
terminal.WithWarningStyle())
time.Sleep(3 * time.Second)
}

sg := c.ui.StepGroup()
defer sg.Wait()

Expand All @@ -43,6 +54,7 @@ func (c *JobCancelCommand) Run(args []string) int {

_, err := c.project.Client().CancelJob(ctx, &pb.CancelJobRequest{
JobId: jobId,
Force: c.flagForce,
})
if err != nil {
s.Update("Failed to marked job %q for cancellation", jobId)
Expand All @@ -59,14 +71,29 @@ func (c *JobCancelCommand) Run(args []string) int {
return 1
}

s.Update("Marked job %q for cancellation", jobId)
if !c.flagForce {
s.Update("Marked job %q for cancellation", jobId)
} else {
s.Update("Forcefully marked job %q for cancellation", jobId)
s.Status(terminal.StatusWarn)
}
s.Done()

return 0
}

func (c *JobCancelCommand) Flags() *flag.Sets {
return c.flagSet(flagSetOperation, func(set *flag.Sets) {
f := set.NewSet("Command Options")
f.BoolVar(&flag.BoolVar{
Name: "dangerously-force",
Target: &c.flagForce,
Default: false,
Usage: "Will forcefully cancel the job. This will immediately mark the " +
"job as complete in the server, regardless of the real job status. This " +
"may leave dangling resources or cause concurrency issues if the underlying " +
"job doesn't gracefully cancel. USE WITH CAUTION.",
})
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/singleprocess/service_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *service) CancelJob(
ctx context.Context,
req *pb.CancelJobRequest,
) (*empty.Empty, error) {
if err := s.state.JobCancel(req.JobId, false); err != nil {
if err := s.state.JobCancel(req.JobId, req.Force); err != nil {
return nil, err
}

Expand Down
Loading