|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | + |
| 9 | +import { formatDistanceToNow } from 'date-fns' |
| 10 | +import { type ReactNode } from 'react' |
| 11 | +import { useForm } from 'react-hook-form' |
| 12 | +import { match } from 'ts-pattern' |
| 13 | + |
| 14 | +import { |
| 15 | + apiQueryClient, |
| 16 | + instanceAutoRestartingSoon, |
| 17 | + useApiMutation, |
| 18 | + usePrefetchedApiQuery, |
| 19 | +} from '~/api' |
| 20 | +import { ListboxField } from '~/components/form/fields/ListboxField' |
| 21 | +import { useInstanceSelector } from '~/hooks/use-params' |
| 22 | +import { addToast } from '~/stores/toast' |
| 23 | +import { Button } from '~/ui/lib/Button' |
| 24 | +import { CardBlock, LearnMore } from '~/ui/lib/CardBlock' |
| 25 | +import { type ListboxItem } from '~/ui/lib/Listbox' |
| 26 | +import { TipIcon } from '~/ui/lib/TipIcon' |
| 27 | +import { toLocaleDateTimeString } from '~/util/date' |
| 28 | +import { links } from '~/util/links' |
| 29 | + |
| 30 | +type FormPolicy = 'default' | 'never' | 'best_effort' |
| 31 | + |
| 32 | +const restartPolicyItems: ListboxItem<FormPolicy>[] = [ |
| 33 | + { value: 'default', label: 'Default' }, |
| 34 | + { value: 'never', label: 'Never' }, |
| 35 | + { value: 'best_effort', label: 'Best effort' }, |
| 36 | +] |
| 37 | + |
| 38 | +type FormValues = { |
| 39 | + autoRestartPolicy: FormPolicy |
| 40 | +} |
| 41 | + |
| 42 | +export function AutoRestartCard() { |
| 43 | + const instanceSelector = useInstanceSelector() |
| 44 | + |
| 45 | + const { data: instance } = usePrefetchedApiQuery('instanceView', { |
| 46 | + path: { instance: instanceSelector.instance }, |
| 47 | + query: { project: instanceSelector.project }, |
| 48 | + }) |
| 49 | + |
| 50 | + const instanceUpdate = useApiMutation('instanceUpdate', { |
| 51 | + onSuccess() { |
| 52 | + apiQueryClient.invalidateQueries('instanceView') |
| 53 | + addToast({ content: 'Instance auto-restart policy updated' }) |
| 54 | + }, |
| 55 | + onError(err) { |
| 56 | + addToast({ |
| 57 | + title: 'Could not update auto-restart policy', |
| 58 | + content: err.message, |
| 59 | + variant: 'error', |
| 60 | + }) |
| 61 | + }, |
| 62 | + }) |
| 63 | + |
| 64 | + const autoRestartPolicy = instance.autoRestartPolicy || 'default' |
| 65 | + const defaultValues: FormValues = { autoRestartPolicy } |
| 66 | + |
| 67 | + const form = useForm({ defaultValues }) |
| 68 | + |
| 69 | + // note there are no instance state-based restrictions on updating auto |
| 70 | + // restart, so there is no instanceCan helper for it |
| 71 | + // https://github.com/oxidecomputer/omicron/blob/0c6ab099e/nexus/db-queries/src/db/datastore/instance.rs#L1050-L1058 |
| 72 | + const disableSubmit = form.watch('autoRestartPolicy') === autoRestartPolicy |
| 73 | + |
| 74 | + const onSubmit = form.handleSubmit((values) => { |
| 75 | + instanceUpdate.mutate({ |
| 76 | + path: { instance: instanceSelector.instance }, |
| 77 | + query: { project: instanceSelector.project }, |
| 78 | + body: { |
| 79 | + ncpus: instance.ncpus, |
| 80 | + memory: instance.memory, |
| 81 | + bootDisk: instance.bootDiskId, |
| 82 | + autoRestartPolicy: match(values.autoRestartPolicy) |
| 83 | + .with('default', () => undefined) |
| 84 | + .with('never', () => 'never' as const) |
| 85 | + .with('best_effort', () => 'best_effort' as const) |
| 86 | + .exhaustive(), |
| 87 | + }, |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + return ( |
| 92 | + <form onSubmit={onSubmit}> |
| 93 | + <CardBlock> |
| 94 | + <CardBlock.Header |
| 95 | + title="Auto-restart" |
| 96 | + description="The auto-restart policy for this instance" |
| 97 | + /> |
| 98 | + <CardBlock.Body> |
| 99 | + <ListboxField |
| 100 | + control={form.control} |
| 101 | + name="autoRestartPolicy" |
| 102 | + label="Policy" |
| 103 | + description="The global default is currently best effort, but this may change in the future." |
| 104 | + items={restartPolicyItems} |
| 105 | + required |
| 106 | + className="max-w-lg" |
| 107 | + /> |
| 108 | + <FormMeta |
| 109 | + label="Cooldown expiration" |
| 110 | + tip="When this instance will next restart (if in a failed state and the policy allows it). If N/A, then either the instance has never been automatically restarted, or the cooldown period has expired." |
| 111 | + > |
| 112 | + {instance.autoRestartCooldownExpiration ? ( |
| 113 | + <> |
| 114 | + {toLocaleDateTimeString(instance.autoRestartCooldownExpiration)}{' '} |
| 115 | + {instance.runState === 'failed' && instance.autoRestartEnabled && ( |
| 116 | + <span className="text-tertiary"> |
| 117 | + ( |
| 118 | + {instanceAutoRestartingSoon(instance) |
| 119 | + ? 'restarting soon' |
| 120 | + : formatDistanceToNow(instance.autoRestartCooldownExpiration)} |
| 121 | + ) |
| 122 | + </span> |
| 123 | + )} |
| 124 | + </> |
| 125 | + ) : ( |
| 126 | + <span className="text-tertiary">N/A</span> |
| 127 | + )} |
| 128 | + </FormMeta> |
| 129 | + <FormMeta |
| 130 | + label="Last auto-restarted" |
| 131 | + tip="When this instance was last automatically restarted. N/A if never auto-restarted." |
| 132 | + > |
| 133 | + {instance.timeLastAutoRestarted ? ( |
| 134 | + toLocaleDateTimeString(instance.timeLastAutoRestarted) |
| 135 | + ) : ( |
| 136 | + <span className="text-tertiary">N/A</span> |
| 137 | + )} |
| 138 | + </FormMeta> |
| 139 | + </CardBlock.Body> |
| 140 | + <CardBlock.Footer> |
| 141 | + <LearnMore href={links.instanceUpdateDocs} text="Auto-Restart" /> |
| 142 | + <Button size="sm" type="submit" disabled={disableSubmit}> |
| 143 | + Save |
| 144 | + </Button> |
| 145 | + </CardBlock.Footer> |
| 146 | + </CardBlock> |
| 147 | + </form> |
| 148 | + ) |
| 149 | +} |
| 150 | + |
| 151 | +type FormMetaProps = { |
| 152 | + label: string |
| 153 | + tip?: string |
| 154 | + children: ReactNode |
| 155 | +} |
| 156 | + |
| 157 | +const FormMeta = ({ label, tip, children }: FormMetaProps) => ( |
| 158 | + <div> |
| 159 | + <div className="mb-2 flex items-center gap-1 border-b pb-2 text-sans-md border-secondary"> |
| 160 | + <div>{label}</div> |
| 161 | + {tip && <TipIcon>{tip}</TipIcon>} |
| 162 | + </div> |
| 163 | + {children} |
| 164 | + </div> |
| 165 | +) |
0 commit comments