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 stop command for Dapr runs #279

Merged
merged 2 commits into from
Feb 9, 2023
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
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
"category": "Dapr",
"icon": "$(debug-all)"
},
{
"command": "vscode-dapr.runs.stop",
"title": "%vscode-dapr.runs.stop.title%",
"category": "Dapr",
"icon": "$(stop)"
},
{
"command": "vscode-dapr.tasks.scaffoldDaprComponents",
"title": "%vscode-dapr.tasks.scaffoldDaprComponents.title%",
Expand Down Expand Up @@ -142,6 +148,10 @@
{
"command": "vscode-dapr.runs.debug",
"when": "never"
},
{
"command": "vscode-dapr.runs.stop",
"when": "never"
}
],
"view/item/context": [
Expand Down Expand Up @@ -174,6 +184,11 @@
"command": "vscode-dapr.runs.debug",
"when": "view == vscode-dapr.views.applications && viewItem =~ /run/ && viewItem =~ /attachable/",
"group": "inline"
},
{
"command": "vscode-dapr.runs.stop",
"when": "view == vscode-dapr.views.applications && viewItem =~ /run/ && viewItem =~ /stoppable/",
"group": "inline"
}
],
"view/title": [
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"vscode-dapr.help.reviewIssues.title": "Review Issues",

"vscode-dapr.runs.debug.title": "Debug Run",
"vscode-dapr.runs.stop.title": "Stop Run",

"vscode-dapr.tasks.scaffoldDaprComponents.title": "Scaffold Dapr Components",
"vscode-dapr.tasks.scaffoldDaprTasks.title": "Scaffold Dapr Tasks",
Expand Down
37 changes: 37 additions & 0 deletions src/commands/applications/stopRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as nls from 'vscode-nls';
import * as vscode from 'vscode';
import { IActionContext } from "@microsoft/vscode-azext-utils";
import { DaprRunNode } from "../../views/applications/daprRunNode";
import { getLocalizationPathForFile } from '../../util/localization';
import { DaprCliClient } from '../../services/daprCliClient';

const localize = nls.loadMessageBundle(getLocalizationPathForFile(__filename));

async function stopRun(context: IActionContext, label: string, runTemplatePath: string, daprCliClient: DaprCliClient): Promise<void> {
const stopItem: vscode.MessageItem = { title: localize('commands.applications.stopRun.stopItemTitle', 'Stop Run') };

const selectedItem = await context.ui.showWarningMessage(
localize('commands.applications.stopRun.confirmationMessage', 'Stop the Dapr run \'{0}\'?', label),
{
detail: localize('commands.applications.stopRun.detailMessage', 'All applications associated with the run will be stopped.'),
modal: true
},
stopItem);

if (selectedItem === stopItem) {
await daprCliClient.stopRun(runTemplatePath);
}
}

const createStopRunCommand = (daprCliClient: DaprCliClient) => (context: IActionContext, node: DaprRunNode | undefined): Promise<void> => {
if (node == undefined || node.runTemplatePath === undefined) {
throw new Error(localize('commands.applications.stopRun.noPaletteSupport', 'Stopping requires selecting a valid run in the Dapr view.'));
}

return stopRun(context, node.label, node.runTemplatePath, daprCliClient);
}

export default createStopRunCommand;
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import TreeNode from './views/treeNode';
import createDebugApplicationCommand from './commands/applications/debugApplication';
import createDebugRunCommand from './commands/applications/debugRun';
import { AsyncDisposable } from './util/asyncDisposable';
import createStopRunCommand from './commands/applications/stopRun';

interface ExtensionPackage {
engines: { [key: string]: string };
Expand Down Expand Up @@ -97,6 +98,7 @@ export function activate(context: vscode.ExtensionContext): Promise<void> {
telemetryProvider.registerContextCommandWithTelemetry('vscode-dapr.help.reportIssue', createReportIssueCommand(ui));
telemetryProvider.registerContextCommandWithTelemetry('vscode-dapr.help.reviewIssues', createReviewIssuesCommand(ui));
telemetryProvider.registerContextCommandWithTelemetry('vscode-dapr.runs.debug', createDebugRunCommand());
telemetryProvider.registerContextCommandWithTelemetry('vscode-dapr.runs.stop', createStopRunCommand(daprCliClient));
telemetryProvider.registerCommandWithTelemetry('vscode-dapr.tasks.scaffoldDaprComponents', createScaffoldDaprComponentsCommand(scaffolder, templateScaffolder));
telemetryProvider.registerCommandWithTelemetry('vscode-dapr.tasks.scaffoldDaprTasks', createScaffoldDaprTasksCommand(scaffolder, templateScaffolder, ui));
telemetryProvider.registerContextCommandWithTelemetry('vscode-dapr.tasks.openDaprDashboard', createOpenDaprDashboardCommand(daprDashboardProvider));
Expand Down
17 changes: 17 additions & 0 deletions src/services/daprCliClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface DaprCliClient {
startDashboard(): Promise<DaprDashboard>;
version(): Promise<DaprVersion>;
stopApp(application: DaprApplication | undefined): void;
stopRun(runTemplatePath: string): Promise<void>;
}

interface DaprCliVersion {
Expand Down Expand Up @@ -108,4 +109,20 @@ export default class LocalDaprCliClient implements DaprCliClient {
processId !== undefined ? process.kill(processId, 'SIGTERM') : null;
}
}

async stopRun(runTemplateFile: string): Promise<void> {
const daprPath = this.daprPathProvider();

const command =
CommandLineBuilder
.create(daprPath, 'stop')
.withNamedArg("--run-file", runTemplateFile)
.build();

const result = await Process.exec(command);

if (result.code !== 0) {
throw new Error(localize('services.daprCliClient.stopRunFailed', 'Stopping the run failed: {0}', result.stderr));
}
}
}
10 changes: 5 additions & 5 deletions src/views/applications/daprApplicationTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ export default class DaprApplicationTreeDataProvider extends vscode.Disposable i
}

private getRuns(): TreeNode[] {
const runs: { [key: string]: DaprApplication[] } = {};
const runs: { [key: string]: { applications: DaprApplication[], runTemplatePath: string } } = {};
const individualApps: DaprApplication[] = [];

for (const application of this.applications) {
if (application.runTemplatePath) {
// TODO: Grouping needs to be done via <PPID, RunTemplatePath> to allow for multiple runs.
const name = path.basename(path.dirname(application.runTemplatePath));

const applications = runs[name] ?? [];
const run = runs[name] ?? { applications: [], runTemplatePath: application.runTemplatePath };

applications.push(application);
run.applications.push(application);

runs[name] = applications;
runs[name] = run;
} else {
individualApps.push(application);
}
Expand All @@ -97,7 +97,7 @@ export default class DaprApplicationTreeDataProvider extends vscode.Disposable i
const runNames = Object.keys(runs);

if (runNames.length > 0) {
items.push(...runNames.map(name => DaprRunNode.CreateRunNode(name, runs[name], this.daprClient)))
items.push(...runNames.map(name => DaprRunNode.CreateRunNode(name, runs[name].runTemplatePath, runs[name].applications, this.daprClient)))

if (individualApps.length > 0) {
items.push(DaprRunNode.CreateIndividualApplicationsNode(individualApps, this.daprClient));
Expand Down
14 changes: 10 additions & 4 deletions src/views/applications/daprRunNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ const localize = nls.loadMessageBundle(getLocalizationPathForFile(__filename));
export class DaprRunNode implements TreeNode {
public static CreateRunNode(
label: string,
runTemplatePath: string,
applications: DaprApplication[],
daprClient: DaprClient): DaprRunNode {
return new DaprRunNode(label, applications, daprClient);
return new DaprRunNode(label, runTemplatePath, applications, daprClient);
}

public static CreateIndividualApplicationsNode(
applications: DaprApplication[],
daprClient: DaprClient) : DaprRunNode {
return new DaprRunNode(localize('views.applications.daprRunNode.individualApplicationsLabel', 'Individual Applications'), applications, daprClient, true);
return new DaprRunNode(localize('views.applications.daprRunNode.individualApplicationsLabel', 'Individual Applications'), undefined, applications, daprClient, true);
}

private constructor(
private readonly label: string,
public readonly label: string,
public readonly runTemplatePath: string | undefined,
public readonly applications: DaprApplication[],
private readonly daprClient: DaprClient,
private readonly isIndividualApplicationsNode: boolean = false) {
Expand All @@ -35,7 +37,11 @@ export class DaprRunNode implements TreeNode {
getTreeItem(): Promise<vscode.TreeItem> {
const item = new vscode.TreeItem(this.label, vscode.TreeItemCollapsibleState.Expanded);

item.contextValue = ['run', this.applications.some(application => application.appPid !== undefined) ? 'attachable' : ''].join(' ');
item.contextValue = [
'run',
this.applications.some(application => application.appPid !== undefined) ? 'attachable' : '',
this.runTemplatePath !== undefined ? 'stoppable' : ''
].join(' ');

if (!this.isIndividualApplicationsNode) {
item.iconPath = new vscode.ThemeIcon('layers');
Expand Down