From 473ecda1bd97ae9213422d52fbfdaab3827aed14 Mon Sep 17 00:00:00 2001 From: "Hung-Han (Henry) Chen" Date: Fri, 19 Nov 2021 17:31:41 +0200 Subject: [PATCH] Force abort when systen is suspended Signed-off-by: Hung-Han (Henry) Chen --- src/common/k8s-api/kube-api.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/common/k8s-api/kube-api.ts b/src/common/k8s-api/kube-api.ts index 6c0173aa19..89f5e899e5 100644 --- a/src/common/k8s-api/kube-api.ts +++ b/src/common/k8s-api/kube-api.ts @@ -50,6 +50,16 @@ const onceSystemResume = () => electron.powerMonitor.on("resume", handler); }); +const onceSystemSuspend = () => + new Promise(resolve => { + const handler = () => { + electron.powerMonitor.removeListener("suspend", handler); + resolve(true); + }; + + electron.powerMonitor.on("suspend", handler); + }); + export interface IKubeApiOptions { /** * base api-path for listing all resources, e.g. "/api/v1/pods" @@ -575,11 +585,23 @@ export class KubeApi { onceSystemResume().then(() => { clearTimeout(timedRetry); + logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`); 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); + }).catch((error) => { + logger.warn(`[KUBE-API] resume watching of ${watchUrl} error`, error); + }); + + onceSystemSuspend().then((suspended) => { + if (suspended) { + logger.info(`[KUBE-API] system suspended, abort watching ${watchUrl}...`); + abort?.(); + } + }).catch((error) => { + logger.warn(`[KUBE-API] abort watching of ${watchUrl} error`, error); }); responsePromise