core: cleanup temp files when exit

This commit is contained in:
undefined committed 2022-12-17 07:33:48 +08:00
1 parent 86cbc9d802
commit 632760bedc
2 files changed
+17 -1

No files matched your search

+10 -1
View File
@@ -1,5 +1,6 @@
import http from 'http';
import { resolve } from 'path';
import { tmpdir } from 'os';
import { join, resolve } from 'path';
import cac from 'cac';
import fs from 'fs-extra';
import Koa from 'koa';
@@ -484,14 +485,22 @@ export async function apply(pluginContext: Context) {
${ctx.response.status} ${endTime - startTime}ms ${ctx.response.length}`);
});
}
const uploadDir = join(tmpdir(), 'hydro', 'upload', process.env.NODE_APP_INSTANCE || '0');
fs.ensureDirSync(uploadDir);
logger.debug('Using upload dir: %s', uploadDir);
app.use(Body({
multipart: true,
jsonLimit: '8mb',
formLimit: '8mb',
formidable: {
uploadDir,
maxFileSize: parseMemoryMB(system.get('server.upload') || '256m') * 1024 * 1024,
keepExtensions: true,
},
}));
pluginContext.on('app/exit', () => {
fs.emptyDirSync(uploadDir);
});
const layers = [baseLayer, rendererLayer(router, logger), responseLayer(logger), userLayer];
app.use(async (ctx, next) => await next().catch(console.error)).use(domainLayer);
app.use(router.routes()).use(router.allowedMethods());
+7
View File
@@ -2,8 +2,14 @@
import { Logger } from './logger';
import * as bus from './service/bus';
let terminating = false;
async function terminate() {
if (terminating) process.exit(1);
let hasError = false;
terminating = true;
setTimeout(() => {
new Logger('exit').info('Cleaning up temporary files... (Press Ctrl-C again to force exit)');
}, 1000);
try {
await bus.parallel('app/exit');
} catch (e) {
@@ -12,6 +18,7 @@ async function terminate() {
process.exit(hasError ? 1 : 0);
}
process.on('SIGINT', terminate);
process.on('SIGTERM', terminate);
const shell = new Logger('shell');
async function executeCommand(input: string) {