diff --git a/apiserver/bll/task/task_bll.py b/apiserver/bll/task/task_bll.py index a244397..3883511 100644 --- a/apiserver/bll/task/task_bll.py +++ b/apiserver/bll/task/task_bll.py @@ -439,8 +439,11 @@ class TaskBLL: return ret @staticmethod - def remove_task_from_all_queues(company_id: str, task_id: str) -> int: - return Queue.objects(company=company_id, entries__task=task_id).update( + def remove_task_from_all_queues(company_id: str, task_id: str, exclude: str = None) -> int: + more = {} + if exclude: + more["id__ne"] = exclude + return Queue.objects(company=company_id, entries__task=task_id, **more).update( pull__entries__task=task_id, last_update=datetime.utcnow() ) diff --git a/apiserver/bll/task/task_operations.py b/apiserver/bll/task/task_operations.py index 0378f77..a80922b 100644 --- a/apiserver/bll/task/task_operations.py +++ b/apiserver/bll/task/task_operations.py @@ -231,15 +231,17 @@ def enqueue_task( if validate: TaskBLL.validate(task) + before_enqueue_status = task.status + if task.status == TaskStatus.queued and task.enqueue_status: + before_enqueue_status = task.enqueue_status res = ChangeStatusRequest( task=task, new_status=TaskStatus.queued, status_reason=status_reason, status_message=status_message, - allow_same_state_transition=False, force=force, user_id=user_id, - ).execute(enqueue_status=task.status) + ).execute(enqueue_status=before_enqueue_status) try: queue_bll.add_task(company_id=company_id, queue_id=queue_id, task_id=task.id) @@ -260,7 +262,8 @@ def enqueue_task( Task.objects(id=task_id).update(execution__queue=queue_id, multi=False) else: Task.objects(id=task_id).update(execution=Execution(queue=queue_id), multi=False) - + # make sure that the task is not queued in any other queue + TaskBLL.remove_task_from_all_queues(company_id=company_id, task_id=task_id, exclude=queue_id) nested_set(res, ("fields", "execution.queue"), queue_id) return 1, res