|
| 1 | +import { ProgressBar } from '@/components/ProgressBar'; |
| 2 | +import { getInstanceClient } from '@/config/getInstanceClient'; |
| 3 | +import { useInstanceClientIdParams } from '@/config/useInstanceClient'; |
| 4 | +import { getClusterInfo } from '@/features/cluster/queries/getClusterInfoQuery'; |
| 5 | +import { restartInstance } from '@/features/instance/operations/mutations/restartInstance'; |
| 6 | +import { setConfiguration } from '@/features/instance/operations/mutations/setConfiguration'; |
| 7 | +import { getInstanceUserInfo } from '@/features/instance/operations/queries/getInstanceUserInfo'; |
| 8 | +import { Instance } from '@/lib/api.patch'; |
| 9 | +import { excludeFalsy } from '@/lib/arrays/excludeFalsy'; |
| 10 | +import { pluralize } from '@/lib/pluralize'; |
| 11 | +import { sleep } from '@/lib/sleep'; |
| 12 | +import { getOperationsUrlForInstance } from '@/lib/urls/getOperationsUrlForInstance'; |
| 13 | +import { useQueryClient } from '@tanstack/react-query'; |
| 14 | +import { useCallback, useState } from 'react'; |
| 15 | +import { toast } from 'sonner'; |
| 16 | + |
| 17 | +interface RollingConfigUpdateParams { |
| 18 | + onRestartedSuccessfully?: () => void; |
| 19 | +} |
| 20 | + |
| 21 | +interface RollingConfigUpdateResponse { |
| 22 | + onConfigUpdate: (params: Record<string, unknown>) => void; |
| 23 | + isPending: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +export function useRollingConfigUpdate({ onRestartedSuccessfully }: RollingConfigUpdateParams = {}): RollingConfigUpdateResponse { |
| 27 | + const operationsParams = useInstanceClientIdParams(); |
| 28 | + const queryClient = useQueryClient(); |
| 29 | + const [isPending, setIsPending] = useState(false); |
| 30 | + const onConfigUpdate = useCallback(async (params: Record<string, unknown>) => { |
| 31 | + setIsPending(true); |
| 32 | + |
| 33 | + let canceled = false; |
| 34 | + const toastConfig = { |
| 35 | + duration: 60_000, |
| 36 | + action: { |
| 37 | + label: 'Cancel', |
| 38 | + onClick: () => { |
| 39 | + canceled = true; |
| 40 | + }, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + const toastId = toast.loading('Restarting', { |
| 45 | + ...toastConfig, |
| 46 | + description: <ProgressBar |
| 47 | + animated={true} |
| 48 | + width="0%" |
| 49 | + />, |
| 50 | + }); |
| 51 | + |
| 52 | + const cluster = operationsParams.entityType === 'cluster' |
| 53 | + ? await getClusterInfo(operationsParams.entityId) |
| 54 | + : null; |
| 55 | + const allInstances = operationsParams.entityType === 'cluster' |
| 56 | + ? cluster?.instances || [] |
| 57 | + : [{ id: operationsParams.entityId, status: 'RUNNING' } as Instance]; |
| 58 | + const instanceClients = operationsParams.entityType === 'cluster' |
| 59 | + ? allInstances |
| 60 | + .filter(instance => instance.status === 'RUNNING') |
| 61 | + .map(instance => getInstanceClient({ |
| 62 | + id: instance.id, |
| 63 | + operationsUrl: getOperationsUrlForInstance(instance), |
| 64 | + })) |
| 65 | + .reverse() |
| 66 | + : [operationsParams.instanceClient]; |
| 67 | + let instancesRestarted = 0; |
| 68 | + |
| 69 | + if (instanceClients.length) { |
| 70 | + for (let i = 0; i < instanceClients.length; i++) { |
| 71 | + const instanceClient = instanceClients[i]; |
| 72 | + if (!canceled) { |
| 73 | + toast.loading(`Updating Instance ${i + 1} of ${instanceClients.length}`, { |
| 74 | + ...toastConfig, |
| 75 | + id: toastId, |
| 76 | + description: <ProgressBar |
| 77 | + animated={true} |
| 78 | + width={(i === 0 ? 0 : (i / instanceClients.length * 100)) + '%'} |
| 79 | + />, |
| 80 | + }); |
| 81 | + try { |
| 82 | + // Make sure the instance is responding. |
| 83 | + await getInstanceUserInfo({ |
| 84 | + instanceClient, |
| 85 | + }); |
| 86 | + |
| 87 | + await setConfiguration({ |
| 88 | + ...params, |
| 89 | + instanceClient, |
| 90 | + }); |
| 91 | + // Then restart it. |
| 92 | + await restartInstance({ |
| 93 | + operation: 'restart_service', |
| 94 | + replicated: false, |
| 95 | + instanceClient, |
| 96 | + }); |
| 97 | + instancesRestarted += 1; |
| 98 | + } catch { |
| 99 | + if (i + 1 !== instanceClients.length) { |
| 100 | + // If it fails to restart, or wasn't available, warn for a bit then move on. |
| 101 | + toast.loading(`Failed Restarting Instance ${i + 1} of ${instanceClients.length}`, { |
| 102 | + ...toastConfig, |
| 103 | + id: toastId, |
| 104 | + description: 'We will carry on momentarily.', |
| 105 | + }); |
| 106 | + await sleep(3000); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + setIsPending(false); |
| 114 | + void queryClient.invalidateQueries({ queryKey: [operationsParams.entityId, 'get_configuration'] }); |
| 115 | + |
| 116 | + if (canceled) { |
| 117 | + toast.error('Cancelled', { |
| 118 | + id: toastId, |
| 119 | + description: `The config update was partially cancelled.`, |
| 120 | + duration: 10_000, |
| 121 | + action: { |
| 122 | + label: 'Dismiss', |
| 123 | + onClick: () => toast.dismiss(), |
| 124 | + }, |
| 125 | + }); |
| 126 | + } else if (allInstances.length === instancesRestarted) { |
| 127 | + onRestartedSuccessfully?.(); |
| 128 | + toast.success('Success', { |
| 129 | + id: toastId, |
| 130 | + description: `Your configuration has been updated successfully!`, |
| 131 | + duration: 10_000, |
| 132 | + action: { |
| 133 | + label: 'Dismiss', |
| 134 | + onClick: () => toast.dismiss(), |
| 135 | + }, |
| 136 | + }); |
| 137 | + } else { |
| 138 | + const allTheInstances = pluralize(allInstances.length, 'instance', 'instances'); |
| 139 | + const someRunningInstancesWere = pluralize( |
| 140 | + instancesRestarted, |
| 141 | + '"RUNNING" instance was', |
| 142 | + '"RUNNING" instances were', |
| 143 | + ); |
| 144 | + toast.error('Error', { |
| 145 | + id: toastId, |
| 146 | + description: `Failed to fully update cluster.\n` |
| 147 | + + ([ |
| 148 | + allInstances.length === 0 && 'No instances were found within the cluster to restart.', |
| 149 | + instancesRestarted === 0 && `No instances were in a "RUNNING" state of ${allTheInstances}.`, |
| 150 | + allInstances.length !== instancesRestarted && `Only ${someRunningInstancesWere} restarted of ${allTheInstances}.`, |
| 151 | + ].filter(excludeFalsy).shift() || ''), |
| 152 | + duration: 10_000, |
| 153 | + action: { |
| 154 | + label: 'Dismiss', |
| 155 | + onClick: () => toast.dismiss(), |
| 156 | + }, |
| 157 | + }); |
| 158 | + } |
| 159 | + }, [operationsParams, onRestartedSuccessfully]); |
| 160 | + |
| 161 | + return { |
| 162 | + onConfigUpdate, |
| 163 | + isPending, |
| 164 | + }; |
| 165 | +} |
0 commit comments