mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00
fix: 多机部署,导致任务重复
This commit is contained in:
19
src/pages/api/openapi/startEvents.ts
Normal file
19
src/pages/api/openapi/startEvents.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { jsonRes } from '@/service/response';
|
||||||
|
import { generateQA } from '@/service/events/generateQA';
|
||||||
|
import { generateVector } from '@/service/events/generateVector';
|
||||||
|
|
||||||
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
|
generateQA();
|
||||||
|
generateVector();
|
||||||
|
|
||||||
|
jsonRes(res);
|
||||||
|
} catch (err) {
|
||||||
|
jsonRes(res, {
|
||||||
|
code: 500,
|
||||||
|
error: err
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@@ -8,7 +8,12 @@ import { ChatModelNameEnum } from '@/constants/model';
|
|||||||
import { pushSplitDataBill } from '@/service/events/pushBill';
|
import { pushSplitDataBill } from '@/service/events/pushBill';
|
||||||
|
|
||||||
export async function generateAbstract(next = false): Promise<any> {
|
export async function generateAbstract(next = false): Promise<any> {
|
||||||
|
if (process.env.queueTask !== '1') {
|
||||||
|
fetch(process.env.parentUrl || '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (global.generatingAbstract && !next) return;
|
if (global.generatingAbstract && !next) return;
|
||||||
|
|
||||||
global.generatingAbstract = true;
|
global.generatingAbstract = true;
|
||||||
|
|
||||||
const systemPrompt: ChatCompletionRequestMessage = {
|
const systemPrompt: ChatCompletionRequestMessage = {
|
||||||
|
@@ -13,7 +13,12 @@ import { ModelSplitDataSchema } from '@/types/mongoSchema';
|
|||||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 12);
|
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 12);
|
||||||
|
|
||||||
export async function generateQA(next = false): Promise<any> {
|
export async function generateQA(next = false): Promise<any> {
|
||||||
|
if (process.env.queueTask !== '1') {
|
||||||
|
fetch(process.env.parentUrl || '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (global.generatingQA === true && !next) return;
|
if (global.generatingQA === true && !next) return;
|
||||||
|
|
||||||
global.generatingQA = true;
|
global.generatingQA = true;
|
||||||
|
|
||||||
let dataId = null;
|
let dataId = null;
|
||||||
@@ -165,8 +170,13 @@ export async function generateQA(next = false): Promise<any> {
|
|||||||
console.log('生成QA错误:', error);
|
console.log('生成QA错误:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataId && error?.response?.data?.error?.type === 'insufficient_quota') {
|
// 没有余额或者凭证错误时,拒绝任务
|
||||||
console.log('api 余额不足');
|
if (
|
||||||
|
dataId &&
|
||||||
|
(+error.response?.status === 401 ||
|
||||||
|
error?.response?.data?.error?.type === 'insufficient_quota')
|
||||||
|
) {
|
||||||
|
console.log('api 异常,删除QA任务');
|
||||||
|
|
||||||
await SplitData.findByIdAndUpdate(dataId, {
|
await SplitData.findByIdAndUpdate(dataId, {
|
||||||
textList: [],
|
textList: [],
|
||||||
|
@@ -5,7 +5,13 @@ import { ModelDataStatusEnum } from '@/constants/redis';
|
|||||||
import { openaiCreateEmbedding, getOpenApiKey } from '../utils/openai';
|
import { openaiCreateEmbedding, getOpenApiKey } from '../utils/openai';
|
||||||
|
|
||||||
export async function generateVector(next = false): Promise<any> {
|
export async function generateVector(next = false): Promise<any> {
|
||||||
|
if (process.env.queueTask !== '1') {
|
||||||
|
fetch(process.env.parentUrl || '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (global.generatingVector && !next) return;
|
if (global.generatingVector && !next) return;
|
||||||
|
|
||||||
global.generatingVector = true;
|
global.generatingVector = true;
|
||||||
let dataId = null;
|
let dataId = null;
|
||||||
try {
|
try {
|
||||||
@@ -84,8 +90,13 @@ export async function generateVector(next = false): Promise<any> {
|
|||||||
console.log('生成向量错误:', error);
|
console.log('生成向量错误:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataId && error?.response?.data?.error?.type === 'insufficient_quota') {
|
// 没有余额或者凭证错误时,拒绝任务
|
||||||
console.log('api 余额不足,删除 redis 模型数据');
|
if (
|
||||||
|
dataId &&
|
||||||
|
(+error.response?.status === 401 ||
|
||||||
|
error?.response?.data?.error?.type === 'insufficient_quota')
|
||||||
|
) {
|
||||||
|
console.log('删除向量生成任务记录');
|
||||||
const redis = await connectRedis();
|
const redis = await connectRedis();
|
||||||
redis.del(dataId);
|
redis.del(dataId);
|
||||||
generateVector(true);
|
generateVector(true);
|
||||||
|
Reference in New Issue
Block a user