From 80adb5c09316b73d655db72025db113a3008cf55 Mon Sep 17 00:00:00 2001 From: MiriSafra Date: Sun, 8 Dec 2024 20:36:02 +0200 Subject: [PATCH] add function for bulk cancel Signed-off-by: MiriSafra --- api/task.go | 45 ++++++++++++++++++++++++++++++++++++++------- binding/task.go | 6 ++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/api/task.go b/api/task.go index 74600e687..b749c8b52 100644 --- a/api/task.go +++ b/api/task.go @@ -32,6 +32,7 @@ const ( TaskBucketContentRoot = TaskBucketRoot + "/*" + Wildcard TaskSubmitRoot = TaskRoot + "/submit" TaskCancelRoot = TaskRoot + "/cancel" + TaskCancelListRoot = TasksRoot + "/cancel/list" ) const ( @@ -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")) @@ -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" func (h TaskHandler) Cancel(ctx *gin.Context) { id := h.pk(ctx) rtx := RichContext(ctx) @@ -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] +// @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. diff --git a/binding/task.go b/binding/task.go index 623bd2bcc..af581b4d9 100644 --- a/binding/task.go +++ b/binding/task.go @@ -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})