Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions redisinsight/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { WinstonModule } from 'nest-winston';
import { GlobalExceptionFilter } from 'src/exceptions/global-exception.filter';
import { get } from 'src/utils';
import { migrateHomeFolder } from 'src/init-helper';
import { LogFileProvider } from 'src/modules/profiler/providers/log-file.provider';
import { AppModule } from './app.module';
import SWAGGER_CONFIG from '../config/swagger';
import LOGGER_CONFIG from '../config/logger';

export default async function bootstrap() {
export default async function bootstrap(): Promise<Function> {
await migrateHomeFolder();

const serverConfig = get('server');
Expand Down Expand Up @@ -44,18 +45,28 @@ export default async function bootstrap() {
);
}

app.enableShutdownHooks();
const logFileProvider = app.get(LogFileProvider);

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

process.on('SIGTERM', () => {
logger.log('SIGTERM command received. Shutting down...');
const gracefulShutdown = (signal) => {
try {
logger.log(`Signal ${signal} received. Shutting down...`);
logFileProvider.onModuleDestroy();
} catch (e) {
// ignore errors if any
}
process.exit(0);
});
};

process.on('SIGTERM', gracefulShutdown);
process.on('SIGINT', gracefulShutdown);

return gracefulShutdown;
}

if (process.env.APP_ENV !== 'electron') {
Expand Down
4 changes: 2 additions & 2 deletions redisinsight/api/src/modules/profiler/models/log-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ export class LogFile {
/**
* Remove file and delete write stream after finish
*/
async destroy() {
destroy() {
try {
this.writeStream?.close();
this.writeStream = null;
const size = this.getFileSize();
await fs.unlink(this.filePath);
fs.unlink(this.filePath);

this.analyticsEvents.get(TelemetryEvents.ProfilerLogDeleted)(this.instanceId, size);
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions redisinsight/api/src/modules/profiler/profiler.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ import { ProfilerService } from './profiler.service';
ProfilerService,
],
controllers: [ProfilerController],
exports: [LogFileProvider],
})
export class ProfilerModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ export class LogFileProvider implements OnModuleDestroy {
return { stream, filename: logFile.getFilename() };
}

async onModuleDestroy() {
await Promise.all(Array.from(this.profilerLogFiles.values()).map((logFile: LogFile) => logFile.destroy()));
onModuleDestroy() {
this.profilerLogFiles.forEach((logFile) => {
try {
logFile.destroy();
} catch (e) {
// process other files on error
}
});
}
}
11 changes: 10 additions & 1 deletion redisinsight/main.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ export const getDisplayAppInTrayValue = (): boolean => {
* Backend part...
*/
const port = 5001;

let backendGracefulShutdown: Function;
const launchApiServer = async () => {
try {
const detectPortConst = await detectPort(port);
process.env.API_PORT = detectPortConst?.toString();
log.info('Available port:', detectPortConst);
await server();
backendGracefulShutdown = await server();
} catch (error) {
log.error('Catch server error:', error);
}
Expand Down Expand Up @@ -387,6 +389,13 @@ app.on('certificate-error', (event, _webContents, _url, _error, _certificate, ca
callback(true);
});

app.on('quit', () => {
try {
backendGracefulShutdown?.();
} catch (e) {
// ignore any error
}
});
// ipc events
ipcMain.handle(IpcEvent.getAppVersion, () => app?.getVersion());

Expand Down