Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry logic to getFunctionsForHostedProject on ECONNREFUSED #4101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,11 @@
"description": "%azureFunctions.requestTimeout%",
"default": 15
},
"azureFunctions.hostStartTimeout": {
"type": "number",
"description": "%azureFunctions.hostStartTimeout%",
"default": 60
},
"azureFunctions.defaultFunctionAppToDeploy": {
"scope": "resource",
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"azureFunctions.redeploy": "Redeploy",
"azureFunctions.reportIssue": "Report Issue...",
"azureFunctions.requestTimeout": "The timeout (in seconds) to be used when making requests, for example getting the latest templates.",
"azureFunctions.hostStartTimeout": "The timeout (in seconds) to be used when starting the Azure Functions host.",
"azureFunctions.restartFunctionApp": "Restart",
"azureFunctions.scmDoBuildDuringDeployment": "Set to true to build your project on the server. Currently only applicable for Linux Function Apps.",
"azureFunctions.setAzureWebJobsStorage": "Set AzureWebJobsStorage...",
Expand Down
55 changes: 45 additions & 10 deletions src/workspace/listLocalFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
*--------------------------------------------------------------------------------------------*/

import { type FunctionEnvelope } from "@azure/arm-appservice";
import { AzExtFsExtra, callWithTelemetryAndErrorHandling, nonNullProp, type IActionContext } from "@microsoft/vscode-azext-utils";
import { type AzExtPipelineResponse } from '@microsoft/vscode-azext-azureutils';
import { AzExtFsExtra, callWithTelemetryAndErrorHandling, nonNullProp, parseError, type IActionContext } from "@microsoft/vscode-azext-utils";
import { functionJsonFileName } from "../constants";
import { ParsedFunctionJson } from "../funcConfig/function";
import { runningFuncTaskMap } from "../funcCoreTools/funcHostTask";
import { ProjectNotRunningError, getFunctionFolders } from "../tree/localProject/LocalFunctionsTreeItem";
import { nonNullValue } from "../utils/nonNull";
import { isNodeV4Plus, isPythonV2Plus } from "../utils/programmingModelUtils";
import { requestUtils } from "../utils/requestUtils";
import { getWorkspaceSetting } from "../vsCodeConfig/settings";
import { LocalFunction, type ILocalFunction } from "./LocalFunction";
import { type LocalProjectInternal } from "./listLocalProjects";
import path = require("path");
Expand Down Expand Up @@ -62,22 +65,54 @@ export async function listLocalFunctions(project: LocalProjectInternal): Promise
}))!;
}

const timeoutKey: string = 'hostStartTimeout';

function getHostStartTimeoutMS(): number {
// Shouldn't be null because the setting has a default value
return nonNullValue(getWorkspaceSetting<number>(timeoutKey), timeoutKey) * 1000;
}

/**
* Some projects (e.g. .NET Isolated and PyStein (i.e. Python model >=2)) don't have typical "function.json" files, so we'll have to ping localhost to get functions (only available if the project is running)
*/
async function getFunctionsForHostedProject(context: IActionContext, project: LocalProjectInternal): Promise<ILocalFunction[]> {
if (runningFuncTaskMap.has(project.options.folder)) {
const hostRequest = await project.getHostRequest(context);
const functions = await requestUtils.sendRequestWithExtTimeout(context, {
url: `${hostRequest.url}/admin/functions`,
method: 'GET',
rejectUnauthorized: hostRequest.rejectUnauthorized
});
const timeout = getHostStartTimeoutMS();
const startTime = Date.now();
let functions: AzExtPipelineResponse | undefined = undefined;
let retry = true;
while (retry) {
retry = false;

try {
functions = await requestUtils.sendRequestWithExtTimeout(context, {
url: `${hostRequest.url}/admin/functions`,
method: 'GET',
rejectUnauthorized: hostRequest.rejectUnauthorized
});
} catch (error) {
// The functions host will not run immediately after starting debugging and will return ECONNREFUSED instead, so we want to retry for a period of time
const errorType = parseError(error).errorType;
if (errorType === 'ECONNREFUSED') {
const currentTime = Date.now();
if (currentTime - startTime < timeout) {
retry = true;
} else {
throw error;
}
} else {
throw error;
}
}
}

return (<FunctionEnvelope[]>functions.parsedBody).map(func => {
func = requestUtils.convertToAzureSdkObject(func);
return new LocalFunction(project, nonNullProp(func, 'name'), new ParsedFunctionJson(func.config), func)
});
if (functions !== undefined) {
return (<FunctionEnvelope[]>functions.parsedBody).map(func => {
func = requestUtils.convertToAzureSdkObject(func);
return new LocalFunction(project, nonNullProp(func, 'name'), new ParsedFunctionJson(func.config), func);
});
}
} else {
throw new ProjectNotRunningError();
}
Expand Down
Loading