mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
Perf system plugin and worker (#2126)
* perf: worker pool * perf: worker register * perf: worker controller * perf: system plugin worker * perf: system plugin worker * perf: worker * perf: worker * worker timeout * perf: copy icon
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
"dependencies": {
|
||||
"duck-duck-scrape": "^2.2.5",
|
||||
"lodash": "^4.17.21",
|
||||
"axios": "^1.5.1",
|
||||
"expr-eval": "^2.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@@ -4,10 +4,12 @@ import { FastGPTProUrl, isProduction } from '../service/common/system/constants'
|
||||
import { GET, POST } from '@fastgpt/service/common/api/plusRequest';
|
||||
import { SystemPluginTemplateItemType } from '@fastgpt/global/core/workflow/type';
|
||||
import { cloneDeep } from 'lodash';
|
||||
import { WorkerNameEnum, runWorker } from '@fastgpt/service/worker/utils';
|
||||
|
||||
let list = [
|
||||
'getTime',
|
||||
'fetchUrl',
|
||||
// Run in main thread
|
||||
const staticPluginList = ['getTime', 'fetchUrl'];
|
||||
// Run in worker thread (Have npm packages)
|
||||
const packagePluginList = [
|
||||
'mathExprVal',
|
||||
'duckduckgo',
|
||||
'duckduckgo/search',
|
||||
@@ -16,6 +18,8 @@ let list = [
|
||||
'duckduckgo/searchVideo'
|
||||
];
|
||||
|
||||
const list = [...staticPluginList, ...packagePluginList];
|
||||
|
||||
/* Get plugins */
|
||||
export const getCommunityPlugins = () => {
|
||||
return list.map<SystemPluginTemplateItemType>((name) => {
|
||||
@@ -58,8 +62,7 @@ export const getSystemPluginTemplates = async (refresh = false) => {
|
||||
};
|
||||
|
||||
export const getCommunityCb = async () => {
|
||||
// Do not modify the following code
|
||||
const loadModule = async (name: string) => {
|
||||
const loadCommunityModule = async (name: string) => {
|
||||
const module = await import(`./src/${name}/index`);
|
||||
return module.default;
|
||||
};
|
||||
@@ -70,7 +73,14 @@ export const getCommunityCb = async () => {
|
||||
try {
|
||||
return {
|
||||
name,
|
||||
cb: await loadModule(name)
|
||||
cb: staticPluginList.includes(name)
|
||||
? await loadCommunityModule(name)
|
||||
: (e: any) => {
|
||||
return runWorker(WorkerNameEnum.systemPluginRun, {
|
||||
pluginName: name,
|
||||
data: e
|
||||
});
|
||||
}
|
||||
};
|
||||
} catch (error) {}
|
||||
})
|
||||
|
24
packages/plugins/runtime/worker.ts
Normal file
24
packages/plugins/runtime/worker.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { SystemPluginResponseType } from '../type';
|
||||
import { parentPort } from 'worker_threads';
|
||||
|
||||
const loadModule = async (name: string): Promise<(e: any) => SystemPluginResponseType> => {
|
||||
const module = await import(`../src/${name}/index`);
|
||||
return module.default;
|
||||
};
|
||||
|
||||
parentPort?.on('message', async ({ pluginName, data }: { pluginName: string; data: any }) => {
|
||||
try {
|
||||
const cb = await loadModule(pluginName);
|
||||
parentPort?.postMessage({
|
||||
type: 'success',
|
||||
data: await cb(data)
|
||||
});
|
||||
} catch (error) {
|
||||
parentPort?.postMessage({
|
||||
type: 'error',
|
||||
data: error
|
||||
});
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
@@ -32,14 +32,13 @@ const main = async (props: Props, retry = 3): Response => {
|
||||
};
|
||||
} catch (error) {
|
||||
if (retry <= 0) {
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
return {
|
||||
result: 'Failed to fetch data'
|
||||
};
|
||||
}
|
||||
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
|
||||
await delay(Math.random() * 2000);
|
||||
await delay(Math.random() * 5000);
|
||||
return main(props, retry - 1);
|
||||
}
|
||||
};
|
||||
|
@@ -31,14 +31,13 @@ const main = async (props: Props, retry = 3): Response => {
|
||||
};
|
||||
} catch (error) {
|
||||
if (retry <= 0) {
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
return {
|
||||
result: 'Failed to fetch data'
|
||||
};
|
||||
}
|
||||
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
|
||||
await delay(Math.random() * 2000);
|
||||
await delay(Math.random() * 5000);
|
||||
return main(props, retry - 1);
|
||||
}
|
||||
};
|
||||
|
@@ -32,14 +32,13 @@ const main = async (props: Props, retry = 3): Response => {
|
||||
};
|
||||
} catch (error) {
|
||||
if (retry <= 0) {
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
return {
|
||||
result: 'Failed to fetch data'
|
||||
};
|
||||
}
|
||||
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
|
||||
await delay(Math.random() * 2000);
|
||||
await delay(Math.random() * 5000);
|
||||
return main(props, retry - 1);
|
||||
}
|
||||
};
|
||||
|
@@ -32,14 +32,13 @@ const main = async (props: Props, retry = 3): Response => {
|
||||
};
|
||||
} catch (error) {
|
||||
if (retry <= 0) {
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
return {
|
||||
result: 'Failed to fetch data'
|
||||
};
|
||||
}
|
||||
|
||||
addLog.warn('DuckDuckGo error', { error });
|
||||
|
||||
await delay(Math.random() * 2000);
|
||||
await delay(Math.random() * 5000);
|
||||
return main(props, retry - 1);
|
||||
}
|
||||
};
|
||||
|
@@ -1,4 +1,3 @@
|
||||
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
|
||||
import { Parser } from 'expr-eval';
|
||||
|
||||
type Props = {
|
||||
|
Reference in New Issue
Block a user