| 1 | import { Injectable, Logger } from '@nestjs/common' |
| 2 | import { Cron, CronExpression } from '@nestjs/schedule' |
| 3 | import { WithLoggerContext } from '@yikart/common' |
| 4 | import { Redlock } from '@yikart/redlock' |
| 5 | import { RedlockKey } from '../../common/enums' |
| 6 | import { config } from '../../config' |
| 7 | import { AgentService } from './agent.service' |
| 8 | |
| 9 | @Injectable() |
| 10 | export class AgentTaskTimeoutScheduler { |
| 11 | private readonly logger = new Logger(AgentTaskTimeoutScheduler.name) |
| 12 | |
| 13 | constructor(private readonly agentService: AgentService) { } |
| 14 | |
| 15 | /** |
| 16 | * 每10分钟检查一次超时的 running 任务 |
| 17 | * 将超过配置的超时时间未更新的 running 任务更新为 error 状态 |
| 18 | */ |
| 19 | @Cron(CronExpression.EVERY_10_MINUTES) |
| 20 | @Redlock(RedlockKey.AgentTaskTimeout, 600, { throwOnFailure: false }) |
| 21 | @WithLoggerContext() |
| 22 | async recoverTimeoutRunningTasks() { |
| 23 | const timeoutMs = config.agent.taskTimeoutMs |
| 24 | this.logger.debug( |
| 25 | `开始检查超时的 running 任务(超时时间: ${timeoutMs}ms,约 ${Math.round(timeoutMs / 1000 / 60)} 分钟)`, |
| 26 | ) |
| 27 | |
| 28 | const result |
| 29 | = await this.agentService.recoverTimeoutRunningTasks(timeoutMs) |
| 30 | if (result.updatedCount > 0) { |
| 31 | this.logger.debug( |
| 32 | `成功将 ${result.updatedCount} 个超时任务更新为 error 状态`, |
| 33 | ) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 |