api: publish: block on concurrent calls

This commit blocks concurrent calls to RunTaskInBackground which is
intended to fix the quirky behaviour where concurrent PUT calls to
api/publish/<prefix>/<distribution> would immedietly reuturn an error.

The solution proposed in this commit is not elegant and probaly has
unintended side-effects. The intention of this commit is to highlight
the area that actually needs to be addressed.
Ideally this patch is amended or dropped entierly in favor of a better
fixup.
This commit is contained in:
Ramón N.Rodriguez
2024-04-02 00:51:31 +02:00
committed by André Roth
parent 47696a3303
commit 1987220f1e

View File

@@ -117,19 +117,20 @@ func (list *List) GetTaskReturnValueByID(ID int) (*ProcessReturnValue, error) {
return task.processReturnValue, nil return task.processReturnValue, nil
} }
// RunTaskInBackground creates task and runs it in background. It won't be run and an error // RunTaskInBackground creates task and runs it in background. This will block until the necessary resources
// returned if there are running tasks which are using needed resources already. // become available.
func (list *List) RunTaskInBackground(name string, resources []string, process Process) (Task, *ResourceConflictError) { func (list *List) RunTaskInBackground(name string, resources []string, process Process) (Task, *ResourceConflictError) {
list.Lock() list.Lock()
defer list.Unlock() defer list.Unlock()
tasks := list.usedResources.UsedBy(resources) tasks := list.usedResources.UsedBy(resources)
if len(tasks) > 0 { for len(tasks) > 0 {
conflictError := &ResourceConflictError{ for _, task := range(tasks) {
Tasks: tasks, list.Unlock()
Message: "Needed resources are used by other tasks.", list.wgTasks[task.ID].Wait()
list.Lock()
} }
return Task{}, conflictError tasks = list.usedResources.UsedBy(resources)
} }
list.idCounter++ list.idCounter++