From 4c5e5c674ad087bcb5e467a39fc96afb96b89c63 Mon Sep 17 00:00:00 2001 From: Liang Huang Date: Sat, 30 Nov 2019 19:48:57 -0500 Subject: [PATCH] ask user to terminate or restart a task if it is active With changes in this pull request, Theia offers users the flexibility of terminating or restarting a task, if the user tries to run a task that is actively running. This pull request resolves https://github.com/eclipse-theia/theia/issues/6618#issuecomment-558047443 Signed-off-by: Liang Huang --- packages/task/src/browser/task-service.ts | 40 ++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/task/src/browser/task-service.ts b/packages/task/src/browser/task-service.ts index f40189232de51..4dbf366f89396 100644 --- a/packages/task/src/browser/task-service.ts +++ b/packages/task/src/browser/task-service.ts @@ -409,6 +409,40 @@ export class TaskService implements TaskConfigurationClient { } async runTask(task: TaskConfiguration, option?: RunTaskOption): Promise { + const runningTasksInfo: TaskInfo[] = await this.getRunningTasks(); + + // check if the task is active + const matchedRunningTaskInfo = runningTasksInfo.find(taskInfo => { + const taskConfig = taskInfo.config; + return this.taskDefinitionRegistry.compareTasks(taskConfig, task); + }); + if (matchedRunningTaskInfo) { // the task is active + const taskName = this.taskNameResolver.resolve(task); + const taskId = matchedRunningTaskInfo.taskId; + const terminalId = matchedRunningTaskInfo.terminalId; + let terminal: TerminalWidget | undefined; + if (terminalId) { + terminal = this.terminalService.getById(this.getTerminalWidgetId(terminalId)); + if (terminal) { + terminal.show(); // bring the terminal to the top + } + } + this.messageService.info(`The task \'${taskName}\' is already active`, 'Terminate Task', 'Restart Task').then(actionTask => { + if (actionTask) { + this.taskServer.kill(taskId); + if (actionTask === 'Restart Task' && terminal) { + terminal.close(); + this.doRunTask(task, option); + } + } + }); + return; + } else { // run task as the task is not active + return this.doRunTask(task, option); + } + } + + private async doRunTask(task: TaskConfiguration, option?: RunTaskOption): Promise { if (option && option.customization) { const taskDefinition = this.taskDefinitionRegistry.getDefinition(task); if (taskDefinition) { // use the customization object to override the task config @@ -648,7 +682,7 @@ export class TaskService implements TaskConfigurationClient { TERMINAL_WIDGET_FACTORY_ID, { created: new Date().toString(), - id: 'terminal-' + processId, + id: this.getTerminalWidgetId(processId), title: taskInfo ? `Task: ${taskInfo.config.label}` : `Task: #${taskId}`, @@ -660,6 +694,10 @@ export class TaskService implements TaskConfigurationClient { widget.start(processId); } + private getTerminalWidgetId(terminalId: number): string { + return `${TERMINAL_WIDGET_FACTORY_ID}-${terminalId}`; + } + async configure(task: TaskConfiguration): Promise { await this.taskConfigurations.configure(task); }