From 53e6cf959cf1e36f25ce88593f0296c1d485427d Mon Sep 17 00:00:00 2001 From: alexweininger Date: Fri, 30 Apr 2021 05:17:09 -0700 Subject: [PATCH] add health request time out of 10 seconds --- src/tree/health/HealthTreeItemProvider.ts | 7 +++++-- src/ui/AddProjectWizard.ts | 5 ++++- src/utils/promiseWithTimeout.ts | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/utils/promiseWithTimeout.ts diff --git a/src/tree/health/HealthTreeItemProvider.ts b/src/tree/health/HealthTreeItemProvider.ts index 95970b3..bbb95b0 100644 --- a/src/tree/health/HealthTreeItemProvider.ts +++ b/src/tree/health/HealthTreeItemProvider.ts @@ -6,6 +6,7 @@ import * as dayjs from "dayjs"; import * as localizedFormat from "dayjs/plugin/localizedFormat"; import { healthTooltips } from "../../appwrite/Health"; import { AppwriteHealth } from "../../appwrite"; +import { promiseWithTimeout } from "../../utils/promiseWithTimeout"; // dayjs.extend(relativeTime); dayjs.extend(localizedFormat); @@ -27,14 +28,16 @@ export class HealthTreeItemProvider implements vscode.TreeDataProvider { - if (healthClient === undefined) { return []; } // get children for root if (element === undefined) { - const health = await healthClient.checkup(); + const health = await promiseWithTimeout(10000, async () => await healthClient?.checkup(), 'Health request timed out'); + if (health === undefined) { + return []; + } ext.outputChannel?.append(JSON.stringify(health, null, 4)); const healthItems = Object.entries(health).map(([service, status]) => { return new HealthTreeItem(service, status, healthTooltips[service as keyof AppwriteHealth]); diff --git a/src/ui/AddProjectWizard.ts b/src/ui/AddProjectWizard.ts index a31a654..8e4d84e 100644 --- a/src/ui/AddProjectWizard.ts +++ b/src/ui/AddProjectWizard.ts @@ -34,7 +34,10 @@ export async function addProjectWizard(): Promise(timeoutMs: number, promise: () => Promise, failureMessage?: string): Promise => { + let timeoutHandle: NodeJS.Timeout; + const timeoutPromise = new Promise((resolve, reject) => { + timeoutHandle = setTimeout(() => reject(new Error(failureMessage)), timeoutMs); + }); + + return Promise.race([promise(), timeoutPromise]).then((result) => { + clearTimeout(timeoutHandle); + return result; + }); +};