Skip to content

Commit

Permalink
add function for bulk cancel
Browse files Browse the repository at this point in the history
Signed-off-by: MiriSafra <[email protected]>
  • Loading branch information
MiriSafra committed Dec 8, 2024
1 parent f99f88d commit 80adb5c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
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"
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]
// @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

0 comments on commit 80adb5c

Please sign in to comment.