Skip to content

Commit

Permalink
queue sync calls
Browse files Browse the repository at this point in the history
  • Loading branch information
neolynx committed Jun 15, 2024
1 parent efc5573 commit 3e1485f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
14 changes: 11 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,17 @@ func maybeRunTaskInBackground(c *gin.Context, name string, resources []string, p
c.JSON(202, task)
} else {
log.Debug().Msg("Executing task synchronously")
out := context.Progress()
detail := task.Detail{}
retValue, err := proc(out, &detail)
task, conflictErr := runTaskInBackground(name, resources, proc)
if conflictErr != nil {
AbortWithJSONError(c, 409, conflictErr)
return
}

// wait for task to finish
context.TaskList().WaitForTaskByID(task.ID)

retValue, _ := context.TaskList().GetTaskReturnValueByID(task.ID)
err, _ := context.TaskList().GetTaskErrorByID(task.ID)
if err != nil {
AbortWithJSONError(c, retValue.Code, err)
return
Expand Down
12 changes: 12 additions & 0 deletions task/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (list *List) consumer() {
list.Lock()
{
task.processReturnValue = retValue
task.err = err
if err != nil {
task.output.Printf("Task failed with error: %v", err)
task.State = FAILED
Expand Down Expand Up @@ -241,3 +242,14 @@ func (list *List) WaitForTaskByID(ID int) (Task, error) {
wgTask.Wait()
return list.GetTaskByID(ID)
}

// GetTaskError returns the Task error for a given id
func (list *List) GetTaskErrorByID(ID int) (error, error) {
task, err := list.GetTaskByID(ID)

if err != nil {
return nil, err
}

return task.err, nil
}
1 change: 1 addition & 0 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Task struct {
detail *Detail
process Process
processReturnValue *ProcessReturnValue
err error
Name string
ID int
State State
Expand Down

0 comments on commit 3e1485f

Please sign in to comment.