Skip to content

Commit

Permalink
fix: cannot reset data while starting model
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Apr 11, 2024
1 parent 0d31f9e commit 902309e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
40 changes: 17 additions & 23 deletions web/hooks/useActiveModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,27 @@ export function useActiveModel() {
})
}

const stopModel = useCallback(
async (model?: Model) => {
const stoppingModel = activeModel || model
if (
!stoppingModel ||
(!model && stateModel.state === 'stop' && stateModel.loading)
)
return

setStateModel({ state: 'stop', loading: true, model: stoppingModel })
const engine = EngineManager.instance().get(stoppingModel.engine)
return engine
?.unloadModel(stoppingModel)
.catch()
.then(() => {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
loadModelController?.abort()
})
},
[activeModel, setActiveModel, setStateModel, stateModel]
)
const stopModel = useCallback(async () => {
const stoppingModel = activeModel || stateModel.model
if (!stoppingModel || (stateModel.state === 'stop' && stateModel.loading))
return

setStateModel({ state: 'stop', loading: true, model: stoppingModel })
const engine = EngineManager.instance().get(stoppingModel.engine)
return engine
?.unloadModel(stoppingModel)
.catch()
.then(() => {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
loadModelController?.abort()
})
}, [activeModel, setActiveModel, setStateModel, stateModel])

const stopInference = useCallback(async () => {
// Loading model
if (stateModel.loading) {
stopModel(stateModel.model)
stopModel()
return
}
if (!activeModel) return
Expand Down
16 changes: 8 additions & 8 deletions web/hooks/useFactoryReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useCallback } from 'react'
import { fs, AppConfiguration } from '@janhq/core'
import { atom, useAtomValue, useSetAtom } from 'jotai'

import { PromiseRetry } from '@/utils/retry'

import { useActiveModel } from './useActiveModel'

import { defaultJanDataFolderAtom } from '@/helpers/atoms/App.atom'
Expand All @@ -19,7 +21,7 @@ export const factoryResetStateAtom = atom(FactoryResetState.Idle)

export default function useFactoryReset() {
const defaultJanDataFolder = useAtomValue(defaultJanDataFolderAtom)
const { activeModel, stopModel } = useActiveModel()
const { stopModel } = useActiveModel()
const setFactoryResetState = useSetAtom(factoryResetStateAtom)

const resetAll = useCallback(
Expand All @@ -44,22 +46,20 @@ export default function useFactoryReset() {
await window.core?.api?.updateAppConfiguration(configuration)
}

if (activeModel) {
setFactoryResetState(FactoryResetState.StoppingModel)
await stopModel()
await new Promise((resolve) => setTimeout(resolve, 4000))
}
setFactoryResetState(FactoryResetState.StoppingModel)
await stopModel()

setFactoryResetState(FactoryResetState.DeletingData)
await fs.rm(janDataFolderPath)
// Try to delete the jan data folder
await PromiseRetry(fs.rm(janDataFolderPath), 10, 1000)

setFactoryResetState(FactoryResetState.ClearLocalStorage)
// reset the localStorage
localStorage.clear()

await window.core?.api?.relaunch()
},
[defaultJanDataFolder, activeModel, stopModel, setFactoryResetState]
[defaultJanDataFolder, stopModel, setFactoryResetState]
)

return {
Expand Down
24 changes: 24 additions & 0 deletions web/utils/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function PromiseRetry<T>(
promise: Promise<T>,
times: number = 5,
delay: number = 500
): Promise<T> {
return new Promise<T>((resolve, reject) => {
const retry = async () => {
let lastError: Error | undefined
for (let i = 0; i < times; i++) {
try {
const res = await promise
resolve(res)
return
} catch (err) {
lastError = err as Error
console.warn(err)
await new Promise((resolve) => setTimeout(resolve, delay))
}
}
reject(lastError)
}
retry()
})
}

0 comments on commit 902309e

Please sign in to comment.