(totalRuns, options = {})
| 4028 | } |
| 4029 | |
| 4030 | async function scheduleAutoRun(totalRuns, options = {}) { |
| 4031 | const state = await getState(); |
| 4032 | if (isAutoRunLockedState(state) || isAutoRunPausedState(state) || autoRunActive) { |
| 4033 | throw new Error('自动运行已在进行中,请先停止后再重新计划。'); |
| 4034 | } |
| 4035 | if (getPendingAutoRunTimerPlan(state)) { |
| 4036 | throw new Error('已有自动运行倒计时计划,请先取消或立即开始。'); |
| 4037 | } |
| 4038 | |
| 4039 | const delayMinutes = normalizeAutoRunDelayMinutes(options.delayMinutes); |
| 4040 | const timerPlan = normalizeAutoRunTimerPlan({ |
| 4041 | kind: AUTO_RUN_TIMER_KIND_SCHEDULED_START, |
| 4042 | fireAt: Date.now() + delayMinutes * 60 * 1000, |
| 4043 | totalRuns, |
| 4044 | autoRunSkipFailures: options.autoRunSkipFailures, |
| 4045 | mode: options.mode, |
| 4046 | }); |
| 4047 | |
| 4048 | autoRunCurrentRun = 0; |
| 4049 | autoRunTotalRuns = timerPlan.totalRuns; |
| 4050 | autoRunAttemptRun = 0; |
| 4051 | |
| 4052 | await persistAutoRunTimerPlan(timerPlan, { |
| 4053 | autoRunSkipFailures: timerPlan.autoRunSkipFailures, |
| 4054 | autoRunRoundSummaries: serializeAutoRunRoundSummaries(timerPlan.totalRuns, []), |
| 4055 | }); |
| 4056 | await addLog( |
| 4057 | `自动运行已计划:${delayMinutes} 分钟后启动(${formatAutoRunScheduleTime(timerPlan.fireAt)}),目标 ${timerPlan.totalRuns} 轮。`, |
| 4058 | 'info' |
| 4059 | ); |
| 4060 | return { ok: true, scheduledAt: timerPlan.fireAt }; |
| 4061 | } |
| 4062 | |
| 4063 | async function cancelScheduledAutoRun(options = {}) { |
| 4064 | const state = await getState(); |
no test coverage detected