mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: cleanup temp files when exit
This commit is contained in:
1 parent
86cbc9d802
commit
632760bedc
2 files changed
+17
-1
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
import http from 'http';
|
import http from 'http';
|
||||||
import { resolve } from 'path';
|
import { tmpdir } from 'os';
|
||||||
|
import { join, resolve } from 'path';
|
||||||
import cac from 'cac';
|
import cac from 'cac';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import Koa from 'koa';
|
import Koa from 'koa';
|
||||||
@@ -484,14 +485,22 @@ export async function apply(pluginContext: Context) {
|
|||||||
${ctx.response.status} ${endTime - startTime}ms ${ctx.response.length}`);
|
${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({
|
app.use(Body({
|
||||||
multipart: true,
|
multipart: true,
|
||||||
jsonLimit: '8mb',
|
jsonLimit: '8mb',
|
||||||
formLimit: '8mb',
|
formLimit: '8mb',
|
||||||
formidable: {
|
formidable: {
|
||||||
|
uploadDir,
|
||||||
maxFileSize: parseMemoryMB(system.get('server.upload') || '256m') * 1024 * 1024,
|
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];
|
const layers = [baseLayer, rendererLayer(router, logger), responseLayer(logger), userLayer];
|
||||||
app.use(async (ctx, next) => await next().catch(console.error)).use(domainLayer);
|
app.use(async (ctx, next) => await next().catch(console.error)).use(domainLayer);
|
||||||
app.use(router.routes()).use(router.allowedMethods());
|
app.use(router.routes()).use(router.allowedMethods());
|
||||||
|
|||||||
@@ -2,8 +2,14 @@
|
|||||||
import { Logger } from './logger';
|
import { Logger } from './logger';
|
||||||
import * as bus from './service/bus';
|
import * as bus from './service/bus';
|
||||||
|
|
||||||
|
let terminating = false;
|
||||||
async function terminate() {
|
async function terminate() {
|
||||||
|
if (terminating) process.exit(1);
|
||||||
let hasError = false;
|
let hasError = false;
|
||||||
|
terminating = true;
|
||||||
|
setTimeout(() => {
|
||||||
|
new Logger('exit').info('Cleaning up temporary files... (Press Ctrl-C again to force exit)');
|
||||||
|
}, 1000);
|
||||||
try {
|
try {
|
||||||
await bus.parallel('app/exit');
|
await bus.parallel('app/exit');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -12,6 +18,7 @@ async function terminate() {
|
|||||||
process.exit(hasError ? 1 : 0);
|
process.exit(hasError ? 1 : 0);
|
||||||
}
|
}
|
||||||
process.on('SIGINT', terminate);
|
process.on('SIGINT', terminate);
|
||||||
|
process.on('SIGTERM', terminate);
|
||||||
|
|
||||||
const shell = new Logger('shell');
|
const shell = new Logger('shell');
|
||||||
async function executeCommand(input: string) {
|
async function executeCommand(input: string) {
|
||||||
|
|||||||
Reference in new issue
Block a user