add status for redis
This commit is contained in:
30
src/module/redis/get-app-status.ts
Normal file
30
src/module/redis/get-app-status.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { redis } from './redis.ts';
|
||||
|
||||
export type AppLoadStatus = {
|
||||
status: 'running' | 'loading' | 'error' | 'not-exist';
|
||||
message: string;
|
||||
};
|
||||
|
||||
export const getAppLoadStatus = async (user: string, app: string): Promise<AppLoadStatus> => {
|
||||
const key = 'user:app:status:' + app + ':' + user;
|
||||
const value = await redis.get(key);
|
||||
if (!value) {
|
||||
return {
|
||||
status: 'not-exist',
|
||||
message: 'not-exist',
|
||||
}; // 没有加载过
|
||||
}
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
} catch (err) {
|
||||
return {
|
||||
status: 'error',
|
||||
message: 'error',
|
||||
};
|
||||
}
|
||||
};
|
||||
export const setAppLoadStatus = async (user: string, app: string, status: AppLoadStatus) => {
|
||||
const key = 'user:app:status:' + app + ':' + user;
|
||||
const value = JSON.stringify(status);
|
||||
await redis.set(key, value);
|
||||
};
|
||||
Reference in New Issue
Block a user