Skip to content

feat: improve gifts indexing queue priority, task timeouts, and refresh UX feedback - #259

Merged
danoctua merged 3 commits into
mainfrom
feat/gifts-sync-improvements
Sep 26, 2026
Merged

danoctua merged 3 commits into
mainfrom
feat/gifts-sync-improvements

Conversation

@danoctua

Copy link
Copy Markdown
Collaborator

Description

This PR addresses queue priority ordering for Telegram gift indexing and improves the user experience during manual gift refreshes:

  1. Celery Priority Queue Order: Previously, user-initiated high-priority gift indexing tasks could be delayed behind recurring background batches due to Kombu's priority_cycle
    iterating over a Python set with randomized hash seeds in Redis BRPOP. We resolved this by prefixing the queue names alphabetically and using Celery's "queue_order_strategy": "sorted", combined with task_acks_late: True and worker_prefetch_multiplier: 1 for deterministic priority consumption.
  2. Backend Task Status Timeout: The /system/async-tasks/{task_id} polling endpoint previously waited indefinitely in a while not task_result.ready() loop. It now enforces a
    default timeout (DEFAULT_ASYNC_TASK_WAIT_TIMEOUT_SECONDS = 9.5) and returns HTTP 408 Request Timeout ("Task took longer than expected") if the task is still executing.
  3. Frontend Timeout & UX Feedback: Added targeted per-request timeout support to ApiService (ASYNC_TASK_TIMEOUT_MS = 10000) for task status checks. With Ky's built-in 1-retry
    on status 408, the client waits ~19–20 seconds total before giving up. Added visual feedback toasts in ChatConditions:
    • Success toast on completion ("Gifts refreshed successfully").
    • Warning toast on timeout ("Your request to refresh gifts was submitted, but took longer than expected. Please, wait for 5 minutes or try again"), while refreshing chat state.
    • Error toast on failures or rate limits (e.g. 5-minute cooldown).

Fixes: # (if applicable)

Checklist

Before submitting your pull request, please ensure the following:

  • I have read the contributing guidelines.
  • I ran all tests and they passed successfully.
  • I added/updated documentation (if applicable).
  • I reviewed my own code and followed the project's code style.
  • I included relevant details in the PR description or commit messages.

Changes

  • Queue Prioritization (core/constants.py, indexer_gifts/celery_app.py, docker-compose.yml):
    • Prefixed gift queues with 0-gift-user-priority, 1-gift-collections-sync, and 2-gift-user-batch.
    • Configured "queue_order_strategy": "sorted", "task_acks_late": True, and "worker_prefetch_multiplier": 1.
  • Backend Task Polling Timeout (core/constants.py, core/utils/task.py, api/routes/system.py):
    • Added DEFAULT_ASYNC_TASK_WAIT_TIMEOUT_SECONDS = 9.5.
    • Added timeout parameter to wait_for_task raising TimeoutError when exceeded.
    • Renamed route handler to get_task_status, catching TimeoutError and returning HTTP 408 Request Timeout.
  • Frontend Targeted Timeout (common/utils/constants.ts, services/ApiService.ts, store/user/api.ts):
    • Defined ASYNC_TASK_TIMEOUT_MS = 10000.
    • Added optional timeout override in ApiOptions and mapped Ky TimeoutError to HTTP 408.
    • Passed timeout override specifically in completeChatTasksAPI.
  • Store & UI Feedback (store/user/user.ts, ChatConditions.tsx):
    • Exported TaskTimeoutError for HTTP 408 responses.
    • Added success, warning (timeout), and error feedback toasts in handleRefreshGifts.

How Has This Been Tested?

  • Backend Test Suite: Ran make test inside the Docker test environment (all 111 tests passed in 9.97s).
  • Backend Linting: Ran pre-commit (ruff, ruff-format) on all modified backend files with zero violations.
  • Frontend Build: Verified TypeScript compilation and bundling via npm run build (built cleanly with 0 errors).

Screenshots (if applicable)

N/A

Additional Notes

  • The 9.5s backend wait paired with Ky's default 1-retry policy on status 408 provides an optimal 2-attempt window (~19–20s total) allowing tasks that take just slightly longer to
    succeed without blocking the UI indefinitely.

…n Celery

- Prefixes gift queues with 0-, 1-, and 2- for deterministic alphabetical sorting
- Updates Celery queue_order_strategy to 'sorted' to avoid Kombu priority_cycle set-ordering bug
- Enables task_acks_late to ensure workers fetch tasks just-in-time
@danoctua danoctua self-assigned this Sep 26, 2026
@danoctua danoctua added enhancement New feature or request gifts labels Sep 26, 2026
@danoctua
danoctua merged commit 0bc9985 into main Sep 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request gifts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant