[SQL-bindings] remove watcher for files (#20250)

* remove watcher for files

* nit
This commit is contained in:
Vasu Bhog
2022-08-05 16:18:03 -07:00
committed by GitHub
parent 9dee889808
commit 4ebe4c4547
4 changed files with 3 additions and 82 deletions

View File

@@ -25,11 +25,6 @@ export interface ILocalSettingsJson {
ConnectionStrings?: { [key: string]: string };
}
export interface IFileFunctionObject {
filePromise: Promise<string>;
watcherDisposable: vscode.Disposable;
}
/**
* copied and modified from vscode-azurefunctions extension
* https://github.com/microsoft/vscode-azurefunctions/blob/main/src/funcConfig/local.settings.ts
@@ -198,42 +193,6 @@ export async function getSettingsFile(projectFolder: string): Promise<string | u
return path.join(projectFolder, 'local.settings.json');
}
/**
* New azure function file watcher and watcher disposable to be used to watch for changes to the azure function project
* @param projectFolder is the parent directory to the project file
* @returns the function file path once created and the watcher disposable
*/
export function waitForNewFunctionFile(projectFolder: string): IFileFunctionObject {
const watcher = vscode.workspace.createFileSystemWatcher((
new vscode.RelativePattern(projectFolder, '**/*.cs')), false, true, true);
const filePromise = new Promise<string>((resolve, _) => {
watcher.onDidCreate((e) => {
resolve(e.fsPath);
});
});
return {
filePromise,
watcherDisposable: watcher
};
}
/**
* Retrieves the new host project file once it has created and the watcher disposable
* @returns the host file path once created and the watcher disposable
*/
export function waitForNewHostFile(): IFileFunctionObject {
const watcher = vscode.workspace.createFileSystemWatcher('**/host.json', false, true, true);
const filePromise = new Promise<string>((resolve, _) => {
watcher.onDidCreate((e) => {
resolve(e.fsPath);
});
});
return {
filePromise,
watcherDisposable: watcher
};
}
/**
* Adds the required nuget package to the project
* @param selectedProjectFile is the users selected project file path

View File

@@ -83,20 +83,6 @@ export function generateQuotedFullName(schema: string, objectName: string): stri
return `[${escapeClosingBrackets(schema)}].[${escapeClosingBrackets(objectName)}]`;
}
/**
* Returns a promise that will reject after the specified timeout
* @param errorMessage error message to be returned in the rejection
* @param ms timeout in milliseconds. Default is 10 seconds
* @returns a promise that rejects after the specified timeout
*/
export function timeoutPromise(errorMessage: string, ms: number = 10000): Promise<string> {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new TimeoutError(errorMessage));
}, ms);
});
}
/**
* Gets a unique file name
* Increment the file name by adding 1 to function name if the file already exists

View File

@@ -26,7 +26,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise<void> {
TelemetryReporter.sendActionEvent(TelemetryViews.CreateAzureFunctionWithSqlBinding, TelemetryActions.startCreateAzureFunctionWithSqlBinding);
let connectionInfo: IConnectionInfo | undefined;
let isCreateNewProject: boolean = false;
let newFunctionFileObject: azureFunctionsUtils.IFileFunctionObject | undefined;
try {
// check to see if Azure Functions Extension is installed
@@ -101,9 +100,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise<void> {
// user has an azure function project open
projectFolder = path.dirname(projectFile);
}
// create a system file watcher for the project folder
newFunctionFileObject = azureFunctionsUtils.waitForNewFunctionFile(projectFolder);
// Get connection string parameters and construct object name from prompt or connectionInfo given
let objectName: string | undefined;
let selectedBindingType: BindingType | undefined;
@@ -263,9 +259,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise<void> {
.withAdditionalProperties(propertyBag)
.withConnectionInfo(connectionInfo).send();
// check for the new function file to be created and dispose of the file system watcher
const timeoutForFunctionFile = utils.timeoutPromise(constants.timeoutAzureFunctionFileError);
await Promise.race([newFunctionFileObject.filePromise, timeoutForFunctionFile]);
telemetryStep = 'finishCreateFunction';
propertyBag.telemetryStep = telemetryStep;
exitReason = ExitReason.finishCreate;
@@ -275,15 +268,9 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise<void> {
} catch (error) {
let errorType = utils.getErrorType(error);
propertyBag.telemetryStep = telemetryStep;
if (errorType === 'TimeoutError') {
// this error can be cause by many different scenarios including timeout or error occurred during createFunction
exitReason = ExitReason.timeout;
console.log('Timed out waiting for Azure Function project to be created. This may not necessarily be an error, for example if the user canceled out of the create flow.');
} else {
// else an error would occur during the createFunction
exitReason = ExitReason.error;
void vscode.window.showErrorMessage(constants.errorNewAzureFunction(error));
}
// an error occurred during createFunction
exitReason = ExitReason.error;
void vscode.window.showErrorMessage(constants.errorNewAzureFunction(error));
TelemetryReporter.createErrorEvent(TelemetryViews.CreateAzureFunctionWithSqlBinding, TelemetryActions.exitCreateAzureFunctionQuickpick, undefined, errorType)
.withAdditionalProperties(propertyBag).send();
return;
@@ -292,7 +279,6 @@ export async function createAzureFunction(node?: ITreeNodeInfo): Promise<void> {
propertyBag.exitReason = exitReason;
TelemetryReporter.createActionEvent(TelemetryViews.CreateAzureFunctionWithSqlBinding, TelemetryActions.exitCreateAzureFunctionQuickpick)
.withAdditionalProperties(propertyBag).send();
newFunctionFileObject?.watcherDisposable.dispose();
}
}

View File

@@ -7,7 +7,6 @@ import * as fs from 'fs';
import * as path from 'path';
import * as should from 'should';
import * as sinon from 'sinon';
import * as TypeMoq from 'typemoq';
import * as vscode from 'vscode';
import * as azureFunctionUtils from '../../common/azureFunctionsUtils';
import * as constants from '../../common/constants';
@@ -90,9 +89,6 @@ describe('AzureFunctionsService', () => {
sinon.stub(azureFunctionUtils, 'setLocalAppSetting').withArgs(sinon.match.any, 'SqlConnectionString', 'testConnectionString').resolves((true));
sinon.stub(utils, 'executeCommand').resolves('downloaded nuget package');
const testWatcher = TypeMoq.Mock.ofType<vscode.FileSystemWatcher>().object;
sinon.stub(azureFunctionUtils, 'waitForNewFunctionFile').withArgs(sinon.match.any).returns({ filePromise: Promise.resolve('TestFileCreated'), watcherDisposable: testWatcher });
should(connectionInfo.database).equal('my_db', 'Initial ConnectionInfo database should be my_db');
await azureFunctionService.createAzureFunction(tableTestNode);
@@ -127,9 +123,6 @@ describe('AzureFunctionsService', () => {
sinon.stub(azureFunctionUtils, 'setLocalAppSetting').withArgs(sinon.match.any, 'SqlConnectionString', 'testConnectionString').resolves((true));
sinon.stub(utils, 'executeCommand').resolves('downloaded nuget package');
const testWatcher = TypeMoq.Mock.ofType<vscode.FileSystemWatcher>().object;
sinon.stub(azureFunctionUtils, 'waitForNewFunctionFile').withArgs(sinon.match.any).returns({ filePromise: Promise.resolve('TestFileCreated'), watcherDisposable: testWatcher });
showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage'); // error message spy to be used for checking tests
});
@@ -221,9 +214,6 @@ describe('AzureFunctionsService', () => {
sinon.stub(azureFunctionUtils, 'setLocalAppSetting').withArgs(sinon.match.any, 'SqlConnectionString', 'testConnectionString').resolves((true));
sinon.stub(utils, 'executeCommand').resolves('downloaded nuget package');
const testWatcher = TypeMoq.Mock.ofType<vscode.FileSystemWatcher>().object;
sinon.stub(azureFunctionUtils, 'waitForNewFunctionFile').withArgs(sinon.match.any).returns({ filePromise: Promise.resolve('TestFileCreated'), watcherDisposable: testWatcher });
showErrorMessageSpy = sinon.spy(vscode.window, 'showErrorMessage'); // error message spy to be used for checking tests
});