Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:A616567126 GPT WEB JAVA MJ Task Timeout Refund

From Leeroopedia
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.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment