Skip to content

Commit 386cb38

Browse files
author
arthosofteq
authored
Merge pull request #552 from RedisInsight/bugfix/RI-2734-remove-files-after-shutting-down
remove files after shutting down
2 parents 932ab14 + d2ca35f commit 386cb38

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

redisinsight/api/src/main.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { WinstonModule } from 'nest-winston';
77
import { GlobalExceptionFilter } from 'src/exceptions/global-exception.filter';
88
import { get } from 'src/utils';
99
import { migrateHomeFolder } from 'src/init-helper';
10+
import { LogFileProvider } from 'src/modules/profiler/providers/log-file.provider';
1011
import { AppModule } from './app.module';
1112
import SWAGGER_CONFIG from '../config/swagger';
1213
import LOGGER_CONFIG from '../config/logger';
1314

14-
export default async function bootstrap() {
15+
export default async function bootstrap(): Promise<Function> {
1516
await migrateHomeFolder();
1617

1718
const serverConfig = get('server');
@@ -44,18 +45,28 @@ export default async function bootstrap() {
4445
);
4546
}
4647

47-
app.enableShutdownHooks();
48+
const logFileProvider = app.get(LogFileProvider);
4849

4950
await app.listen(port);
5051
logger.log({
5152
message: `Server is running on http(s)://localhost:${port}`,
5253
context: 'bootstrap',
5354
});
5455

55-
process.on('SIGTERM', () => {
56-
logger.log('SIGTERM command received. Shutting down...');
56+
const gracefulShutdown = (signal) => {
57+
try {
58+
logger.log(`Signal ${signal} received. Shutting down...`);
59+
logFileProvider.onModuleDestroy();
60+
} catch (e) {
61+
// ignore errors if any
62+
}
5763
process.exit(0);
58-
});
64+
};
65+
66+
process.on('SIGTERM', gracefulShutdown);
67+
process.on('SIGINT', gracefulShutdown);
68+
69+
return gracefulShutdown;
5970
}
6071

6172
if (process.env.APP_ENV !== 'electron') {

redisinsight/api/src/modules/profiler/models/log-file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ export class LogFile {
117117
/**
118118
* Remove file and delete write stream after finish
119119
*/
120-
async destroy() {
120+
destroy() {
121121
try {
122122
this.writeStream?.close();
123123
this.writeStream = null;
124124
const size = this.getFileSize();
125-
await fs.unlink(this.filePath);
125+
fs.unlink(this.filePath);
126126

127127
this.analyticsEvents.get(TelemetryEvents.ProfilerLogDeleted)(this.instanceId, size);
128128
} catch (e) {

redisinsight/api/src/modules/profiler/profiler.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ import { ProfilerService } from './profiler.service';
1919
ProfilerService,
2020
],
2121
controllers: [ProfilerController],
22+
exports: [LogFileProvider],
2223
})
2324
export class ProfilerModule {}

redisinsight/api/src/modules/profiler/providers/log-file.provider.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ export class LogFileProvider implements OnModuleDestroy {
4747
return { stream, filename: logFile.getFilename() };
4848
}
4949

50-
async onModuleDestroy() {
51-
await Promise.all(Array.from(this.profilerLogFiles.values()).map((logFile: LogFile) => logFile.destroy()));
50+
onModuleDestroy() {
51+
this.profilerLogFiles.forEach((logFile) => {
52+
try {
53+
logFile.destroy();
54+
} catch (e) {
55+
// process other files on error
56+
}
57+
});
5258
}
5359
}

redisinsight/main.dev.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@ export const getDisplayAppInTrayValue = (): boolean => {
9797
* Backend part...
9898
*/
9999
const port = 5001;
100+
101+
let backendGracefulShutdown: Function;
100102
const launchApiServer = async () => {
101103
try {
102104
const detectPortConst = await detectPort(port);
103105
process.env.API_PORT = detectPortConst?.toString();
104106
log.info('Available port:', detectPortConst);
105-
await server();
107+
backendGracefulShutdown = await server();
106108
} catch (error) {
107109
log.error('Catch server error:', error);
108110
}
@@ -387,6 +389,13 @@ app.on('certificate-error', (event, _webContents, _url, _error, _certificate, ca
387389
callback(true);
388390
});
389391

392+
app.on('quit', () => {
393+
try {
394+
backendGracefulShutdown?.();
395+
} catch (e) {
396+
// ignore any error
397+
}
398+
});
390399
// ipc events
391400
ipcMain.handle(IpcEvent.getAppVersion, () => app?.getVersion());
392401

0 commit comments

Comments
 (0)