mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-08 05:50:47 +00:00
Add task api and resource locking ability
This commit is contained in:
committed by
Lorenzo Bolla
parent
e63d74dff2
commit
6ab5e60833
@@ -0,0 +1,93 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AllLocalReposResourcesKey to be used as resource key when all local repos are needed
|
||||
const AllLocalReposResourcesKey = "__alllocalrepos__"
|
||||
|
||||
// ResourceConflictError represents a list tasks
|
||||
// using conflicitng resources
|
||||
type ResourceConflictError struct {
|
||||
Tasks []Task
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *ResourceConflictError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// ResourcesSet represents a set of task resources.
|
||||
// A resource is represented by its unique key
|
||||
type ResourcesSet struct {
|
||||
set map[string]*Task
|
||||
}
|
||||
|
||||
// NewResourcesSet creates new instance of resources set
|
||||
func NewResourcesSet() *ResourcesSet {
|
||||
return &ResourcesSet{make(map[string]*Task)}
|
||||
}
|
||||
|
||||
// MarkInUse given resources as used by given task
|
||||
func (r *ResourcesSet) MarkInUse(resources []string, task *Task) {
|
||||
for _, resource := range resources {
|
||||
r.set[resource] = task
|
||||
}
|
||||
}
|
||||
|
||||
// UsedBy checks whether one of given resources
|
||||
// is used by a task and if yes returns slice of such task
|
||||
func (r *ResourcesSet) UsedBy(resources []string) []Task {
|
||||
var tasks []Task
|
||||
var task *Task
|
||||
var found bool
|
||||
|
||||
for _, resource := range resources {
|
||||
|
||||
if resource == AllLocalReposResourcesKey {
|
||||
for taskResource, task := range r.set {
|
||||
if strings.HasPrefix(taskResource, "L") {
|
||||
tasks = appendTask(tasks, task)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task, found = r.set[resource]
|
||||
if found {
|
||||
tasks = appendTask(tasks, task)
|
||||
}
|
||||
}
|
||||
|
||||
task, found = r.set[AllLocalReposResourcesKey]
|
||||
if found {
|
||||
tasks = appendTask(tasks, task)
|
||||
}
|
||||
|
||||
return tasks
|
||||
}
|
||||
|
||||
// appendTask only appends task to tasks slice if not already
|
||||
// on slice
|
||||
func appendTask(tasks []Task, task *Task) []Task {
|
||||
needsAppending := true
|
||||
for _, givenTask := range tasks {
|
||||
if givenTask.ID == task.ID {
|
||||
needsAppending = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if needsAppending {
|
||||
return append(tasks, *task)
|
||||
}
|
||||
|
||||
return tasks
|
||||
}
|
||||
|
||||
// Free removes given resources from dependency set
|
||||
func (r *ResourcesSet) Free(resources []string) {
|
||||
for _, resource := range resources {
|
||||
delete(r.set, resource)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user