Skip to content

Commit

Permalink
ask user to terminate or restart a task if it is active
Browse files Browse the repository at this point in the history
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 #6618 (comment)

Signed-off-by: Liang Huang <liang.huang@ericsson.com>
  • Loading branch information
Liang Huang committed Dec 3, 2019
1 parent 5d24847 commit dc75955
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/

import { ApplicationShell, FrontendApplication, WidgetManager } from '@theia/core/lib/browser';
import { BOTTOM_AREA_ID } from '@theia/core/lib/browser/shell/theia-dock-panel';
import { open, OpenerService } from '@theia/core/lib/browser/opener-service';
import { ILogger, CommandService } from '@theia/core/lib/common';
import { MessageService } from '@theia/core/lib/common/message-service';
Expand Down Expand Up @@ -409,6 +410,46 @@ export class TaskService implements TaskConfigurationClient {
}

async runTask(task: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
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(async actionTask => {
if (actionTask) {
await this.kill(taskId);
if (terminal) {
const parent = terminal.parent;
terminal.close();
if (actionTask === 'Terminate Task' && parent && parent.id === BOTTOM_AREA_ID) {
parent.hide();
}
}
if (actionTask === 'Restart Task') {
this.doRunTask(task, option);
}
}
});
return;
} else { // run task as the task is not active
return this.doRunTask(task, option);
}
}

protected async doRunTask(task: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
if (option && option.customization) {
const taskDefinition = this.taskDefinitionRegistry.getDefinition(task);
if (taskDefinition) { // use the customization object to override the task config
Expand Down Expand Up @@ -648,7 +689,7 @@ export class TaskService implements TaskConfigurationClient {
TERMINAL_WIDGET_FACTORY_ID,
<TerminalWidgetFactoryOptions>{
created: new Date().toString(),
id: 'terminal-' + processId,
id: this.getTerminalWidgetId(processId),
title: taskInfo
? `Task: ${taskInfo.config.label}`
: `Task: #${taskId}`,
Expand All @@ -660,6 +701,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<void> {
await this.taskConfigurations.configure(task);
}
Expand Down

0 comments on commit dc75955

Please sign in to comment.