Improve queue position accuracy and wording in upload email
This commit is contained in:
+30
-9
@@ -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'<span style="color:{_accent_color()}; font-weight:bold;">{queue_pos}</span>'
|
||||
)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user