Skip to content

Commit

Permalink
Use Uri.fspath as key for FuncRunningMap/Port (#4309)
Browse files Browse the repository at this point in the history
* Use Uri.fspath as key for FuncRunningMap/Port

This commit modifies pickFuncProcess startFuncProcessFromApi removing
the workspaceFolder requirement and adding in the environment variables.

funcHostTask was originally tracked by vscode.WorkspaceFolder via
Task.Scope and DebugConfiguration.WorkspaceFolder. This commit modifies
it to use a new AzureFunctionTaskDefinition.functionsApp property which
is either the `buildPath` from startFuncProcessFromApi or the
vscode.WorkspaceFolder.uri.fsPath to maintain backwards compatibility.

runningFuncTaskMap, funcTaskStartedEmitter, and runningFuncPortMap now
all using the vscode.Uri.fsPath string which can be used to map back to
a WorkspaceFolder. However in the cases the WorkspaceFolder fails, APIs
that used it will now see undefined passed in.

* Make unique 'type' so VS Code will launch multiple tasks

* Address Comments
  • Loading branch information
WardenGnaw authored Oct 31, 2024
1 parent 1cda7d9 commit e562e8b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 87 deletions.
141 changes: 79 additions & 62 deletions src/commands/pickFuncProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,54 @@ import * as vscode from 'vscode';
import { hostStartTaskName } from '../constants';
import { preDebugValidate, type IPreDebugValidateResult } from '../debug/validatePreDebug';
import { ext } from '../extensionVariables';
import { getFuncPortFromTaskOrProject, isFuncHostTask, runningFuncTaskMap, stopFuncTaskIfRunning, type IRunningFuncTask } from '../funcCoreTools/funcHostTask';
import { AzureFunctionTaskDefinition, getFuncPortFromTaskOrProject, isFuncHostTask, runningFuncTaskMap, stopFuncTaskIfRunning, type IRunningFuncTask } from '../funcCoreTools/funcHostTask';
import { localize } from '../localize';
import { delay } from '../utils/delay';
import { requestUtils } from '../utils/requestUtils';
import { taskUtils } from '../utils/taskUtils';
import { getWindowsProcessTree, ProcessDataFlag, type IProcessInfo, type IWindowsProcessTree } from '../utils/windowsProcessTree';
import { getWorkspaceSetting } from '../vsCodeConfig/settings';

const funcTaskReadyEmitter = new vscode.EventEmitter<vscode.WorkspaceFolder>();
const funcTaskReadyEmitter = new vscode.EventEmitter<string>();
export const onDotnetFuncTaskReady = funcTaskReadyEmitter.event;

export async function startFuncProcessFromApi(
workspaceFolder: vscode.WorkspaceFolder,
buildPath: string,
args?: string[]
args: string[],
env: { [key: string]: string }
): Promise<{ processId: string; success: boolean; error: string }> {
const result = {
processId: '',
success: false,
error: ''
};

const uriFile: vscode.Uri = vscode.Uri.file(buildPath)

const azFuncTaskDefinition: AzureFunctionTaskDefinition = {
// VS Code will only run a single instance of a task `type`,
// the path will be used here to make each project be unique.
type: `func ${uriFile.fsPath}`,
functionsApp: uriFile.fsPath
}

let funcHostStartCmd: string = 'func host start';
if (args) {
funcHostStartCmd += ` ${args.join(' ')}`;
}

await callWithTelemetryAndErrorHandling('azureFunctions.api.startFuncProcess', async (context: IActionContext) => {
try {
await waitForPrevFuncTaskToStop(workspaceFolder);
const funcTask = new vscode.Task({ type: 'func' },
workspaceFolder,
await waitForPrevFuncTaskToStop(azFuncTaskDefinition.functionsApp);
const funcTask = new vscode.Task(azFuncTaskDefinition,
vscode.TaskScope.Global,
hostStartTaskName, 'func',
new vscode.ShellExecution(funcHostStartCmd, {
cwd: buildPath,
env: env
}));

const taskInfo = await startFuncTask(context, workspaceFolder, funcTask);
const taskInfo = await startFuncTask(context, funcTask);
result.processId = await pickChildProcess(taskInfo);
result.success = true;
} catch (err) {
Expand All @@ -65,7 +75,7 @@ export async function pickFuncProcess(context: IActionContext, debugConfig: vsco
throw new UserCancelledError('preDebugValidate');
}

await waitForPrevFuncTaskToStop(result.workspace);
await waitForPrevFuncTaskToStop(result.workspace.uri.fsPath);

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const preLaunchTaskName: string | undefined = debugConfig.preLaunchTask;
Expand All @@ -78,25 +88,25 @@ export async function pickFuncProcess(context: IActionContext, debugConfig: vsco
throw new Error(localize('noFuncTask', 'Failed to find "{0}" task.', preLaunchTaskName || hostStartTaskName));
}

const taskInfo = await startFuncTask(context, result.workspace, funcTask);
const taskInfo = await startFuncTask(context, funcTask);
return await pickChildProcess(taskInfo);
}

async function waitForPrevFuncTaskToStop(workspaceFolder: vscode.WorkspaceFolder): Promise<void> {
stopFuncTaskIfRunning(workspaceFolder);
async function waitForPrevFuncTaskToStop(functionApp: string): Promise<void> {
stopFuncTaskIfRunning(functionApp);

const timeoutInSeconds: number = 30;
const maxTime: number = Date.now() + timeoutInSeconds * 1000;
while (Date.now() < maxTime) {
if (!runningFuncTaskMap.has(workspaceFolder)) {
if (!runningFuncTaskMap.has(functionApp)) {
return;
}
await delay(1000);
}
throw new Error(localize('failedToFindFuncHost', 'Failed to stop previous running Functions host within "{0}" seconds. Make sure the task has stopped before you debug again.', timeoutInSeconds));
}

async function startFuncTask(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, funcTask: vscode.Task): Promise<IRunningFuncTask> {
async function startFuncTask(context: IActionContext, funcTask: vscode.Task): Promise<IRunningFuncTask> {
const settingKey: string = 'pickProcessTimeout';
const settingValue: number | undefined = getWorkspaceSetting<number>(settingKey);
const timeoutInSeconds: number = Number(settingValue);
Expand All @@ -105,64 +115,71 @@ async function startFuncTask(context: IActionContext, workspaceFolder: vscode.Wo
}
context.telemetry.properties.timeoutInSeconds = timeoutInSeconds.toString();

let taskError: Error | undefined;
const errorListener: vscode.Disposable = vscode.tasks.onDidEndTaskProcess((e: vscode.TaskProcessEndEvent) => {
if (e.execution.task.scope === workspaceFolder && e.exitCode !== 0) {
context.errorHandling.suppressReportIssue = true;
// Throw if _any_ task fails, not just funcTask (since funcTask often depends on build/clean tasks)
taskError = new Error(localize('taskFailed', 'Error exists after running preLaunchTask "{0}". View task output for more information.', e.execution.task.name, e.exitCode));
errorListener.dispose();
}
});

try {
// The "IfNotActive" part helps when the user starts, stops and restarts debugging quickly in succession. We want to use the already-active task to avoid two func tasks causing a port conflict error
// The most common case we hit this is if the "clean" or "build" task is running when we get here. It's unlikely the "func host start" task is active, since we would've stopped it in `waitForPrevFuncTaskToStop` above
await taskUtils.executeIfNotActive(funcTask);

const intervalMs: number = 500;
const funcPort: string = await getFuncPortFromTaskOrProject(context, funcTask, workspaceFolder);
let statusRequestTimeout: number = intervalMs;
const maxTime: number = Date.now() + timeoutInSeconds * 1000;
while (Date.now() < maxTime) {
if (taskError !== undefined) {
throw taskError;
if (AzureFunctionTaskDefinition.is(funcTask.definition)) {
let taskError: Error | undefined;
const errorListener: vscode.Disposable = vscode.tasks.onDidEndTaskProcess((e: vscode.TaskProcessEndEvent) => {
if (AzureFunctionTaskDefinition.is(e.execution.task.definition) && e.execution.task.definition.functionsApp === funcTask.definition.functionsApp && e.exitCode !== 0) {
context.errorHandling.suppressReportIssue = true;
// Throw if _any_ task fails, not just funcTask (since funcTask often depends on build/clean tasks)
taskError = new Error(localize('taskFailed', 'Error exists after running preLaunchTask "{0}". View task output for more information.', e.execution.task.name, e.exitCode));
errorListener.dispose();
}
});

const taskInfo: IRunningFuncTask | undefined = runningFuncTaskMap.get(workspaceFolder);
if (taskInfo) {
for (const scheme of ['http', 'https']) {
const statusRequest: AzExtRequestPrepareOptions = { url: `${scheme}://localhost:${funcPort}/admin/host/status`, method: 'GET' };
if (scheme === 'https') {
statusRequest.rejectUnauthorized = false;
}
const workspaceFolder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(vscode.Uri.parse(funcTask.definition.functionsApp))

try {
// wait for status url to indicate functions host is running
const response = await sendRequestWithTimeout(context, statusRequest, statusRequestTimeout, undefined);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
if (response.parsedBody.state.toLowerCase() === 'running') {
funcTaskReadyEmitter.fire(workspaceFolder);
return taskInfo;
try {
// The "IfNotActive" part helps when the user starts, stops and restarts debugging quickly in succession. We want to use the already-active task to avoid two func tasks causing a port conflict error
// The most common case we hit this is if the "clean" or "build" task is running when we get here. It's unlikely the "func host start" task is active, since we would've stopped it in `waitForPrevFuncTaskToStop` above
await taskUtils.executeIfNotActive(funcTask);

const intervalMs: number = 500;
const funcPort: string = await getFuncPortFromTaskOrProject(context, funcTask, workspaceFolder);
let statusRequestTimeout: number = intervalMs;
const maxTime: number = Date.now() + timeoutInSeconds * 1000;
while (Date.now() < maxTime) {
if (taskError !== undefined) {
throw taskError;
}

const taskInfo: IRunningFuncTask | undefined = runningFuncTaskMap.get(funcTask.definition.functionsApp);
if (taskInfo) {
for (const scheme of ['http', 'https']) {
const statusRequest: AzExtRequestPrepareOptions = { url: `${scheme}://localhost:${funcPort}/admin/host/status`, method: 'GET' };
if (scheme === 'https') {
statusRequest.rejectUnauthorized = false;
}
} catch (error) {
if (requestUtils.isTimeoutError(error)) {
// Timeout likely means localhost isn't ready yet, but we'll increase the timeout each time it fails just in case it's a slow computer that can't handle a request that fast
statusRequestTimeout *= 2;
context.telemetry.measurements.maxStatusTimeout = statusRequestTimeout;
} else {
// ignore

try {
// wait for status url to indicate functions host is running
const response = await sendRequestWithTimeout(context, statusRequest, statusRequestTimeout, undefined);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
if (response.parsedBody.state.toLowerCase() === 'running') {
funcTaskReadyEmitter.fire(funcTask.definition.functionsApp);
return taskInfo;
}
} catch (error) {
if (requestUtils.isTimeoutError(error)) {
// Timeout likely means localhost isn't ready yet, but we'll increase the timeout each time it fails just in case it's a slow computer that can't handle a request that fast
statusRequestTimeout *= 2;
context.telemetry.measurements.maxStatusTimeout = statusRequestTimeout;
} else {
// ignore
}
}
}
}

await delay(intervalMs);
}

await delay(intervalMs);
throw new Error(localize('failedToFindFuncHost', 'Failed to detect running Functions host within "{0}" seconds. You may want to adjust the "{1}" setting.', timeoutInSeconds, `${ext.prefix}.${settingKey}`));
} finally {
errorListener.dispose();
}

throw new Error(localize('failedToFindFuncHost', 'Failed to detect running Functions host within "{0}" seconds. You may want to adjust the "{1}" setting.', timeoutInSeconds, `${ext.prefix}.${settingKey}`));
} finally {
errorListener.dispose();
}
else {
throw new Error(localize('failedToFindFuncTask', 'Failed to detect AzFunctions Task'));
}
}

Expand Down
Loading

0 comments on commit e562e8b

Please sign in to comment.