1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix race condition with multiple opens

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-07-28 14:55:02 -04:00
parent 1f0e79cee8
commit 2ef2cbb2df
5 changed files with 97 additions and 35 deletions

View File

@ -23,13 +23,17 @@ import { catalogCategoryRegistry } from "../catalog/catalog-category-registry";
import { CatalogEntity, CatalogEntityAddMenuContext, CatalogEntityContextMenuContext, CatalogEntityMetadata, CatalogEntityStatus } from "../catalog"; import { CatalogEntity, CatalogEntityAddMenuContext, CatalogEntityContextMenuContext, CatalogEntityMetadata, CatalogEntityStatus } from "../catalog";
import { clusterActivateHandler, clusterDeleteHandler, clusterDisconnectHandler, navigateToClusterHandler } from "../cluster-ipc"; import { clusterActivateHandler, clusterDeleteHandler, clusterDisconnectHandler, navigateToClusterHandler } from "../cluster-ipc";
import { ClusterStore } from "../cluster-store"; import { ClusterStore } from "../cluster-store";
import { onNewWindowForClusterHandler, requestMain } from "../ipc"; import { requestMain } from "../ipc";
import { CatalogCategory, CatalogCategorySpec } from "../catalog"; import { CatalogCategory, CatalogCategorySpec } from "../catalog";
import { addClusterURL } from "../routes"; import { addClusterURL } from "../routes";
import { app } from "electron"; import { app } from "electron";
import type { CatalogEntitySpec } from "../catalog/catalog-entity"; import type { CatalogEntityActionContext, CatalogEntitySpec } from "../catalog/catalog-entity";
import { HotbarStore } from "../hotbar-store"; import { HotbarStore } from "../hotbar-store";
export interface KubernetesClusterActionContextArgs extends Record<string, any> {
newWindow?: boolean;
}
export interface KubernetesClusterPrometheusMetrics { export interface KubernetesClusterPrometheusMetrics {
address?: { address?: {
namespace: string; namespace: string;
@ -90,8 +94,12 @@ export class KubernetesCluster extends CatalogEntity<KubernetesClusterMetadata,
} }
} }
async onRun() { async onRun(context: CatalogEntityActionContext<KubernetesClusterActionContextArgs>) {
await requestMain(navigateToClusterHandler, this.getId()); await requestMain(
navigateToClusterHandler,
this.getId(),
context.args?.newWindow ?? false,
);
} }
onDetailsOpen(): void { onDetailsOpen(): void {
@ -103,20 +111,28 @@ export class KubernetesCluster extends CatalogEntity<KubernetesClusterMetadata,
} }
async onContextMenuOpen(context: CatalogEntityContextMenuContext) { async onContextMenuOpen(context: CatalogEntityContextMenuContext) {
if (this.status.phase === "connected" || this.status.phase === "disconnected") { const runContext = () => ({
navigate: context.navigate,
setCommandPaletteContext: context.setCommandPaletteContext,
});
context.menuItems.push( context.menuItems.push(
{ {
title: "Open", title: "Open",
icon: "open_in_full", icon: "open_in_full",
onClick: () => this.onRun(), onClick: () => this.onRun(runContext()),
}, },
{ {
title: "Open in new window", title: "Open in new window",
icon: "launch", icon: "launch",
onClick: () => requestMain(onNewWindowForClusterHandler, this.getId()), onClick: () => this.onRun({
...runContext(),
args: {
newWindow: true,
}
}),
}, },
); );
}
if (!this.metadata.source || this.metadata.source === "local") { if (!this.metadata.source || this.metadata.source === "local") {
context.menuItems.push( context.menuItems.push(

View File

@ -95,9 +95,10 @@ export interface CatalogEntityStatus {
active?: boolean; active?: boolean;
} }
export interface CatalogEntityActionContext { export interface CatalogEntityActionContext<T extends Record<string, any> = Record<string, any>> {
navigate: (url: string) => void; navigate: (url: string) => void;
setCommandPaletteContext: (context?: CatalogEntity) => void; setCommandPaletteContext: (context?: CatalogEntity) => void;
args?: T;
} }
export interface CatalogEntityContextMenu { export interface CatalogEntityContextMenu {

View File

@ -51,6 +51,7 @@ export * from "./tar";
export * from "./toggle-set"; export * from "./toggle-set";
export * from "./toJS"; export * from "./toJS";
export * from "./type-narrowing"; export * from "./type-narrowing";
export * from "./map-helpers";
import * as iter from "./iter"; import * as iter from "./iter";

View File

@ -0,0 +1,35 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Get the current value ascociated with `key` or
* @param map The map to interact with
* @param key The key to optionally initialize
* @param value The value to use if the key needs initializing
* @returns The current value in the map
*/
export function getOrInsert<K, V>(map: Map<K, V>, key: K, value: V): V {
if (!map.has(key)) {
map.set(key, value);
}
return map.get(key);
}

View File

@ -19,20 +19,23 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import AwaitLock from "await-lock";
import type { IpcMainInvokeEvent } from "electron"; import type { IpcMainInvokeEvent } from "electron";
import { when } from "mobx";
import { KubernetesCluster } from "../../common/catalog-entities"; import { KubernetesCluster } from "../../common/catalog-entities";
import { ClusterFrames } from "../../common/cluster-frames"; import { ClusterFrames } from "../../common/cluster-frames";
import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, navigateToClusterHandler } from "../../common/cluster-ipc"; import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, navigateToClusterHandler } from "../../common/cluster-ipc";
import { ClusterId, ClusterStore } from "../../common/cluster-store"; import { ClusterId, ClusterStore } from "../../common/cluster-store";
import { appEventBus } from "../../common/event-bus"; import { appEventBus } from "../../common/event-bus";
import { ipcMainHandle, onNewWindowForClusterHandler } from "../../common/ipc"; import { ipcMainHandle } from "../../common/ipc";
import { getOrInsert } from "../../common/utils";
import { catalogEntityRegistry } from "../catalog"; import { catalogEntityRegistry } from "../catalog";
import { ClusterManager } from "../cluster-manager"; import { ClusterManager } from "../cluster-manager";
import { bundledKubectlPath } from "../kubectl"; import { bundledKubectlPath } from "../kubectl";
import logger from "../logger"; import logger from "../logger";
import { promiseExecFile } from "../promise-exec"; import { promiseExecFile } from "../promise-exec";
import { ResourceApplier } from "../resource-applier"; import { ResourceApplier } from "../resource-applier";
import { WindowManager } from "../window-manager"; import { NavigateFrameInfoSpecifier, WindowManager } from "../window-manager";
export function initIpcMainHandlers() { export function initIpcMainHandlers() {
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => { ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
@ -54,16 +57,6 @@ export function initIpcMainHandlers() {
} }
}); });
ipcMainHandle(navigateToClusterHandler, async (event, clusterId: ClusterId) => {
const cluster = ClusterStore.getInstance().getById(clusterId);
if (!cluster) {
return void logger.warn("[NAVIGATE-TO-CLUSTER]: unknown cluster", { clusterId });
}
await WindowManager.getInstance().navigate(`/cluster/${clusterId}`, { clusterId }, { windowId: event.sender.getProcessId() });
});
ipcMainHandle(clusterVisibilityHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId, visible: boolean) => { ipcMainHandle(clusterVisibilityHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId, visible: boolean) => {
const entity = catalogEntityRegistry.getById(clusterId); const entity = catalogEntityRegistry.getById(clusterId);
@ -153,18 +146,34 @@ export function initIpcMainHandlers() {
} }
}); });
ipcMainHandle(onNewWindowForClusterHandler, async (event, clusterId: ClusterId) => { const navigateLocks = new Map<ClusterId, AwaitLock>();
appEventBus.emit({ name: "cluster", action: "open-new-window" });
ipcMainHandle(navigateToClusterHandler, async (event, clusterId: ClusterId, newWindow?: boolean) => {
appEventBus.emit({ name: "cluster", action: "navigate" });
const cluster = ClusterStore.getInstance().getById(clusterId); const cluster = ClusterStore.getInstance().getById(clusterId);
if (!cluster) { if (!cluster) {
return void logger.info("Cannot open clutser in new window, unknown cluster Id", { clusterId }); return void logger.warn("[NAVIGATE-TO-CLUSTER]: unknown cluster", { clusterId });
} }
const lock = getOrInsert(navigateLocks, clusterId, new AwaitLock());
try { try {
await WindowManager.getInstance().navigate(`/cluster/${clusterId}`, { clusterId }, { windowId: true }); await lock.acquireAsync();
const specifics: NavigateFrameInfoSpecifier[] = [{ clusterId }];
if (newWindow) {
specifics.push({ windowId: true });
} else {
specifics.push({ windowId: event.sender.getProcessId() });
}
await WindowManager.getInstance().navigate(`/cluster/${clusterId}`, ...specifics);
await when(() => Boolean(ClusterFrames.getInstance().getFrameInfoByClusterId(clusterId)));
} catch (error) { } catch (error) {
logger.error("Failed to load url for new cluster window", error); logger.error("Failed to load url for new cluster window", error);
} finally {
lock.release();
} }
}); });
} }