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

Add whenCanResolveDomainName

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-11-22 16:27:35 +02:00
parent 8b112009ac
commit 6285ccaaa6
No known key found for this signature in database
GPG Key ID: 54B44603D251B788

View File

@ -40,6 +40,8 @@ import { Agent, AgentOptions } from "https";
import type { Patch } from "rfc6902";
import electron from "electron";
import { ipcMainOn } from "../ipc";
import { promises as dns } from "dns";
import retry from "async-retry";
export interface IKubeApiOptions<T extends KubeObject> {
/**
@ -555,6 +557,10 @@ export class KubeApi<T extends KubeObject> {
this.watchingNetworkStatus = true;
}
private whenCanResolveDomainName(url = "https://k8slens.dev/", retryOptions: Parameters<typeof retry>[1]) {
return retry(() => dns.resolve(url), retryOptions);
}
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
let errorReceived = false;
let timedRetry: NodeJS.Timeout;
@ -595,16 +601,22 @@ export class KubeApi<T extends KubeObject> {
electron.powerMonitor.once("resume", () => {
clearTimeout(timedRetry);
timedRetry = setTimeout(() => {
if (this.networkOnline) {
logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`);
this.watch({ ...opts, namespace, callback, watchId, retry: true });
} else {
logger.info(`[KUBE-API] system resumed but network appears to be offline, resume watching when online.`);
electron.ipcMain.once("network:online", () => {
logger.info(`[KUBE-API] network on line, resume watching of ${watchUrl}...`);
this.watch({ ...opts, namespace, callback, watchId, retry: true });
});
}
const url = "https://k8slens.dev/"; // url to check if domain names are resolvable.
this.whenCanResolveDomainName(url, { retries: 10 }).then(() => {
if (this.networkOnline) {
logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`);
this.watch({ ...opts, namespace, callback, watchId, retry: true });
} else {
logger.info(`[KUBE-API] system resumed but network appears to be offline, resume watching when online.`);
electron.ipcMain.once("network:online", () => {
logger.info(`[KUBE-API] network on line, resume watching of ${watchUrl}...`);
this.watch({ ...opts, namespace, callback, watchId, retry: true });
});
}
}).catch((error) => {
logger.error(`[KUBE-API] error resolving domain name ${url}`, error);
});
// 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);