Skip to content

Commit

Permalink
fix eclipse-theia#2961: a workaround to show output and title of shor…
Browse files Browse the repository at this point in the history
…t-lived tasks.

Signed-off-by: Cai Xuye <[email protected]>
  • Loading branch information
a1994846931931 committed May 9, 2020
1 parent 7cf845b commit 6c0258f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 44 deletions.
22 changes: 22 additions & 0 deletions packages/process/src/node/process-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ export class ProcessManager implements BackendApplicationContribution {

protected id: number = 0;
protected readonly processes: Map<number, Process>;
protected readonly willReapProcesses: Map<number, Process>;
protected readonly deleteEmitter: Emitter<number>;
protected readonly REAP_TIMEOUT: number = 1000;

constructor(
@inject(ILogger) @named('process') protected logger: ILogger
) {
this.processes = new Map();
this.willReapProcesses = new Map();
this.deleteEmitter = new Emitter<number>();
}

Expand Down Expand Up @@ -63,13 +66,32 @@ export class ProcessManager implements BackendApplicationContribution {
}
if (this.processes.delete(process.id)) {
this.deleteEmitter.fire(process.id);
// Process will be recorded in willReapProcesses for a while so that it's still possible to get information of it before it's automatically reaped
this.willReapProcesses.set(process.id, process);
setTimeout(() => {
this.willReapProcesses.delete(process.id);
}, this.REAP_TIMEOUT);
this.logger.debug(`The process was successfully unregistered. ${processLabel}`);
} else {
this.logger.warn(`This process was not registered or was already unregistered. ${processLabel}`);
}
}

/**
* Get a process given its id. The process could be either alive, or dead and will be reaped soon.
*
* @param id the process id.
*/
get(id: number): Process | undefined {
return this.processes.get(id) || this.willReapProcesses.get(id);
}

/**
* Get a process given its id. Only process marked alive will be returned.
*
* @param id the process id.
*/
getAlive(id: number): Process | undefined {
return this.processes.get(id);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/task/src/browser/quick-open-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
if (mode !== QuickOpenMode.OPEN) {
return false;
}
this.taskService.attach(task.terminalId!, task.taskId);
this.taskService.attach(task.terminalId!, task);
return true;
}
},
Expand Down
41 changes: 18 additions & 23 deletions packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ export class TaskService implements TaskConfigurationClient {
* Reason: Maybe a new task type wants to also be displayed in a terminal.
*/
if (typeof taskInfo.terminalId === 'number') {
this.attach(taskInfo.terminalId, taskInfo.taskId);
this.attach(taskInfo.terminalId, taskInfo);
}
return taskInfo;
} catch (error) {
Expand Down Expand Up @@ -997,24 +997,19 @@ export class TaskService implements TaskConfigurationClient {
terminal.sendText(selectedText);
}

async attach(terminalId: number, taskId: number): Promise<void> {
// Get the list of all available running tasks.
const runningTasks: TaskInfo[] = await this.getRunningTasks();
// Get the corresponding task information based on task id if available.
const taskInfo: TaskInfo | undefined = runningTasks.find((t: TaskInfo) => t.taskId === taskId);
async attach(terminalId: number, taskInfo: TaskInfo): Promise<void> {
let widgetOpenMode: WidgetOpenMode = 'open';
if (taskInfo) {
const terminalWidget = this.terminalService.getByTerminalId(terminalId);
if (terminalWidget) {
this.messageService.error('Task is already running in terminal');
return this.terminalService.open(terminalWidget, { mode: 'activate' });
}
if (TaskOutputPresentation.shouldAlwaysRevealTerminal(taskInfo.config)) {
if (TaskOutputPresentation.shouldSetFocusToTerminal(taskInfo.config)) { // assign focus to the terminal if presentation.focus is true
widgetOpenMode = 'activate';
} else { // show the terminal but not assign focus
widgetOpenMode = 'reveal';
}
const { taskId } = taskInfo;
const terminalWidget = this.terminalService.getByTerminalId(terminalId);
if (terminalWidget) {
this.messageService.error('Task is already running in terminal');
return this.terminalService.open(terminalWidget, { mode: 'activate' });
}
if (TaskOutputPresentation.shouldAlwaysRevealTerminal(taskInfo.config)) {
if (TaskOutputPresentation.shouldSetFocusToTerminal(taskInfo.config)) { // assign focus to the terminal if presentation.focus is true
widgetOpenMode = 'activate';
} else { // show the terminal but not assign focus
widgetOpenMode = 'reveal';
}
}
// Create / find a terminal widget to display an execution output of a task that was launched as a command inside a shell.
Expand All @@ -1026,11 +1021,11 @@ export class TaskService implements TaskConfigurationClient {
: `Task: #${taskId}`,
destroyTermOnClose: true
}, {
taskId,
widgetOptions: { area: 'bottom' },
mode: widgetOpenMode,
taskInfo
});
taskId,
widgetOptions: { area: 'bottom' },
mode: widgetOpenMode,
taskInfo
});
widget.start(terminalId);
}

Expand Down
40 changes: 20 additions & 20 deletions packages/terminal/src/node/base-terminal-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {
abstract create(options: IBaseTerminalServerOptions): Promise<number>;

async attach(id: number): Promise<number> {
const term = this.processManager.get(id);
const terminal = this.processManager.get(id);

if (term && term instanceof TerminalProcess) {
return term.id;
if (terminal && terminal instanceof TerminalProcess) {
return terminal.id;
} else {
this.logger.error(`Couldn't attach - can't find terminal with id: ${id} `);
return -1;
Expand All @@ -53,15 +53,15 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {

async getProcessId(id: number): Promise<number> {
const terminal = this.processManager.get(id);
if (!(terminal instanceof TerminalProcess)) {
if (!(terminal && terminal instanceof TerminalProcess)) {
throw new Error(`terminal "${id}" does not exist`);
}
return terminal.pid;
}

async getProcessInfo(id: number): Promise<TerminalProcessInfo> {
const terminal = this.processManager.get(id);
if (!(terminal instanceof TerminalProcess)) {
if (!(terminal && terminal instanceof TerminalProcess)) {
throw new Error(`terminal "${id}" does not exist`);
}
return {
Expand All @@ -71,18 +71,18 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {
}

async getCwdURI(id: number): Promise<string> {
const terminal = this.processManager.get(id);
if (!(terminal instanceof TerminalProcess)) {
const terminal = this.processManager.getAlive(id);
if (!(terminal && terminal instanceof TerminalProcess)) {
throw new Error(`terminal "${id}" does not exist`);
}
return terminal.getCwdURI();
}

async close(id: number): Promise<void> {
const term = this.processManager.get(id);
const terminal = this.processManager.getAlive(id);

if (term instanceof TerminalProcess) {
term.kill();
if (terminal && terminal instanceof TerminalProcess) {
terminal.kill();
}
}

Expand All @@ -95,9 +95,9 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {
}

async resize(id: number, cols: number, rows: number): Promise<void> {
const term = this.processManager.get(id);
if (term && term instanceof TerminalProcess) {
term.resize(cols, rows);
const terminal = this.processManager.getAlive(id);
if (terminal && terminal instanceof TerminalProcess) {
terminal.resize(cols, rows);
} else {
console.error("Couldn't resize terminal " + id + ", because it doesn't exist.");
}
Expand All @@ -108,31 +108,31 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {
this.client = client;
}

protected postCreate(term: TerminalProcess): void {
protected postCreate(terminal: TerminalProcess): void {
const toDispose = new DisposableCollection();

toDispose.push(term.onError(error => {
this.logger.error(`Terminal pid: ${term.pid} error: ${error}, closing it.`);
toDispose.push(terminal.onError(error => {
this.logger.error(`Terminal pid: ${terminal.pid} error: ${error}, closing it.`);

if (this.client !== undefined) {
this.client.onTerminalError({
'terminalId': term.id,
'terminalId': terminal.id,
'error': new Error(`Failed to execute terminal process (${error.code})`),
});
}
}));

toDispose.push(term.onExit(event => {
toDispose.push(terminal.onExit(event => {
if (this.client !== undefined) {
this.client.onTerminalExitChanged({
'terminalId': term.id,
'terminalId': terminal.id,
'code': event.code,
'signal': event.signal
});
}
}));

this.terminalToDispose.set(term.id, toDispose);
this.terminalToDispose.set(terminal.id, toDispose);
}

}

0 comments on commit 6c0258f

Please sign in to comment.