Heuristic:A616567126 GPT WEB JAVA MJ Task Timeout Refund
| Knowledge Sources | |
|---|---|
| Domains | Debugging, Task_Management |
| Last Updated | 2026-10-13 18:00 GMT |
Overview
Apply per-account configurable timeout for Midjourney tasks with automatic credit refund based on task action type to prevent revenue loss from failed tasks.
Description
The `MyTask.checkTasks()` scheduled job (runs every 30 seconds) scans all alive Discord instances for timed-out tasks. Timeout is configured per Discord account via `timeoutMinutes` in `discord_account_config`. For each task whose elapsed time exceeds the timeout, the task is marked as failed with the message "任务超时" (task timeout), the appropriate credit cost is refunded to the user based on the task action type, and the task is removed from the running queue. The credit cost varies by action: imagine = 5 credits (`MJ_NUMBER`), upscale = 2 credits (`MJ_U_NUMBER`), variation = 5 credits (`MJ_V_NUMBER`), describe = 3 credits (`MJ_DESCRIBE_NUMBER`), blend = 5 credits (`MJ_BLEND_NUMBER`). If a timed-out task is already in FAILURE or SUCCESS state, it is simply removed from the queue without refund.
Usage
Use this heuristic when implementing timeout handling for long-running tasks with credit-based access control. Always refund credits for timeout-induced failures, using the correct cost for each action type. Use a scheduled job (not per-task timers) to batch-check timeouts across all instances.
The Insight (Rule of Thumb)
- Action: Run `checkTasks()` every 30 seconds; for each task where `System.currentTimeMillis() - startTime > timeoutMinutes * 60_000`, mark as failed and refund credits.
- Value: Poll interval = 30 seconds; timeout = per-account `timeoutMinutes` (configurable in DB).
- Trade-off: 30-second polling has a max 30-second delay in detecting timeout; shorter intervals increase CPU but improve responsiveness.
- Compatibility: Credit amounts differ per action type; must match the `CommonConst` constants used at task submission time.
Reasoning
Midjourney tasks involve Discord API interactions that can hang indefinitely (network issues, bot rate limits, account bans). Without timeout enforcement, tasks would remain in the running queue forever, consuming thread pool capacity and leaving users without feedback or credit refund. The per-account timeout allows different Discord accounts to have different timeout thresholds based on their reliability. The credit refund on timeout is critical for user trust — users should not lose paid credits for tasks that the system failed to complete. The action-type-based refund ensures the correct amount is returned, since different MJ actions consume different credit amounts.