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

fix: add ability to stop containerless function #4025

Merged
merged 12 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"Other"
],
"dependencies": {
"@heroku/functions-core": "^0.2.7",
"@heroku/functions-core": "^0.3.0",
"@salesforce/core": "^2.35.0",
"@salesforce/salesforcedx-sobjects-faux-generator": "54.8.0",
"@salesforce/salesforcedx-utils-vscode": "54.8.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const CONTAINER_START_TEXT_KEY =
export const FUNCTION_CONTAINER_LOG_NAME = 'force_function_containerless_start';

/**
* Executes sfdx run:function:start:local --verbose
* Starts a local run of the function which can then be invoked with payloads.
* @param sourceUri
*/
export const forceFunctionContainerlessStartCommand = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { LocalRun } from '@heroku/functions-core';
import { LocalRun, LocalRunProcess } from '@heroku/functions-core';
import { Disposable } from 'vscode';
import { channelService } from '../../../channels';
import { nls } from '../../../messages';
Expand All @@ -19,14 +19,19 @@ import {
import { ForceFunctionStartExecutor } from './ForceFunctionStartExecutor';

export class ForceFunctionContainerlessStartExecutor extends ForceFunctionStartExecutor {
private process: LocalRunProcess | undefined;

public async setupFunctionListeners(): Promise<void> {
console.log('No listeners for containerless function.');
}

public async cancelFunction(
registeredStartedFunctionDisposable: Disposable
): Promise<void> {
// TODO: how to stop the localRun
if (this.process && !this.process.cancelled) {
this.process.cancel();
this.process = undefined;
}
registeredStartedFunctionDisposable.dispose();
}

Expand All @@ -51,10 +56,8 @@ export class ForceFunctionContainerlessStartExecutor extends ForceFunctionStartE

localRun
mohanraj-r marked this conversation as resolved.
Show resolved Hide resolved
.exec()
.then(msg => {
console.log(
`localRun resolved in ForceFunctionContainerlessStartExecutor with message: ${msg}`
);
.then(process => {
this.process = process;
})
.catch((err: Error) => {
const errorNotificationMessage = nls.localize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { LocalRunProcess } from '@heroku/functions-core';
import { vscodeStub } from '@salesforce/salesforcedx-utils-vscode/out/test/unit/commands/mocks';
import { expect } from 'chai';
import * as proxyquire from 'proxyquire';
Expand Down Expand Up @@ -216,19 +217,14 @@ describe('ForceFunctionContainerlessStartExecutor unit tests', () => {
});

it('Should be able to call methods that are no-ops for containerless mode.', async () => {
const disposable = { dispose: stub() };
const executor = new ForceFunctionContainerlessStartExecutor(
START_KEY,
LOG_NAME
);
const listenerResult = await executor.setupFunctionListeners(
'funDirPath',
disposable
'funDirPath'
);
expect(listenerResult).to.equal(undefined);
const cancelResult = await executor.cancelFunction(disposable);
expect(cancelResult).to.equal(undefined);
assert.calledOnce(disposable.dispose);
const buildResult = await executor.buildFunction('nameMe', 'funDirPath');
expect(buildResult).to.equal(undefined);
});
Expand Down Expand Up @@ -260,7 +256,32 @@ describe('ForceFunctionContainerlessStartExecutor unit tests', () => {
assert.calledOnce(fakeLocalRunInst.exec);
});

it('Should call telementry when localRun fails.', async () => {
it('Should be able to cancel running function.', async () => {
const fakeProcess = { cancel: stub().resolves() };
const fakeLocalRunInst = {
exec: stub().resolves(fakeProcess)
};
const disposable = {
dispose: stub()
};
const executor = new ForceFunctionContainerlessStartExecutor(
START_KEY,
LOG_NAME
);
localRunConstructorStub.returns(fakeLocalRunInst);

// Sets the local process
await executor.startFunction('foo', 'bar');

// have to wait for the unawaited promise in exec().then() to resolve
await Promise.resolve();

await executor.cancelFunction(disposable);
assert.calledOnce(fakeProcess.cancel);
assert.calledOnce(disposable.dispose);
});

it('Should call telemetry when localRun fails.', async () => {
const fakeType = 'typescript';
getFunctionTypeStub.returns(fakeType);
const errMessage = 'oh noes. FAIL';
Expand Down