Configurable background task execution

This commit is contained in:
Lorenzo Bolla
2021-09-08 15:50:54 +02:00
parent bd4c3a246d
commit 9b28d8984f
18 changed files with 427 additions and 214 deletions

View File

@@ -2,6 +2,7 @@ package task
import (
"fmt"
"github.com/aptly-dev/aptly/aptly"
"sync"
)
@@ -138,10 +139,11 @@ func (list *List) RunTaskInBackground(name string, resources []string, process P
}
list.Unlock()
err := process(task.output, task.detail)
retCode, err := process(aptly.Progress(task.output), task.detail)
list.Lock()
{
task.processReturnCode = retCode
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
check "gopkg.in/check.v1"
@@ -15,8 +16,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 *Output, detail *Detail) error {
return nil
task, err := list.RunTaskInBackground("Successful task", nil, func(out aptly.Progress, detail *Detail) (int, error) {
return -1, nil
})
c.Assert(err, check.IsNil)
list.WaitForTaskByID(task.ID)
@@ -30,10 +31,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 *Output, detail *Detail) error {
task, err = list.RunTaskInBackground("Faulty task", nil, func(out aptly.Progress, detail *Detail) (int, error) {
detail.Store("Details")
out.WriteString("Test Progress\n")
return errors.New("Task failed")
out.Printf("Test Progress\n")
return -1, errors.New("Task failed")
})
c.Assert(err, check.IsNil)
list.WaitForTaskByID(task.ID)

View File

@@ -17,7 +17,7 @@ type Output struct {
// PublishOutput specific output for publishing api
type PublishOutput struct {
*Output
aptly.Progress
PublishDetail
barType *aptly.BarType
}

View File

@@ -1,6 +1,7 @@
package task
import (
"github.com/aptly-dev/aptly/aptly"
"sync/atomic"
)
@@ -20,7 +21,7 @@ type PublishDetail struct {
}
// Process is a function implementing the actual task logic
type Process func(out *Output, detail *Detail) error
type Process func(out aptly.Progress, detail *Detail) (int, error)
const (
// IDLE when task is waiting
@@ -35,12 +36,13 @@ const (
// Task represents as task in a queue encapsulates process code
type Task struct {
output *Output
detail *Detail
process Process
Name string
ID int
State State
output *Output
detail *Detail
process Process
processReturnCode int
Name string
ID int
State State
}
// NewTask creates new task