flipped-aurora/gin-vue-admin · warning

任务执行超时

Error message

任务执行超时

What it means

errTaskTimeout is the sentinel error returned by RunTask/runMethod/runHTTP when a timed task exceeds its execution deadline. The runner recognizes it via errors.Is and records the task run's status as "timeout" rather than "fail", enabling different alerting/metrics.

Source

Thrown at server/service/system/sys_timed_task_runner.go:37

	"github.com/flipped-aurora/gin-vue-admin/server/utils/logger"
	"github.com/flipped-aurora/gin-vue-admin/server/utils/sse"
)

// 超时用 var 而非 const: 单测需收窄
var (
	defaultMethodTimeout = 5 * time.Minute
	defaultHTTPTimeout   = 30 * time.Second
)

const (
	maxHTTPRespBytes = 1 << 20 // HTTP 响应体读取上限 1MB
	maxLogTextLen    = 4000    // error/output 落库截断长度
	alertAuthorityID = 888     // 失败告警接收角色
	alertEventName   = "timedTask:alert"
)

// errTaskTimeout 超时哨兵: Runner 据此把状态记为 timeout 而非 fail
var errTaskTimeout = errors.New("任务执行超时")

func truncateText(s string, n int) string {
	if len(s) <= n {
		return s
	}
	return s[:n] + "...(截断)"
}

// RunTask 统一执行入口(自动调度与手动触发共用):
// panic 兜底、起止/耗时/状态/错误落 sys_timed_task_logs、失败经 SSE 告警。
// 阻塞执行; 调度器回调与手动触发均应在独立 goroutine 中调用。
func (s *TimedTaskService) RunTask(t system.SysTimedTask, trigger string) {
	started := time.Now()
	var output string
	var runErr error
	switch t.ExecutorType {
	case system.TimedTaskExecutorMethod:
		output, runErr = s.runMethod(t)

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Increase the task's timeout configuration if the workload legitimately needs longer.
  2. Add/raise timeouts inside the task's own HTTP client or DB queries so they fail fast and meaningfully.
  3. Optimize the slow operation (indexes, pagination, async processing).
  4. Check task run logs for partial output to identify where the task hangs.

Example fix

// before
client := &http.Client{} // no timeout inside task; runner kills at deadline

// after
client := &http.Client{Timeout: 20 * time.Second} // well under task timeout
Defensive patterns

Strategy: try-catch

Validate before calling

if task.TimeoutSeconds > 0 && estimatedDuration > time.Duration(task.TimeoutSeconds)*time.Second {
    log.Warnf("task %s may exceed its %ds timeout", task.Name, task.TimeoutSeconds)
}

Try / catch

if err := runner.RunTask(ctx, task); err != nil {
    if errors.Is(err, errTaskTimeout) {
        log.Warnf("task %s timed out; consider raising its timeout or optimizing the job", task.Name)
        return
    }
    log.Errorf("task %s failed: %v", task.Name, err)
}

Prevention

When it happens

Trigger: A method-executor task whose registered function blocks past the configured timeout (deadlocks, slow queries); an HTTP-executor task calling a slow or hanging endpoint that doesn't respond within the deadline.

Common situations: HTTP targets stuck waiting on downstream services without their own timeout; DB queries that scan large tables; method tasks doing synchronous network I/O; timeout configured too low for legitimately slow jobs.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/d174ba08525570e3. Report an issue: GitHub.