You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tooling.apache.org by sb...@apache.org on 2025/03/21 17:40:17 UTC

(tooling-trusted-release) branch main updated: Fix a task claiming bug

This is an automated email from the ASF dual-hosted git repository.

sbp pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-release.git


The following commit(s) were added to refs/heads/main by this push:
     new 2f7cd47  Fix a task claiming bug
2f7cd47 is described below

commit 2f7cd47ad41d40771f5afc69e9bf313ce761104a
Author: Sean B. Palmer <se...@miscoranda.com>
AuthorDate: Fri Mar 21 19:40:02 2025 +0200

    Fix a task claiming bug
---
 atr/worker.py | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/atr/worker.py b/atr/worker.py
index 7ba98ff..968051e 100644
--- a/atr/worker.py
+++ b/atr/worker.py
@@ -125,25 +125,35 @@ async def _task_next_claim() -> tuple[int, str, list[str] | dict[str, Any]] | No
     """
     async with db.session() as data:
         async with data.begin():
-            # Get all queued tasks and order by added date
-            query = (
-                sqlmodel.select(models.Task)
+            # Get the ID of the oldest queued task
+            oldest_queued_task = (
+                sqlmodel.select(models.Task.id)
                 .where(models.Task.status == task.QUEUED)
                 .order_by(db.validate_instrumented_attribute(models.Task.added).asc())
                 .limit(1)
             )
 
-            result = await data.execute(query)
-            task_obj = result.scalar_one_or_none()
+            # Use an UPDATE with a WHERE clause to atomically claim the task
+            # This ensures that only one worker can claim a specific task
+            now = datetime.datetime.now(datetime.UTC)
+            update_stmt = (
+                sqlmodel.update(models.Task)
+                .where(sqlmodel.and_(models.Task.id == oldest_queued_task, models.Task.status == task.QUEUED))
+                .values(status=task.ACTIVE, started=now, pid=os.getpid())
+                .returning(
+                    db.validate_instrumented_attribute(models.Task.id),
+                    db.validate_instrumented_attribute(models.Task.task_type),
+                    db.validate_instrumented_attribute(models.Task.task_args),
+                )
+            )
 
-            if task_obj:
-                # Update the task to mark it as active
-                task_obj.status = task.ACTIVE
-                task_obj.started = datetime.datetime.now(datetime.UTC)
-                task_obj.pid = os.getpid()
+            result = await data.execute(update_stmt)
+            claimed_task = result.first()
 
-                _LOGGER.info(f"Claimed task {task_obj.id} ({task_obj.task_type}) with args {task_obj.task_args}")
-                return task_obj.id, task_obj.task_type, task_obj.task_args
+            if claimed_task:
+                task_id, task_type, task_args = claimed_task
+                _LOGGER.info(f"Claimed task {task_id} ({task_type}) with args {task_args}")
+                return task_id, task_type, task_args
 
             return None
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org