Skip to content

Commit

Permalink
added support for cancelling a running task
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Jackson committed Sep 27, 2024
1 parent e350219 commit dc1c985
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 17 deletions.
4 changes: 0 additions & 4 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,4 @@ func main() {
}
time.Sleep(10 * time.Second)
}
// list all pending tasks from the database with a type of crafting
// if no results continue
// if results get the first array record
// for number of quantity do crafting task
}
9 changes: 9 additions & 0 deletions internal/controllers/combat.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func CompleteCombatOrder(task models.Task) (string, error) {
fmt.Printf("%s fought %s and %s. It dropped %v. Fight number %d\n", *artifacts.Client.CharacterName, task.Monster, resp.Data.Fight.Result, resp.Data.Fight.Drops, i)
time.Sleep(utils.CalculateTimeDifference(resp.Data.Cooldown.StartedAt, resp.Data.Cooldown.Expiration))
}
updatedTask, err := database.Client.GetTask(task.Id)
if err != nil {
utils.Logger.Error("failed to get task status", zap.String("task", task.Id), zap.Error(err))
return "failed to get task status", err
}
if updatedTask.Status == models.TaskStatusCancelled {
utils.Logger.Info("task cancelling due to cancelled status", zap.String("task", task.Id))
break
}
}
return "", nil
}
9 changes: 9 additions & 0 deletions internal/controllers/crafting.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func CompleteCraftingOrder(task models.Task) (string, error) {
if err != nil {
return "failed to withdraw items", err
}
updatedTask, err := database.Client.GetTask(task.Id)
if err != nil {
utils.Logger.Error("failed to get task status", zap.String("task", task.Id), zap.Error(err))
return "failed to get task status", err
}
if updatedTask.Status == models.TaskStatusCancelled {
utils.Logger.Info("task cancelling due to cancelled status", zap.String("task", task.Id))
break
}
}
return "", nil
}
1 change: 1 addition & 0 deletions internal/models/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const (
TroutX int = 7
TroutY int = 12
ItemRetrieved string = "item_retrieved"
TaskCancelled string = "task_cancelled"
TaskCreated string = "task_created"
TaskRetrieved string = "task_retrieved"
WeaponcraftingWorkshop string = "weaponcrafting_workshop"
Expand Down
32 changes: 19 additions & 13 deletions internal/models/task.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package models

const (
TaskStatusPending = "pending"
TaskStatusRunning = "running"
TaskStatusSuccess = "success"
TaskStatusError = "error"
TaskStatusCancelled = "cancelled"
TaskStatusPending = "pending"
TaskStatusRunning = "running"
TaskStatusSuccess = "success"
TaskStatusError = "error"
)

type CancelTask struct {
Reason string `json:"reason"`
}

type CreateTask struct {
Action string `json:"action,omitempty"`
ActionCategory string `json:"actionCategory"`
Expand All @@ -17,13 +22,14 @@ type CreateTask struct {
}

type Task struct {
Id string `bson:"_id" json:"id"`
Action string `bson:"action,omitempty" json:"action,omitempty"`
ActionCategory string `bson:"actionCategory" json:"actionCategory"`
Item string `bson:"item,omitempty" json:"item,omitempty"`
Monster string `bson:"monster,omitempty" json:"monster,omitempty"`
Quantity int `bson:"quantity" json:"quantity"`
Status string `bson:"status" json:"status"`
ErrorReason string `bson:"errorReason" json:"errorReason"`
Character string `bson:"character" json:"character"`
Id string `bson:"_id" json:"id"`
Action string `bson:"action,omitempty" json:"action,omitempty"`
ActionCategory string `bson:"actionCategory" json:"actionCategory"`
Item string `bson:"item,omitempty" json:"item,omitempty"`
Monster string `bson:"monster,omitempty" json:"monster,omitempty"`
Quantity int `bson:"quantity" json:"quantity"`
QuantityCompleted int `bson:"quantityCompleted" json:"quantityCompleted"`
Status string `bson:"status" json:"status"`
ErrorReason string `bson:"errorReason" json:"errorReason"`
Character string `bson:"character" json:"character"`
}
26 changes: 26 additions & 0 deletions internal/routes/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,38 @@ func GenerateTaskRoutes(router *gin.Engine) {
taskRoutes := router.Group("/v1/tasks")
taskRoutes.Use(middleware.ValidateTransactionId())
{
taskRoutes.POST("/:id/cancel", cancelTask)
taskRoutes.POST("", createTask)
taskRoutes.GET("", listTasks)
taskRoutes.GET(":id", getTask)
}
}

func cancelTask(c *gin.Context) {
var request models.CancelTask
transactionId, _ := utils.GetTransactionIdHeader(c)
id := c.Param("id")
if err := c.ShouldBindJSON(&request); err != nil {
utilErr := utils.GenerateError(models.TaskCancelled, utils.InvalidRequestBody, http.StatusBadRequest, transactionId.TransactionId, err)
utils.WriteErrorLog(utilErr)
c.AbortWithStatusJSON(utilErr.ExternalError.Code, utilErr.ExternalError)
return
}
err := database.Client.UpdateTask(id, request.Reason, models.TaskStatusCancelled)
if errors.Is(err, utils.ErrTaskNotFound) {
utilErr := utils.GenerateError(models.TaskCancelled, utils.TaskNotFound, http.StatusNotFound, transactionId.TransactionId, nil)
utils.WriteErrorLog(utilErr)
c.AbortWithStatusJSON(utilErr.ExternalError.Code, utilErr.ExternalError)
return
} else if err != nil {
utilErr := utils.GenerateError(models.TaskCancelled, utils.GenericInternalServerErrorMessage, http.StatusInternalServerError, transactionId.TransactionId, err)
utils.WriteErrorLog(utilErr)
c.AbortWithStatusJSON(utilErr.ExternalError.Code, utilErr.ExternalError)
return
}
c.Status(http.StatusNoContent)
}

func createTask(c *gin.Context) {
var request models.CreateTask
transactionId, _ := utils.GetTransactionIdHeader(c)
Expand Down

0 comments on commit dc1c985

Please sign in to comment.