File size: 1,657 Bytes
8ede856 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import axios from 'axios'
import { getDesktopRuntimeInfo } from '@/utils/desktopRuntime'
type WaitingForRestartRef = {
check: (initialStartTime?: number | null) => void | Promise<void>
stop?: () => void
}
async function triggerWaiting(
waitingRef?: WaitingForRestartRef | null,
initialStartTime?: number | null
) {
if (!waitingRef) return
await waitingRef.check(initialStartTime)
}
async function fetchCurrentStartTime(): Promise<number | null> {
try {
const response = await axios.get('/api/stat/start-time', { timeout: 1500 })
const rawStartTime = response?.data?.data?.start_time
const numericStartTime = Number(rawStartTime)
return Number.isFinite(numericStartTime) ? numericStartTime : null
} catch (_error) {
return null
}
}
export async function restartAstrBot(
waitingRef?: WaitingForRestartRef | null
): Promise<void> {
const { bridge: desktopBridge, hasDesktopRestartCapability, isDesktopRuntime } =
await getDesktopRuntimeInfo()
if (desktopBridge && hasDesktopRestartCapability && isDesktopRuntime) {
const authToken = localStorage.getItem('token')
const initialStartTime = await fetchCurrentStartTime()
try {
const restartPromise = desktopBridge.restartBackend(authToken)
await triggerWaiting(waitingRef, initialStartTime)
const result = await restartPromise
if (!result.ok) {
waitingRef?.stop?.()
throw new Error(result.reason || 'Failed to restart backend.')
}
} catch (error) {
waitingRef?.stop?.()
throw error
}
return
}
await axios.post('/api/stat/restart-core')
await triggerWaiting(waitingRef)
}
|