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

✨ Add bulk cancel functionality for analyses #769

Merged
merged 1 commit into from
Jan 17, 2025
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
45 changes: 38 additions & 7 deletions api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
TaskBucketContentRoot = TaskBucketRoot + "/*" + Wildcard
TaskSubmitRoot = TaskRoot + "/submit"
TaskCancelRoot = TaskRoot + "/cancel"
TaskCancelListRoot = TasksRoot + "/cancel/list"
)

const (
Expand Down Expand Up @@ -59,6 +60,7 @@ func (h TaskHandler) AddRoutes(e *gin.Engine) {
// Actions
routeGroup.PUT(TaskSubmitRoot, Transaction, h.Submit)
routeGroup.PUT(TaskCancelRoot, h.Cancel)
routeGroup.PUT(TaskCancelListRoot, h.CancelList)
// Bucket
routeGroup = e.Group("/")
routeGroup.Use(Required("tasks.bucket"))
Expand Down Expand Up @@ -468,13 +470,13 @@ func (h TaskHandler) Submit(ctx *gin.Context) {
h.Update(ctx)
}

// Cancel godoc
// @summary Cancel a task.
// @description Cancel a task.
// @tags tasks
// @success 202
// @router /tasks/{id}/cancel [put]
// @param id path int true "Task ID"
// // Cancel godoc
// // @summary Cancel a task.
// // @description Cancel a task.
// // @tags tasks
// // @success 202
// // @router /tasks/{id}/cancel [put]
// // @param id path int true "Task ID"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why were the additional // added? ^

func (h TaskHandler) Cancel(ctx *gin.Context) {
id := h.pk(ctx)
rtx := RichContext(ctx)
Expand All @@ -487,6 +489,35 @@ func (h TaskHandler) Cancel(ctx *gin.Context) {
h.Status(ctx, http.StatusAccepted)
}

// CancelList godoc
// @summary Cancel multiple tasks.
// @description Cancel multiple tasks by IDs.
// @tags tasks
// @success 202
// @router /tasks/cancel/list [put]
Copy link
Contributor

@jortel jortel Jan 16, 2025

Choose a reason for hiding this comment

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

Semantically, I think it would be more correct for the PUT to be against the collection because the action is updating the collection. Ex: PUT /tasks/cancel (without the /list ). The collection implies multiple objects.

Better yet, PUT /tasks/cancel?filter=id:[1,2,3]. This would be the most RESTful and more powerful. For example, it would support selection of tasks to be canceled by task kind and state. Example: /tasks/cancel?filter=kind:discovery,state=Running. or all tasks for an application
/tasks/cancel?filter=application.id=44.

// @param tasks body []uint true "List of Task IDs"
func (h TaskHandler) CancelList(ctx *gin.Context) {
ids := []uint{}

if err := h.Bind(ctx, &ids); err != nil {
_ = ctx.Error(err)
return
}

rtx := RichContext(ctx)

for _, id := range ids {
err := rtx.TaskManager.Cancel(h.DB(ctx), id)
if err != nil {
_ = ctx.Error(err)
return
}
}

h.Status(ctx, http.StatusAccepted)
}


// BucketGet godoc
// @summary Get bucket content by ID and path.
// @description Get bucket content by ID and path.
Expand Down
6 changes: 6 additions & 0 deletions binding/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (h *Task) List() (list []api.Task, err error) {
return
}

// CancelMultipleTasks - Cancel multiple tasks by their IDs.
func (h *Task) CancelMultipleTasks(ids []uint) (err error) {
err = h.client.Put(api.TaskCancelListRoot, ids)
return
}

// Update a Task.
func (h *Task) Update(r *api.Task) (err error) {
path := Path(api.TaskRoot).Inject(Params{api.ID: r.ID})
Expand Down
Loading