Also watch network status

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen committed 2021-11-23 09:22:44 +02:00
1 parent 5340ce24f8
commit c1120239db
1 file changed
+28 -3
+28 -3
View File
@@ -39,6 +39,7 @@ import AbortController from "abort-controller";
import { Agent, AgentOptions } from "https"; import { Agent, AgentOptions } from "https";
import type { Patch } from "rfc6902"; import type { Patch } from "rfc6902";
import electron from "electron"; import electron from "electron";
import { ipcMainOn } from "../ipc";
export interface IKubeApiOptions<T extends KubeObject> { export interface IKubeApiOptions<T extends KubeObject> {
/** /**
@@ -236,7 +237,9 @@ export class KubeApi<T extends KubeObject> {
protected watchDisposer: () => void; protected watchDisposer: () => void;
private watchId = 1; private watchId = 1;
private watchingSystemStatus = false; // If true, we are watching system status ('suspend'/'resume') private watchingSystemStatus = false; // If true, we are watching system status ('suspend'/'resume')
private watchingNetworkStatus = false;
private systemSuspended = false; private systemSuspended = false;
private networkOnline = true; // default is true as we assume the network is online when the KubeApi is initialized
constructor(protected options: IKubeApiOptions<T>) { constructor(protected options: IKubeApiOptions<T>) {
const { const {
@@ -540,6 +543,18 @@ export class KubeApi<T extends KubeObject> {
this.watchingSystemStatus = true; this.watchingSystemStatus = true;
} }
private watchNetworkStatus() {
ipcMainOn("network:online", () => {
this.networkOnline = true;
});
ipcMainOn("network:offline", () => {
this.networkOnline = false;
});
this.watchingNetworkStatus = true;
}
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void { watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
let errorReceived = false; let errorReceived = false;
let timedRetry: NodeJS.Timeout; let timedRetry: NodeJS.Timeout;
@@ -561,6 +576,10 @@ export class KubeApi<T extends KubeObject> {
this.watchSystemStatus(); this.watchSystemStatus();
} }
if (!this.watchingNetworkStatus) {
this.watchNetworkStatus();
}
electron.powerMonitor.once("suspend", () => { electron.powerMonitor.once("suspend", () => {
logger.info(`[KUBE-API] system suspended, abort watching of ${watchUrl}...`); logger.info(`[KUBE-API] system suspended, abort watching of ${watchUrl}...`);
@@ -573,9 +592,15 @@ export class KubeApi<T extends KubeObject> {
clearTimeout(timedRetry); clearTimeout(timedRetry);
logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`); logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`);
timedRetry = setTimeout(() => { timedRetry = setTimeout(() => {
this.watch({ ...opts, namespace, callback, watchId, retry: true }); if (this.networkOnline) {
// 3000 is a naive value, we assume that after 3 seconds the system is ready this.watch({ ...opts, namespace, callback, watchId, retry: true });
// to start watching again. (Network interface/DNS is ready etc.) } else {
electron.ipcMain.once("network:online", () => {
this.watch({ ...opts, namespace, callback, watchId, retry: true });
});
}
// 3000 is a naive value, we assume that after 3 seconds the system is ready
// to start watching again. (Network interface/DNS is ready etc.)
}, 3000); }, 3000);
}); });
}); });