From e55f36a131f3c2f41e33b0b2cbe43963606fa4e0 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 14 Jun 2026 20:51:34 +0000 Subject: [PATCH] Improve queue position accuracy and wording in upload email --- misc/upload_notification_template.html | 2 +- scraibe/tasks.py | 39 ++++++++++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/misc/upload_notification_template.html b/misc/upload_notification_template.html index e4af4c1..3606f03 100644 --- a/misc/upload_notification_template.html +++ b/misc/upload_notification_template.html @@ -13,7 +13,7 @@

Upload Successful

Dear user,

Your file has been successfully uploaded and is now in our processing queue. This means that our system has received your file, and it is waiting to be processed. We will handle your file as soon as possible.

-

Your current position in the queue is: {queue_position}. This is the order in which your file will be processed. We appreciate your patience as we work through the queue.

+

Your current position in the queue is: {queue_position_text}. This is the order in which your file will be processed. We appreciate your patience as we work through the queue.

We will notify you once your file has been processed. If you have any urgent needs or further questions, feel free to reach out to our support team.

You can contact our support team at {contact_email}. Please note that our support team is here to help with any questions or issues you might have.

diff --git a/scraibe/tasks.py b/scraibe/tasks.py index 03ac706..fec0537 100644 --- a/scraibe/tasks.py +++ b/scraibe/tasks.py @@ -70,20 +70,37 @@ def _get_subject(env_var: str, default: str) -> str: def get_queue_position(task_id: str) -> int: """ Estimate the job's position in the queue. + Returns: + - A positive int if we can estimate (1 = first in line). + - 0 if we cannot reliably determine position. """ try: inspect = celery_app.control.inspect() - ready = inspect.active() or {} - reserved = inspect.reserved() or {} - count = 0 - for _, tasks in list(ready.values()) + list(reserved.values()): + reserved = inspect.reserved() or {} # queued but not yet running + active = inspect.active() or {} # currently running + + # Count tasks ahead of this one in the reserved (waiting) queue + ahead = 0 + found = False + for _, tasks in list(reserved.values()): for t in tasks: - if t.get("id") == task_id: + tid = t.get("id") + if tid == task_id: + found = True break - count += 1 - return max(count + 1, 1) + ahead += 1 + if found: + break + + # If not found in reserved, it may already be active or not yet visible. + # In that case, treat it as position 1. + if found: + return max(ahead + 1, 1) + else: + return 1 except Exception: - return -1 + # If inspection fails, don't guess; caller should use a safe message. + return 0 def send_initial_email(to: str, queue_pos: int): @@ -103,8 +120,12 @@ def send_initial_email(to: str, queue_pos: int): if queue_pos > 0: body += f"Your request is currently number {queue_pos} in the queue.\n" + queue_position_display = ( + f'{queue_pos}' + ) else: body += "Your request has been queued for processing.\n" + queue_position_display = "the queue" body += ( "\n" @@ -119,7 +140,7 @@ def send_initial_email(to: str, queue_pos: int): try: html = load_template( "upload_notification_template.html", - queue_position=str(max(queue_pos, 1)), + queue_position_text=queue_position_display, ) except EmailError as e: logger.warning("Failed to render upload notification template: %s", e)