Skip to content

Commit 9ffe8ee

Browse files
authored
fix: dev hot restart do not post s3 (#295)
* fix: upload file prefix * fix: hot restart do not post s3 avatars
1 parent 66c652d commit 9ffe8ee

File tree

4 files changed

+98
-71
lines changed

4 files changed

+98
-71
lines changed

modules/model/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ const allProviders: ProviderConfigType[] = [
7474
yi
7575
];
7676

77-
export const initModels = async () => {
78-
await initModelAvatars();
77+
export const initModels = async (reboot: boolean = false) => {
78+
if (!reboot) await initModelAvatars();
7979

8080
modelsBuffer.data = allProviders
8181
.map((item) => {

modules/tool/loadToolDev.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,29 @@ export const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
3131
const readmeFile = join(toolPath, 'README.md');
3232

3333
// Upload logo files if found
34-
for (const logoPath of logoFiles) {
35-
try {
36-
const logoFilename = logoPath.split('/').pop()!;
37-
const logoNameWithoutExt = logoFilename.split('.').slice(0, -1).join('.');
38-
await publicS3Server.uploadFileAdvanced({
39-
path: logoPath,
40-
defaultFilename: logoNameWithoutExt,
41-
prefix: UploadToolsS3Path + '/' + filename,
42-
keepRawFilename: true,
43-
contentType: mimeMap[parse(logoPath).ext]
44-
});
45-
addLog.debug(
46-
`📦 Uploaded tool logo file: ${filename} -> ${UploadToolsS3Path}/${filename}/${logoNameWithoutExt}`
47-
);
48-
} catch (error) {
49-
addLog.warn(`Failed to upload logo file ${logoPath}: ${error}`);
34+
if (!global.isReboot) {
35+
for (const logoPath of logoFiles) {
36+
try {
37+
const logoFilename = logoPath.split('/').pop()!;
38+
const logoNameWithoutExt = logoFilename.split('.').slice(0, -1).join('.');
39+
await publicS3Server.uploadFileAdvanced({
40+
path: logoPath,
41+
defaultFilename: logoNameWithoutExt,
42+
prefix: UploadToolsS3Path + '/' + filename,
43+
keepRawFilename: true,
44+
contentType: mimeMap[parse(logoPath).ext]
45+
});
46+
addLog.debug(
47+
`📦 Uploaded tool logo file: ${filename} -> ${UploadToolsS3Path}/${filename}/${logoNameWithoutExt}`
48+
);
49+
} catch (error) {
50+
addLog.warn(`Failed to upload logo file ${logoPath}: ${error}`);
51+
}
5052
}
5153
}
5254

5355
// Upload README.md if it exists
54-
if (existsSync(readmeFile)) {
56+
if (existsSync(readmeFile) && !global.isReboot) {
5557
try {
5658
await publicS3Server.uploadFileAdvanced({
5759
path: readmeFile,
@@ -102,7 +104,7 @@ export const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
102104
// Find logo files using glob pattern for child tool
103105
const childLogoFiles = await glob(`${childPath}/logo.*`);
104106

105-
if (childLogoFiles.length > 0) {
107+
if (childLogoFiles.length > 0 && !global.isReboot) {
106108
// Child has its own logo, upload it
107109
for (const logoPath of childLogoFiles) {
108110
try {
@@ -125,7 +127,7 @@ export const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
125127
} else {
126128
// Child doesn't have logo, use parent's logo
127129
const parentLogoFiles = await glob(`${toolPath}/logo.*`);
128-
if (parentLogoFiles.length > 0) {
130+
if (parentLogoFiles.length > 0 && !global.isReboot) {
129131
for (const parentLogoPath of parentLogoFiles) {
130132
try {
131133
const logoFilename = parentLogoPath.split('/').pop()!;
@@ -188,3 +190,7 @@ export const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
188190
tools.forEach((tool) => devToolIds.add(tool.toolId));
189191
return tools;
190192
};
193+
194+
declare global {
195+
var isReboot: boolean;
196+
}

runtime/dev/devServer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class DevServer {
1010
private serverProcess: Subprocess | null = null;
1111
private isRestarting = false;
1212
private debounceTimer: Timer | null = null;
13+
private isFirstStart = true;
1314

1415
// 启动开发环境
1516
async start() {
@@ -35,12 +36,18 @@ export class DevServer {
3536
await this.stopServer();
3637
}
3738

39+
const cmd = this.isFirstStart
40+
? ['bun', 'run', path.join(__dirname, '..', 'index.ts')]
41+
: ['bun', 'run', path.join(__dirname, '..', 'index.ts'), '--reboot'];
42+
3843
this.serverProcess = spawn({
39-
cmd: ['bun', 'run', path.join(__dirname, '..', 'index.ts')],
44+
cmd,
4045
stdout: 'inherit',
4146
stderr: 'inherit',
4247
stdin: 'inherit'
4348
});
49+
50+
this.isFirstStart = false;
4451
}
4552

4653
// 停止服务器进程

runtime/index.ts

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,75 @@ import { setupGlobalErrorHandling } from './utils/error';
1818

1919
const requestSizeLimit = `${Number(process.env.MAX_API_SIZE || 10)}mb`;
2020

21-
const app = express().use(
22-
express.json({ limit: requestSizeLimit }),
23-
express.urlencoded({ extended: true, limit: requestSizeLimit }),
24-
express.static(isProd ? 'public' : join(basePath, 'dist', 'public'), {
25-
maxAge: isProd ? '1d' : '0',
26-
etag: true,
27-
lastModified: true
28-
})
29-
);
21+
async function main(reboot: boolean = false) {
22+
const app = express().use(
23+
express.json({ limit: requestSizeLimit }),
24+
express.urlencoded({ extended: true, limit: requestSizeLimit }),
25+
express.static(isProd ? 'public' : join(basePath, 'dist', 'public'), {
26+
maxAge: isProd ? '1d' : '0',
27+
etag: true,
28+
lastModified: true
29+
})
30+
);
3031

31-
connectSignoz();
32+
connectSignoz();
3233

33-
// System
34-
initOpenAPI(app);
35-
initRouter(app);
36-
setupProxy();
34+
// System
35+
initOpenAPI(app);
36+
initRouter(app);
37+
setupProxy();
3738

38-
// DB
39-
try {
40-
await connectMongo(connectionMongo, MONGO_URL);
41-
} catch (error) {
42-
addLog.error('Failed to initialize services:', error);
43-
process.exit(1);
44-
}
39+
// DB
40+
try {
41+
await connectMongo(connectionMongo, MONGO_URL);
42+
} catch (error) {
43+
addLog.error('Failed to initialize services:', error);
44+
process.exit(1);
45+
}
4546

46-
await initializeS3();
47+
await initializeS3();
4748

48-
// Modules
49-
await refreshDir(tempDir); // upload pkg files, unpkg, temp dir
50-
await ensureDir(tempToolsDir); // ensure the unpkged tools temp dir
49+
// Modules
50+
await refreshDir(tempDir); // upload pkg files, unpkg, temp dir
51+
await ensureDir(tempToolsDir); // ensure the unpkged tools temp dir
5152

52-
await Promise.all([
53-
getCachedData(SystemCacheKeyEnum.systemTool), // init system tool
54-
initModels(),
55-
initWorkflowTemplates()
56-
]);
53+
await Promise.all([
54+
getCachedData(SystemCacheKeyEnum.systemTool), // init system tool
55+
initModels(reboot),
56+
initWorkflowTemplates()
57+
]);
5758

58-
const PORT = parseInt(process.env.PORT || '3000');
59-
const server = app.listen(PORT, (error?: Error) => {
60-
if (error) {
61-
console.error(error);
62-
process.exit(1);
63-
}
64-
addLog.info(`FastGPT Plugin Service is listening at http://localhost:${PORT}`);
65-
});
59+
const PORT = parseInt(process.env.PORT || '3000');
60+
const server = app.listen(PORT, (error?: Error) => {
61+
if (error) {
62+
console.error(error);
63+
process.exit(1);
64+
}
65+
addLog.info(`FastGPT Plugin Service is listening at http://localhost:${PORT}`);
66+
});
6667

67-
['SIGTERM', 'SIGINT'].forEach((signal) =>
68-
process.on(signal, () => {
69-
addLog.debug(`${signal} signal received: closing HTTP server`);
70-
server.close(() => {
71-
addLog.info('HTTP server closed');
72-
process.exit(0);
73-
});
74-
})
75-
);
68+
['SIGTERM', 'SIGINT'].forEach((signal) =>
69+
process.on(signal, () => {
70+
addLog.debug(`${signal} signal received: closing HTTP server`);
71+
server.close(() => {
72+
addLog.info('HTTP server closed');
73+
process.exit(0);
74+
});
75+
})
76+
);
7677

77-
// 全局错误处理设置
78-
setupGlobalErrorHandling(app);
78+
// 全局错误处理设置
79+
setupGlobalErrorHandling(app);
80+
}
81+
82+
if (import.meta.main) {
83+
// get the arguments from the command line
84+
const args = process.argv.slice(2);
85+
const reboot = args.includes('--reboot');
86+
global.isReboot = reboot;
87+
await main(reboot);
88+
}
89+
90+
declare global {
91+
var isReboot: boolean;
92+
}

0 commit comments

Comments
 (0)