Principle:A616567126 GPT WEB JAVA Task Timeout Management
| Knowledge Sources | |
|---|---|
| Domains | Task_Management, Concurrency |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Pattern that monitors running tasks for timeout conditions, fails overdue tasks, refunds credits, and removes them from the execution queue to free resources.
Description
In distributed task processing, tasks may hang indefinitely due to network failures, external service outages, or unresponsive APIs. A timeout management system periodically scans running tasks, compares elapsed time against a per-account timeout threshold, and takes corrective action for overdue tasks: marking them as failed, refunding the user's credits, and removing them from the running queue.
This repository implements timeout management for Midjourney tasks. A scheduled task (MyTask.checkTasks()) runs every 30 seconds, iterates alive Discord instances, and checks each running task against the instance's timeoutMinutes setting. Overdue tasks are failed, credits refunded, and removed from the instance.
Usage
Use this principle when:
- Managing long-running tasks against external services (Discord, Midjourney)
- Needing automatic credit refunds for failed/timeout tasks
- Preventing resource leaks from stuck tasks in thread pools
Theoretical Basis
Timeout Check Algorithm:
# Abstract algorithm (NOT real implementation)
def check_task_timeouts():
for instance in load_balancer.alive_instances:
timeout_ms = instance.timeout_minutes * 60 * 1000
for task in instance.running_tasks:
if current_time - task.start_time > timeout_ms:
if task.status in (FAILURE, SUCCESS):
log.warn("Task already done but still in queue")
else:
task.fail("任务超时") # "Task timeout"
refund_credits(task.user, cost_for(task.action))
instance.exit_task(task) # Cancel future, remove from queue
Key Properties:
- Per-account timeout: Each Discord account has its own
timeoutMinutessetting - Credit refund: Credits refunded based on the task action type (imagine, upscale, variation, describe, blend)
- Status-aware: Tasks already in FAILURE/SUCCESS state are just cleaned up, not re-failed
- Resource cleanup:
exitTask()cancels the Future and removes the task from runningTasks and taskFutureMap
Practical Guide
The repository implements this in MyTask.checkTasks():
- Runs every 30 seconds via
@Scheduled(fixedRate = 30000) - Iterates
discordLoadBalancer.getAliveInstances() - For each instance, filters running tasks where
currentTime - startTime > timeoutMinutes - Calls
task.fail("任务超时")for overdue non-terminal tasks - Calls
asyncService.updateRemainingTimes()to refund credits based on action type - Calls
instance.exitTask(task)to cancel and remove
Related Pages
Implemented By
- Implementation:A616567126_GPT_WEB_JAVA_MyTask
- Implementation:A616567126_GPT_WEB_JAVA_DiscordInstanceImpl