Skip to content

Commit

Permalink
ask user to terminate or restart a task if it is actve
Browse files Browse the repository at this point in the history
- offer users the flexibility of terminating or restarting a task, if
the user tries to run a task that is actively running.
  • Loading branch information
Liang Huang committed Dec 1, 2019
1 parent da4154b commit 5969bed
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,40 @@ 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(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<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 +682,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 +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<void> {
await this.taskConfigurations.configure(task);
}
Expand Down

0 comments on commit 5969bed

Please sign in to comment.