mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Stop retry if system suspended, resume watch when system is resume
Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
parent
8f84f394fe
commit
3676eabc41
@ -38,6 +38,17 @@ import type { RequestInit } from "node-fetch";
|
|||||||
import AbortController from "abort-controller";
|
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";
|
||||||
|
|
||||||
|
const onceSystemResume = () =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
const handler = () => {
|
||||||
|
electron.powerMonitor.removeListener("resume", handler);
|
||||||
|
resolve(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
electron.powerMonitor.on("resume", handler);
|
||||||
|
});
|
||||||
|
|
||||||
export interface IKubeApiOptions<T extends KubeObject> {
|
export interface IKubeApiOptions<T extends KubeObject> {
|
||||||
/**
|
/**
|
||||||
@ -234,6 +245,8 @@ export class KubeApi<T extends KubeObject> {
|
|||||||
protected resourceVersions = new Map<string, string>();
|
protected resourceVersions = new Map<string, string>();
|
||||||
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 systemSuspended = false;
|
||||||
|
|
||||||
constructor(protected options: IKubeApiOptions<T>) {
|
constructor(protected options: IKubeApiOptions<T>) {
|
||||||
const {
|
const {
|
||||||
@ -525,6 +538,20 @@ export class KubeApi<T extends KubeObject> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private watchSystemStatus(forUrl: string) {
|
||||||
|
electron.powerMonitor.on("suspend", () => {
|
||||||
|
logger.info(`[KUBE-API] system suspended... ${forUrl}`);
|
||||||
|
this.systemSuspended = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
electron.powerMonitor.on("resume", () => {
|
||||||
|
logger.info(`[KUBE-API] system resumed! ${forUrl}`);
|
||||||
|
this.systemSuspended = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.watchingSystemStatus = 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;
|
||||||
@ -542,6 +569,19 @@ export class KubeApi<T extends KubeObject> {
|
|||||||
|
|
||||||
logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);
|
logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);
|
||||||
|
|
||||||
|
if (!this.watchingSystemStatus) {
|
||||||
|
this.watchSystemStatus(watchUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
onceSystemResume().then(() => {
|
||||||
|
clearTimeout(timedRetry);
|
||||||
|
timedRetry = setTimeout(() => {
|
||||||
|
this.watch({ ...opts, namespace, callback, watchId, retry: true });
|
||||||
|
// 3000 is a non-evident random value, we assume that after 3 seconds the system is ready
|
||||||
|
// to start watching again. (Network interface/DNS is ready etc.)
|
||||||
|
}, 3000);
|
||||||
|
});
|
||||||
|
|
||||||
responsePromise
|
responsePromise
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@ -555,6 +595,12 @@ export class KubeApi<T extends KubeObject> {
|
|||||||
if (errorReceived) return; // kubernetes errors should be handled in a callback
|
if (errorReceived) return; // kubernetes errors should be handled in a callback
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
|
if (this.systemSuspended) {
|
||||||
|
// don't retry if got "end", "close", "error" and system is suspended
|
||||||
|
// instead, wait for system resume and resume watch using onceSystemResume();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logger.info(`[KUBE-API] watch (${watchId}) ${eventName} ${watchUrl}`);
|
logger.info(`[KUBE-API] watch (${watchId}) ${eventName} ${watchUrl}`);
|
||||||
|
|
||||||
clearTimeout(timedRetry);
|
clearTimeout(timedRetry);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user