More informative return value for task.Process

This commit is contained in:
Lorenzo Bolla
2021-09-20 09:43:44 +02:00
parent 0914cd16af
commit ff51c46915
11 changed files with 156 additions and 127 deletions

View File

@@ -105,6 +105,17 @@ func (list *List) GetTaskDetailByID(ID int) (interface{}, error) {
return detail, nil
}
// GetTaskReturnValueByID returns process return value of task with given id
func (list *List) GetTaskReturnValueByID(ID int) (*ProcessReturnValue, error) {
task, err := list.GetTaskByID(ID)
if err != nil {
return nil, err
}
return task.processReturnValue, nil
}
// RunTaskInBackground creates task and runs it in background. It won't be run and an error
// returned if there are running tasks which are using needed resources already.
func (list *List) RunTaskInBackground(name string, resources []string, process Process) (Task, *ResourceConflictError) {
@@ -139,11 +150,11 @@ func (list *List) RunTaskInBackground(name string, resources []string, process P
}
list.Unlock()
retCode, err := process(aptly.Progress(task.output), task.detail)
retValue, err := process(aptly.Progress(task.output), task.detail)
list.Lock()
{
task.processReturnCode = retCode
task.processReturnValue = retValue
if err != nil {
task.output.Printf("Task failed with error: %v", err)
task.State = FAILED

View File

@@ -2,6 +2,7 @@ package task
import (
"errors"
"github.com/aptly-dev/aptly/aptly"
// need to import as check as otherwise List is redeclared
@@ -16,8 +17,8 @@ func (s *ListSuite) TestList(c *check.C) {
list := NewList()
c.Assert(len(list.GetTasks()), check.Equals, 0)
task, err := list.RunTaskInBackground("Successful task", nil, func(out aptly.Progress, detail *Detail) (int, error) {
return -1, nil
task, err := list.RunTaskInBackground("Successful task", nil, func(out aptly.Progress, detail *Detail) (*ProcessReturnValue, error) {
return nil, nil
})
c.Assert(err, check.IsNil)
list.WaitForTaskByID(task.ID)
@@ -31,10 +32,10 @@ func (s *ListSuite) TestList(c *check.C) {
detail, _ := list.GetTaskDetailByID(task.ID)
c.Check(detail, check.Equals, struct{}{})
task, err = list.RunTaskInBackground("Faulty task", nil, func(out aptly.Progress, detail *Detail) (int, error) {
task, err = list.RunTaskInBackground("Faulty task", nil, func(out aptly.Progress, detail *Detail) (*ProcessReturnValue, error) {
detail.Store("Details")
out.Printf("Test Progress\n")
return -1, errors.New("Task failed")
return nil, errors.New("Task failed")
})
c.Assert(err, check.IsNil)
list.WaitForTaskByID(task.ID)

View File

@@ -20,8 +20,13 @@ type PublishDetail struct {
RemainingNumberOfPackages int64
}
type ProcessReturnValue struct {
Code int
Value interface{}
}
// Process is a function implementing the actual task logic
type Process func(out aptly.Progress, detail *Detail) (int, error)
type Process func(out aptly.Progress, detail *Detail) (*ProcessReturnValue, error)
const (
// IDLE when task is waiting
@@ -36,13 +41,13 @@ const (
// Task represents as task in a queue encapsulates process code
type Task struct {
output *Output
detail *Detail
process Process
processReturnCode int
Name string
ID int
State State
output *Output
detail *Detail
process Process
processReturnValue *ProcessReturnValue
Name string
ID int
State State
}
// NewTask creates new task