(input: DecideScheduleInput)
| 56 | * email when `adminEmail` is set and `email.graceStartTag !== latest.tag`. |
| 57 | */ |
| 58 | export const decideSchedule = (input: DecideScheduleInput): SchedulerDecision => { |
| 59 | const { |
| 60 | state, now, policy, latest, current, preApplyGraceMinutes, adminEmail, |
| 61 | maintenanceWindow, |
| 62 | } = input; |
| 63 | const status = state.execution.status; |
| 64 | |
| 65 | if (!latest) return {action: 'nothing'}; |
| 66 | |
| 67 | if (!policy.canAuto) { |
| 68 | if (status === 'scheduled') return {action: 'cancel-schedule', reason: 'policy-denied'}; |
| 69 | return {action: 'nothing'}; |
| 70 | } |
| 71 | |
| 72 | if (IN_FLIGHT.has(status) || TERMINAL.has(status)) return {action: 'nothing'}; |
| 73 | |
| 74 | if (status === 'scheduled' |
| 75 | && (state.execution as {targetTag: string}).targetTag === latest.tag) { |
| 76 | return {action: 'nothing'}; |
| 77 | } |
| 78 | |
| 79 | const graceMs = clampGrace(preApplyGraceMinutes) * 60 * 1000; |
| 80 | let scheduledForDate = new Date(now.getTime() + graceMs); |
| 81 | // Tier 4: snap forward to the next opening if grace lands outside the window. |
| 82 | if (policy.canAutonomous && maintenanceWindow) { |
| 83 | if (!inWindow(scheduledForDate, maintenanceWindow)) { |
| 84 | scheduledForDate = nextWindowStart(scheduledForDate, maintenanceWindow); |
| 85 | } |
| 86 | } |
| 87 | const scheduledFor = scheduledForDate.toISOString(); |
| 88 | const newExecution = { |
| 89 | status: 'scheduled' as const, |
| 90 | targetTag: latest.tag, |
| 91 | scheduledFor, |
| 92 | startedAt: now.toISOString(), |
| 93 | }; |
| 94 | |
| 95 | const emails: PlannedEmail[] = []; |
| 96 | const newEmailState: EmailSendLog = {...state.email}; |
| 97 | if (adminEmail && state.email.graceStartTag !== latest.tag) { |
| 98 | emails.push({ |
| 99 | kind: 'grace-start', |
| 100 | subject: `[Etherpad] Auto-update scheduled for ${latest.version}`, |
| 101 | body: `Etherpad will auto-update to ${latest.tag} at ${scheduledFor}. ` + |
| 102 | `Your version is ${current}. To cancel, visit /admin/update.`, |
| 103 | }); |
| 104 | newEmailState.graceStartTag = latest.tag; |
| 105 | } |
| 106 | |
| 107 | return {action: 'schedule', newExecution, emails, newEmailState}; |
| 108 | }; |
| 109 | |
| 110 | export type TriggerApplyDecision = |
| 111 | | {action: 'fire'} |
no test coverage detected