add health request time out of 10 seconds

This commit is contained in:
alexweininger 2021-04-30 05:17:09 -07:00
parent 0e533b5d26
commit 53e6cf959c
3 changed files with 20 additions and 3 deletions

View file

@ -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<vscode.Tr
}
async getChildren(element?: HealthTreeItem): Promise<vscode.TreeItem[]> {
if (healthClient === undefined) {
return [];
}
// get children for root
if (element === undefined) {
const health = await healthClient.checkup();
const health = await promiseWithTimeout<AppwriteHealth | undefined>(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]);

View file

@ -34,7 +34,10 @@ export async function addProjectWizard(): Promise<AppwriteProjectConfiguration |
{ label: "Yes", description: "If running Appwrite on localhost, or local IP" },
{ label: "No", description: "If connecting to a remote Appwrite instance" },
],
{ placeHolder: "Allow communication with self-signed SSL certificates? (Select 'Yes' for connecting to Appwrite on localhost)" }
{
placeHolder: "Allow communication with self-signed SSL certificates? (Select 'Yes' for connecting to Appwrite on localhost)",
ignoreFocusOut: true,
}
);
if (selfSigned === undefined) {
return;

View file

@ -0,0 +1,11 @@
export const promiseWithTimeout = <T>(timeoutMs: number, promise: () => Promise<T>, failureMessage?: string): Promise<T> => {
let timeoutHandle: NodeJS.Timeout;
const timeoutPromise = new Promise<never>((resolve, reject) => {
timeoutHandle = setTimeout(() => reject(new Error(failureMessage)), timeoutMs);
});
return Promise.race([promise(), timeoutPromise]).then((result) => {
clearTimeout(timeoutHandle);
return result;
});
};