refactoring, fixes

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-21 16:01:20 +03:00
1 parent a40a9fec0e
commit 13c29a7fed
8 files changed
+58 -60

No files matched your search

+12 -28
View File
@@ -1,5 +1,5 @@
import type http from "http"
import { autorun, reaction } from "mobx";
import { autorun } from "mobx";
import { apiKubePrefix } from "../common/vars";
import { ClusterId, clusterStore } from "../common/cluster-store"
import { Cluster } from "./cluster"
@@ -7,52 +7,36 @@ import { clusterIpc } from "../common/cluster-ipc";
import logger from "./logger";
export class ClusterManager {
protected activeClusterId: ClusterId;
constructor(public readonly port: number) {
this.activeClusterId = clusterStore.activeClusterId;
// auto-init clusters
autorun(() => {
clusterStore.clusters.forEach(cluster => {
if (!cluster.initialized) {
logger.info(`[CLUSTER-MANAGER]: initializing cluster`, cluster.getMeta());
cluster.init(port);
cluster.init(port); // connect to kube-auth-proxy, context handling
cluster.bindEvents(); // send push-updates to renderer
}
});
});
// auto-bind events for active cluster
reaction(() => clusterStore.activeCluster, activeCluster => {
const prevCluster = clusterStore.getById(this.activeClusterId);
if (prevCluster) {
prevCluster.unbindEvents();
}
if (activeCluster) {
this.activeClusterId = activeCluster.id;
activeCluster.bindEvents();
activeCluster.refreshStatus();
activeCluster.pushState();
}
}, {
fireImmediately: true
});
// auto-stop removed clusters
autorun(() => {
const { removedClusters } = clusterStore;
if (removedClusters.size > 0) {
const meta = Array.from(removedClusters.values()).map(cluster => cluster.getMeta());
const removedClusters = Array.from(clusterStore.removedClusters.values());
if (removedClusters.length > 0) {
const meta = removedClusters.map(cluster => cluster.getMeta());
logger.info(`[CLUSTER-MANAGER]: removing clusters`, meta);
removedClusters.forEach(cluster => cluster.disconnect());
removedClusters.clear();
removedClusters.forEach(cluster => {
cluster.disconnect();
cluster.unbindEvents();
});
clusterStore.removedClusters.clear();
}
}, {
delay: 250
});
// listen for ipc-events that must/can be handled *only* in main-process (nodeIntegration=true)
clusterIpc.refresh.handleInMain();
clusterIpc.activate.handleInMain();
clusterIpc.disconnect.handleInMain();
clusterIpc.reconnect.handleInMain();
}
+22 -7
View File
@@ -49,6 +49,7 @@ export class Cluster implements ClusterModel {
@observable webContentUrl: string; // page content url for loading in renderer
@observable online: boolean;
@observable accessible: boolean;
@observable disconnected: boolean;
@observable failureReason: string;
@observable nodes = 0;
@observable version: string;
@@ -75,6 +76,9 @@ export class Cluster implements ClusterModel {
@action
async init(port: number) {
if (this.initialized) {
return;
}
try {
this.contextHandler = new ContextHandler(this);
this.kubeconfigManager = new KubeconfigManager(this, this.contextHandler);
@@ -97,11 +101,11 @@ export class Cluster implements ClusterModel {
bindEvents() {
logger.info(`[CLUSTER]: bind events`, this.getMeta());
const refreshStatusTimer = setInterval(() => this.refreshStatus(), 30000); // every 30s
const refreshEventsTimer = setInterval(() => this.refreshEvents(), 3000); // every 3s
const refreshTimer = setInterval(() => this.online && this.refresh(), 30000); // every 30s
const refreshEventsTimer = setInterval(() => this.online && this.refreshEvents(), 3000); // every 3s
this.disposers.push(
() => clearInterval(refreshStatusTimer),
() => clearInterval(refreshTimer),
() => clearInterval(refreshEventsTimer),
reaction(this.getState, this.pushState, {
fireImmediately: true
@@ -115,22 +119,32 @@ export class Cluster implements ClusterModel {
this.disposers.length = 0;
}
// fixme: possibly doesn't work as expected
async activate() {
await when(() => this.initialized);
if (this.disconnected) await this.reconnect();
await this.refresh();
return this.pushState();
}
// todo: check, possibly doesn't work as expected
async reconnect() {
logger.info(`[CLUSTER]: reconnect`, this.getMeta());
this.disconnected = false;
await this.contextHandler.stopServer();
await this.contextHandler.ensureServer();
}
@action
disconnect() {
logger.info(`[CLUSTER]: disconnect`, this.getMeta());
this.disconnected = true;
this.online = false;
this.accessible = false;
this.contextHandler.stopServer();
this.unbindEvents();
}
@action
async refreshStatus() {
await when(() => this.initialized);
async refresh() {
logger.info(`[CLUSTER]: refreshing status`, this.getMeta());
const connectionStatus = await this.getConnectionStatus();
this.online = connectionStatus > ClusterStatus.Offline;
@@ -351,6 +365,7 @@ export class Cluster implements ClusterModel {
name: this.contextName,
initialized: this.initialized,
accessible: this.accessible,
online: this.online,
}
}
}
+6 -6
View File
@@ -5,7 +5,6 @@ import type { ClusterId } from "../common/cluster-store";
import { clusterStore } from "../common/cluster-store";
import logger from "./logger";
// fixme: error when removing active cluster (can't activate next view => empty window)
// fixme: remove switching view delay on first load
export class WindowManager {
@@ -81,7 +80,7 @@ export class WindowManager {
return;
}
try {
const activeView = this.activeView;
const prevActiveView = this.activeView;
const isLoadedBefore = !!this.getView(clusterId);
const view = this.initView(clusterId);
logger.info(`[WINDOW-MANAGER]: activating cluster view`, {
@@ -90,7 +89,8 @@ export class WindowManager {
contextName: cluster.contextName,
isLoadedBefore: isLoadedBefore,
});
if (activeView !== view) {
if (prevActiveView !== view) {
cluster.activate(); // refresh + reconnect when required
this.activeView = view;
if (!isLoadedBefore) {
await when(() => cluster.initialized);
@@ -98,9 +98,9 @@ export class WindowManager {
this.hideSplash();
}
// refresh position and hide previous active window
if (activeView) {
view.setBounds(activeView.getBounds());
activeView.hide();
if (prevActiveView) {
view.setBounds(prevActiveView.getBounds());
prevActiveView.hide();
}
view.show();
return view.id;