From 1987220f1e0f8a3dd9d606fd454132bce278970d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20N=2ERodriguez?= Date: Tue, 2 Apr 2024 00:51:31 +0200 Subject: [PATCH] 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// 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. --- task/list.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/task/list.go b/task/list.go index 489986c1..092ecb1e 100644 --- a/task/list.go +++ b/task/list.go @@ -117,19 +117,20 @@ func (list *List) GetTaskReturnValueByID(ID int) (*ProcessReturnValue, error) { 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. +// RunTaskInBackground creates task and runs it in background. This will block until the necessary resources +// become available. func (list *List) RunTaskInBackground(name string, resources []string, process Process) (Task, *ResourceConflictError) { list.Lock() defer list.Unlock() tasks := list.usedResources.UsedBy(resources) - if len(tasks) > 0 { - conflictError := &ResourceConflictError{ - Tasks: tasks, - Message: "Needed resources are used by other tasks.", + for len(tasks) > 0 { + for _, task := range(tasks) { + list.Unlock() + list.wgTasks[task.ID].Wait() + list.Lock() } - return Task{}, conflictError + tasks = list.usedResources.UsedBy(resources) } list.idCounter++