-
Notifications
You must be signed in to change notification settings - Fork 35
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 /tasks patch; return 202 for task actions and updates. #660
Changes from all commits
c5319cb
acc870e
2b5f533
921ce47
11a94c2
290edd6
3ce2053
35316c8
94a8292
847bd93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ const ( | |
) | ||
|
||
const ( | ||
LocatorParam = "locator" | ||
Submit = "submit" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hint: LocatorParam unused. The /tasks now supports filtering. |
||
// TaskHandler handles task routes. | ||
|
@@ -55,10 +55,11 @@ func (h TaskHandler) AddRoutes(e *gin.Engine) { | |
routeGroup.POST(TasksRoot, h.Create) | ||
routeGroup.GET(TaskRoot, h.Get) | ||
routeGroup.PUT(TaskRoot, h.Update) | ||
routeGroup.PATCH(TaskRoot, Transaction, h.Update) | ||
routeGroup.DELETE(TaskRoot, h.Delete) | ||
routeGroup.GET(TasksReportQueueRoot, h.Queued) | ||
// Actions | ||
routeGroup.PUT(TaskSubmitRoot, h.Submit, h.Update) | ||
routeGroup.PUT(TaskSubmitRoot, Transaction, h.Submit) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hint: with complexity delegated to the task manager, simpler to implement submit as a patch and no longer chain submit; state=Ready; update(). |
||
routeGroup.PUT(TaskCancelRoot, h.Cancel) | ||
// Bucket | ||
routeGroup = e.Group("/") | ||
|
@@ -335,73 +336,66 @@ func (h TaskHandler) Delete(ctx *gin.Context) { | |
// @description Update a task. | ||
// @tags tasks | ||
// @accept json | ||
// @success 204 | ||
// @success 202 | ||
// @router /tasks/{id} [put] | ||
// @param id path int true "Task ID" | ||
// @param task body Task true "Task data" | ||
func (h TaskHandler) Update(ctx *gin.Context) { | ||
id := h.pk(ctx) | ||
m := &model.Task{} | ||
err := h.DB(ctx).First(m, id).Error | ||
if err != nil { | ||
_ = ctx.Error(err) | ||
return | ||
} | ||
r := &Task{} | ||
err := h.Bind(ctx, r) | ||
if ctx.Request.Method == http.MethodPatch && | ||
ctx.Request.ContentLength > 0 { | ||
r.With(m) | ||
} | ||
err = h.Bind(ctx, r) | ||
if err != nil { | ||
_ = ctx.Error(err) | ||
return | ||
} | ||
r.ID = id | ||
if _, found := ctx.Get(Submit); found { | ||
r.State = tasking.Ready | ||
} | ||
m = r.Model() | ||
m.ID = id | ||
m.UpdateUser = h.CurrentUser(ctx) | ||
rtx := WithContext(ctx) | ||
task := &tasking.Task{} | ||
task.With(r.Model()) | ||
task.UpdateUser = h.BaseHandler.CurrentUser(ctx) | ||
task.With(m) | ||
err = rtx.TaskManager.Update(h.DB(ctx), task) | ||
if err != nil { | ||
_ = ctx.Error(err) | ||
return | ||
} | ||
|
||
h.Status(ctx, http.StatusNoContent) | ||
h.Status(ctx, http.StatusAccepted) | ||
} | ||
|
||
// Submit godoc | ||
// @summary Submit a task. | ||
// @description Submit a task. | ||
// @description Patch and submit a task. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hint: Much better for the caller to be able to patch a task when submitting rather than update. This will support submit with minimal or no patch. |
||
// @tags tasks | ||
// @accept json | ||
// @success 204 | ||
// @success 202 | ||
// @router /tasks/{id}/submit [put] | ||
// @param id path int true "Task ID" | ||
// @param task body Task false "Task data (optional)" | ||
func (h TaskHandler) Submit(ctx *gin.Context) { | ||
id := h.pk(ctx) | ||
r := &Task{} | ||
err := h.findRefs(ctx, r) | ||
if err != nil { | ||
_ = ctx.Error(err) | ||
return | ||
} | ||
mod := func(withBody bool) (err error) { | ||
if !withBody { | ||
m := r.Model() | ||
err = h.DB(ctx).First(m, id).Error | ||
if err != nil { | ||
return | ||
} | ||
r.With(m) | ||
} | ||
r.State = tasking.Ready | ||
return | ||
} | ||
err = h.modBody(ctx, r, mod) | ||
if err != nil { | ||
_ = ctx.Error(err) | ||
return | ||
} | ||
ctx.Next() | ||
ctx.Set(Submit, true) | ||
ctx.Request.Method = http.MethodPatch | ||
h.Update(ctx) | ||
} | ||
|
||
// Cancel godoc | ||
// @summary Cancel a task. | ||
// @description Cancel a task. | ||
// @tags tasks | ||
// @success 204 | ||
// @success 202 | ||
// @router /tasks/{id}/cancel [put] | ||
// @param id path int true "Task ID" | ||
func (h TaskHandler) Cancel(ctx *gin.Context) { | ||
|
@@ -413,7 +407,7 @@ func (h TaskHandler) Cancel(ctx *gin.Context) { | |
return | ||
} | ||
|
||
h.Status(ctx, http.StatusNoContent) | ||
h.Status(ctx, http.StatusAccepted) | ||
} | ||
|
||
// BucketGet godoc | ||
|
@@ -735,7 +729,6 @@ type Task struct { | |
TTL TTL `json:"ttl,omitempty" yaml:",omitempty"` | ||
Data any `json:"data,omitempty" yaml:",omitempty"` | ||
Application *Ref `json:"application,omitempty" yaml:",omitempty"` | ||
Actions []string `json:"actions,omitempty" yaml:",omitempty"` | ||
Bucket *Ref `json:"bucket,omitempty" yaml:",omitempty"` | ||
Pod string `json:"pod,omitempty" yaml:",omitempty"` | ||
Retries int `json:"retries,omitempty" yaml:",omitempty"` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hint: removed in favor of simpler, more direct approach.