feat: task and inform

This commit is contained in:
archer
2023-06-06 22:45:46 +08:00
parent 55d0ed9de6
commit 7a38a81e12
7 changed files with 87 additions and 42 deletions

View File

@@ -31,7 +31,7 @@ export const usePagination = <T = any,>({
...params ...params
}); });
setPageNum(num); setPageNum(num);
res.total && setTotal(res.total); res.total !== undefined && setTotal(res.total);
setData(res.data); setData(res.data);
} catch (error: any) { } catch (error: any) {
toast({ toast({

View File

@@ -1,12 +1,13 @@
import type { NextApiRequest, NextApiResponse } from 'next'; import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response'; import { jsonRes } from '@/service/response';
import { connectToDatabase, User, Pay } from '@/service/mongo'; import { connectToDatabase, User, Pay, TrainingData } from '@/service/mongo';
import { authUser } from '@/service/utils/auth'; import { authUser } from '@/service/utils/auth';
import { PaySchema, UserModelSchema } from '@/types/mongoSchema'; import { PaySchema, UserModelSchema } from '@/types/mongoSchema';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { getPayResult } from '@/service/utils/wxpay'; import { getPayResult } from '@/service/utils/wxpay';
import { pushPromotionRecord } from '@/service/utils/promotion'; import { pushPromotionRecord } from '@/service/utils/promotion';
import { PRICE_SCALE } from '@/constants/common'; import { PRICE_SCALE } from '@/constants/common';
import { startQueue } from '@/service/utils/tools';
/* 校验支付结果 */ /* 校验支付结果 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -75,6 +76,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
jsonRes(res, { jsonRes(res, {
data: '支付成功' data: '支付成功'
}); });
unlockTask(userId);
} }
} catch (error) { } catch (error) {
await Pay.findByIdAndUpdate(payId, { await Pay.findByIdAndUpdate(payId, {
@@ -101,3 +103,19 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}); });
} }
} }
async function unlockTask(userId: string) {
try {
await TrainingData.updateMany(
{
userId
},
{
lockTime: new Date('2000/1/1')
}
);
startQueue();
} catch (error) {
unlockTask(userId);
}
}

View File

@@ -32,10 +32,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
export async function sendInform({ type, title, content, userId }: Props) { export async function sendInform({ type, title, content, userId }: Props) {
if (!type || !title || !content) { if (!type || !title || !content) {
return Promise.reject('参数错误'); return;
} }
try {
if (userId) { if (userId) {
// skip it if have same inform within 5 minutes
const inform = await Inform.findOne({
type,
title,
content,
userId,
read: false,
time: { $lte: new Date(Date.now() + 5 * 60 * 1000) }
});
if (inform) return;
await Inform.create({ await Inform.create({
type, type,
title, title,
@@ -56,6 +69,7 @@ export async function sendInform({ type, title, content, userId }: Props) {
userId: _id userId: _id
})) }))
); );
} catch (error) {
return; console.log('send inform error', error);
}
} }

View File

@@ -57,6 +57,7 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
return checkPayResult(payId); return checkPayResult(payId);
}, },
{ {
enabled: !!payId,
refetchInterval: 2000, refetchInterval: 2000,
onSuccess(res) { onSuccess(res) {
if (!res) return; if (!res) return;

View File

@@ -9,6 +9,7 @@ import { BillTypeEnum } from '@/constants/user';
import { pushDataToKb } from '@/pages/api/openapi/kb/pushData'; import { pushDataToKb } from '@/pages/api/openapi/kb/pushData';
import { TrainingModeEnum } from '@/constants/plugin'; import { TrainingModeEnum } from '@/constants/plugin';
import { ERROR_ENUM } from '../errorCode'; import { ERROR_ENUM } from '../errorCode';
import { sendInform } from '@/pages/api/user/inform/send';
const reduceQueue = () => { const reduceQueue = () => {
global.qaQueueLen = global.qaQueueLen > 0 ? global.qaQueueLen - 1 : 0; global.qaQueueLen = global.qaQueueLen > 0 ? global.qaQueueLen - 1 : 0;
@@ -174,11 +175,22 @@ A2:
} }
// 账号余额不足,删除任务 // 账号余额不足,删除任务
if (err === ERROR_ENUM.insufficientQuota) { if (userId && err === ERROR_ENUM.insufficientQuota) {
console.log('余额不足,删除向量生成任务'); sendInform({
await TrainingData.deleteMany({ type: 'system',
title: 'QA 任务中止',
content: '由于账号余额不足QA 任务中止,重新充值后将会继续。',
userId userId
}); });
console.log('余额不足,暂停向量生成任务');
await TrainingData.updateMany(
{
userId
},
{
lockTime: new Date('2999/5/5')
}
);
return generateQA(); return generateQA();
} }

View File

@@ -4,6 +4,7 @@ import { openaiEmbedding } from '@/pages/api/openapi/plugin/openaiEmbedding';
import { TrainingData } from '../models/trainingData'; import { TrainingData } from '../models/trainingData';
import { ERROR_ENUM } from '../errorCode'; import { ERROR_ENUM } from '../errorCode';
import { TrainingModeEnum } from '@/constants/plugin'; import { TrainingModeEnum } from '@/constants/plugin';
import { sendInform } from '@/pages/api/user/inform/send';
const reduceQueue = () => { const reduceQueue = () => {
global.vectorQueueLen = global.vectorQueueLen > 0 ? global.vectorQueueLen - 1 : 0; global.vectorQueueLen = global.vectorQueueLen > 0 ? global.vectorQueueLen - 1 : 0;
@@ -125,11 +126,22 @@ export async function generateVector(): Promise<any> {
} }
// 账号余额不足,删除任务 // 账号余额不足,删除任务
if (err === ERROR_ENUM.insufficientQuota) { if (userId && err === ERROR_ENUM.insufficientQuota) {
console.log('余额不足,删除向量生成任务'); sendInform({
await TrainingData.deleteMany({ type: 'system',
title: '索引生成任务中止',
content: '由于账号余额不足,索引生成任务中止,重新充值后将会继续。',
userId userId
}); });
console.log('余额不足,暂停向量生成任务');
await TrainingData.updateMany(
{
userId
},
{
lockTime: new Date('2999/5/5')
}
);
return generateVector(); return generateVector();
} }

View File

@@ -42,18 +42,6 @@ export async function connectToDatabase(): Promise<void> {
global.vectorQueueLen = 0; global.vectorQueueLen = 0;
startQueue(); startQueue();
// 5 分钟后解锁不正常的数据,并触发开始训练
setTimeout(async () => {
await TrainingData.updateMany(
{
lockTime: { $lte: Date.now() - 5 * 60 * 1000 }
},
{
lockTime: new Date('2000/1/1')
}
);
startQueue();
}, 5 * 60 * 1000);
} }
export * from './models/authCode'; export * from './models/authCode';