mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Improve file handle closing in different situations
This should cover reloading main and cluster frames and closing cluster frame throught disconnecting cluster. No file handles should be left open now. Signed-off-by: Sami Tiilikainen <97873007+samitiilikainen@users.noreply.github.com>
This commit is contained in:
parent
0c83cfb0e8
commit
da946cf793
@ -3,20 +3,13 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { createLogger, format } from "winston";
|
|
||||||
import type { Logger } from "./logger";
|
import type { Logger } from "./logger";
|
||||||
import { loggerTransportInjectionToken } from "./logger/transports";
|
import winstonLoggerInjectable from "./winston-logger.injectable";
|
||||||
|
|
||||||
const loggerInjectable = getInjectable({
|
const loggerInjectable = getInjectable({
|
||||||
id: "logger",
|
id: "logger",
|
||||||
instantiate: (di): Logger => {
|
instantiate: (di): Logger => {
|
||||||
const baseLogger = createLogger({
|
const baseLogger = di.inject(winstonLoggerInjectable);
|
||||||
format: format.combine(
|
|
||||||
format.splat(),
|
|
||||||
format.simple(),
|
|
||||||
),
|
|
||||||
transports: di.injectMany(loggerTransportInjectionToken),
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
debug: (message, ...data) => baseLogger.debug(message, ...data),
|
debug: (message, ...data) => baseLogger.debug(message, ...data),
|
||||||
|
|||||||
20
packages/core/src/common/winston-logger.injectable.ts
Normal file
20
packages/core/src/common/winston-logger.injectable.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { createLogger, format } from "winston";
|
||||||
|
import { loggerTransportInjectionToken } from "./logger/transports";
|
||||||
|
|
||||||
|
const winstonLoggerInjectable = getInjectable({
|
||||||
|
id: "winston-logger",
|
||||||
|
instantiate: (di) => createLogger({
|
||||||
|
format: format.combine(
|
||||||
|
format.splat(),
|
||||||
|
format.simple(),
|
||||||
|
),
|
||||||
|
transports: di.injectMany(loggerTransportInjectionToken),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export default winstonLoggerInjectable;
|
||||||
@ -19,7 +19,6 @@ import { Router } from "react-router";
|
|||||||
import historyInjectable from "./navigation/history.injectable";
|
import historyInjectable from "./navigation/history.injectable";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import startFrameInjectable from "./start-frame/start-frame.injectable";
|
import startFrameInjectable from "./start-frame/start-frame.injectable";
|
||||||
import closeRendererLogFileInjectable from "./logger/close-renderer-log-file.injectable";
|
|
||||||
|
|
||||||
export async function bootstrap(di: DiContainer) {
|
export async function bootstrap(di: DiContainer) {
|
||||||
const startFrame = di.inject(startFrameInjectable);
|
const startFrame = di.inject(startFrameInjectable);
|
||||||
@ -55,15 +54,9 @@ export async function bootstrap(di: DiContainer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const unmount = () => {
|
await initializeApp(() => {
|
||||||
const closeLogFile = di.inject(closeRendererLogFileInjectable);
|
|
||||||
|
|
||||||
closeLogFile();
|
|
||||||
|
|
||||||
unmountComponentAtNode(rootElem);
|
unmountComponentAtNode(rootElem);
|
||||||
};
|
});
|
||||||
|
|
||||||
await initializeApp(unmount);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`[BOOTSTRAP]: view initialization error: ${error}`, {
|
console.error(`[BOOTSTRAP]: view initialization error: ${error}`, {
|
||||||
origin: location.href,
|
origin: location.href,
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.
|
|||||||
import loadExtensionsInjectable from "../../load-extensions.injectable";
|
import loadExtensionsInjectable from "../../load-extensions.injectable";
|
||||||
import loggerInjectable from "../../../../common/logger.injectable";
|
import loggerInjectable from "../../../../common/logger.injectable";
|
||||||
import showErrorNotificationInjectable from "../../../components/notifications/show-error-notification.injectable";
|
import showErrorNotificationInjectable from "../../../components/notifications/show-error-notification.injectable";
|
||||||
|
import closeRendererLogFileInjectable from "../../../logger/close-renderer-log-file.injectable";
|
||||||
|
|
||||||
const initClusterFrameInjectable = getInjectable({
|
const initClusterFrameInjectable = getInjectable({
|
||||||
id: "init-cluster-frame",
|
id: "init-cluster-frame",
|
||||||
@ -29,6 +30,7 @@ const initClusterFrameInjectable = getInjectable({
|
|||||||
emitAppEvent: di.inject(emitAppEventInjectable),
|
emitAppEvent: di.inject(emitAppEventInjectable),
|
||||||
logger: di.inject(loggerInjectable),
|
logger: di.inject(loggerInjectable),
|
||||||
showErrorNotification: di.inject(showErrorNotificationInjectable),
|
showErrorNotification: di.inject(showErrorNotificationInjectable),
|
||||||
|
closeFileLogging: di.inject(closeRendererLogFileInjectable),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
import { once } from "lodash";
|
||||||
import type { Cluster } from "../../../../common/cluster/cluster";
|
import type { Cluster } from "../../../../common/cluster/cluster";
|
||||||
import type { CatalogEntityRegistry } from "../../../api/catalog/entity/registry";
|
import type { CatalogEntityRegistry } from "../../../api/catalog/entity/registry";
|
||||||
import type { ShowNotification } from "../../../components/notifications";
|
import type { ShowNotification } from "../../../components/notifications";
|
||||||
@ -18,6 +19,7 @@ interface Dependencies {
|
|||||||
emitAppEvent: EmitAppEvent;
|
emitAppEvent: EmitAppEvent;
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
showErrorNotification: ShowNotification;
|
showErrorNotification: ShowNotification;
|
||||||
|
closeFileLogging: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const logPrefix = "[CLUSTER-FRAME]:";
|
const logPrefix = "[CLUSTER-FRAME]:";
|
||||||
@ -30,6 +32,7 @@ export const initClusterFrame = ({
|
|||||||
emitAppEvent,
|
emitAppEvent,
|
||||||
logger,
|
logger,
|
||||||
showErrorNotification,
|
showErrorNotification,
|
||||||
|
closeFileLogging,
|
||||||
}: Dependencies) =>
|
}: Dependencies) =>
|
||||||
async (unmountRoot: () => void) => {
|
async (unmountRoot: () => void) => {
|
||||||
// TODO: Make catalogEntityRegistry already initialized when passed as dependency
|
// TODO: Make catalogEntityRegistry already initialized when passed as dependency
|
||||||
@ -73,11 +76,14 @@ export const initClusterFrame = ({
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
window.onpagehide = () => {
|
const onCloseFrame = once(() => {
|
||||||
logger.info(
|
logger.info(
|
||||||
`${logPrefix} Unload dashboard, clusterId=${(hostedCluster.id)}, frameId=${frameRoutingId}`,
|
`${logPrefix} Unload dashboard, clusterId=${(hostedCluster.id)}, frameId=${frameRoutingId}`,
|
||||||
);
|
);
|
||||||
|
closeFileLogging();
|
||||||
unmountRoot();
|
unmountRoot();
|
||||||
};
|
});
|
||||||
|
|
||||||
|
window.addEventListener("beforeunload", onCloseFrame, { capture: true });
|
||||||
|
window.addEventListener("pagehide", onCloseFrame, { capture: true });
|
||||||
};
|
};
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import loggerInjectable from "../../../common/logger.injectable";
|
|||||||
import { delay } from "../../../common/utils";
|
import { delay } from "../../../common/utils";
|
||||||
import { broadcastMessage } from "../../../common/ipc";
|
import { broadcastMessage } from "../../../common/ipc";
|
||||||
import { bundledExtensionsLoaded } from "../../../common/ipc/extension-handling";
|
import { bundledExtensionsLoaded } from "../../../common/ipc/extension-handling";
|
||||||
|
import closeRendererLogFileInjectable from "../../logger/close-renderer-log-file.injectable";
|
||||||
|
|
||||||
const initRootFrameInjectable = getInjectable({
|
const initRootFrameInjectable = getInjectable({
|
||||||
id: "init-root-frame",
|
id: "init-root-frame",
|
||||||
@ -24,6 +25,7 @@ const initRootFrameInjectable = getInjectable({
|
|||||||
const lensProtocolRouterRenderer = di.inject(lensProtocolRouterRendererInjectable);
|
const lensProtocolRouterRenderer = di.inject(lensProtocolRouterRendererInjectable);
|
||||||
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
||||||
const logger = di.inject(loggerInjectable);
|
const logger = di.inject(loggerInjectable);
|
||||||
|
const closeRendererLogFile = di.inject(closeRendererLogFileInjectable);
|
||||||
|
|
||||||
return async (unmountRoot: () => void) => {
|
return async (unmountRoot: () => void) => {
|
||||||
catalogEntityRegistry.init();
|
catalogEntityRegistry.init();
|
||||||
@ -59,7 +61,7 @@ const initRootFrameInjectable = getInjectable({
|
|||||||
|
|
||||||
window.addEventListener("pagehide", () => {
|
window.addEventListener("pagehide", () => {
|
||||||
logger.info("[ROOT-FRAME]: Unload app");
|
logger.info("[ROOT-FRAME]: Unload app");
|
||||||
|
closeRendererLogFile();
|
||||||
unmountRoot();
|
unmountRoot();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,15 +3,18 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import winstonLoggerInjectable from "../../common/winston-logger.injectable";
|
||||||
import rendererFileLoggerTransportInjectable from "./file-transport.injectable";
|
import rendererFileLoggerTransportInjectable from "./file-transport.injectable";
|
||||||
|
|
||||||
const closeRendererLogFileInjectable = getInjectable({
|
const closeRendererLogFileInjectable = getInjectable({
|
||||||
id: "close-renderer-log-file",
|
id: "close-renderer-log-file",
|
||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
|
const winstonLogger = di.inject(winstonLoggerInjectable);
|
||||||
const fileLoggingTransport = di.inject(rendererFileLoggerTransportInjectable);
|
const fileLoggingTransport = di.inject(rendererFileLoggerTransportInjectable);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
fileLoggingTransport.close?.();
|
fileLoggingTransport.close?.();
|
||||||
|
winstonLogger.remove(fileLoggingTransport);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user