detect status

This commit is contained in:
josc146 2023-05-19 15:59:04 +08:00
parent db61d3f7f9
commit 7ba90ae7af
4 changed files with 28 additions and 4 deletions

View File

@ -6,9 +6,9 @@ Model_Config = "model_config"
class ModelStatus(Enum): class ModelStatus(Enum):
Offline = auto() Offline = 0
Loading = auto() Loading = 2
Working = auto() Working = 3
def init(): def init():

View File

@ -59,3 +59,8 @@ def update_config(body: ModelConfigBody):
global_var.set(global_var.Model_Config, body) global_var.set(global_var.Model_Config, body)
return "success" return "success"
@router.get("/status")
def status():
return {"status": global_var.get(global_var.Model_Status)}

View File

@ -1,4 +1,4 @@
import commonStore from '../stores/commonStore'; import commonStore, {ModelStatus} from '../stores/commonStore';
export const readRoot = async () => { export const readRoot = async () => {
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort; const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
@ -35,3 +35,17 @@ export const updateConfig = async (body: any) => {
body: JSON.stringify(body) body: JSON.stringify(body)
}); });
}; };
export const getStatus = async (timeout?: number): Promise<ModelStatus | undefined> => {
const controller = new AbortController();
if (timeout)
setTimeout(() => controller.abort(), timeout);
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
let ret: ModelStatus | undefined;
await fetch(`http://127.0.0.1:${port}/status`, {signal: controller.signal}).then(r => r.json()).then(data => {
ret = data.status;
}).catch(() => {
});
return ret;
};

View File

@ -1,11 +1,16 @@
import commonStore, {defaultModelConfigs} from './stores/commonStore'; import commonStore, {defaultModelConfigs} from './stores/commonStore';
import {ReadJson} from '../wailsjs/go/backend_golang/App'; import {ReadJson} from '../wailsjs/go/backend_golang/App';
import {downloadProgramFiles, LocalConfig, refreshModels} from './utils'; import {downloadProgramFiles, LocalConfig, refreshModels} from './utils';
import {getStatus} from './apis';
export async function startup() { export async function startup() {
downloadProgramFiles(); downloadProgramFiles();
initCache(); initCache();
await initConfig(); await initConfig();
getStatus(500).then(status => {
if (status)
commonStore.setModelStatus(status);
});
} }
async function initConfig() { async function initConfig() {