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
});
setPageNum(num);
res.total && setTotal(res.total);
res.total !== undefined && setTotal(res.total);
setData(res.data);
} catch (error: any) {
toast({

View File

@@ -1,12 +1,13 @@
import type { NextApiRequest, NextApiResponse } from 'next';
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 { PaySchema, UserModelSchema } from '@/types/mongoSchema';
import dayjs from 'dayjs';
import { getPayResult } from '@/service/utils/wxpay';
import { pushPromotionRecord } from '@/service/utils/promotion';
import { PRICE_SCALE } from '@/constants/common';
import { startQueue } from '@/service/utils/tools';
/* 校验支付结果 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -75,6 +76,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
jsonRes(res, {
data: '支付成功'
});
unlockTask(userId);
}
} catch (error) {
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,30 +32,44 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
export async function sendInform({ type, title, content, userId }: Props) {
if (!type || !title || !content) {
return Promise.reject('参数错误');
}
if (userId) {
await Inform.create({
type,
title,
content,
userId
});
return;
}
// send to all user
const users = await User.find({}, '_id');
await Inform.insertMany(
users.map(({ _id }) => ({
type,
title,
content,
userId: _id
}))
);
try {
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) }
});
return;
if (inform) return;
await Inform.create({
type,
title,
content,
userId
});
return;
}
// send to all user
const users = await User.find({}, '_id');
await Inform.insertMany(
users.map(({ _id }) => ({
type,
title,
content,
userId: _id
}))
);
} catch (error) {
console.log('send inform error', error);
}
}

View File

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

View File

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

View File

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

View File

@@ -42,18 +42,6 @@ export async function connectToDatabase(): Promise<void> {
global.vectorQueueLen = 0;
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';