-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
task-server.ts
257 lines (221 loc) · 10.1 KB
/
task-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/********************************************************************************
* Copyright (C) 2017 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { inject, injectable, named } from 'inversify';
import { Disposable, DisposableCollection, ILogger } from '@theia/core/lib/common/';
import {
TaskClient,
TaskExitedEvent,
TaskInfo,
TaskServer,
TaskConfiguration,
TaskOutputProcessedEvent,
RunTaskOption,
BackgroundTaskEndedEvent
} from '../common';
import { TaskManager } from './task-manager';
import { TaskRunnerRegistry } from './task-runner';
import { Task } from './task';
import { ProcessTask } from './process/process-task';
import { ProblemCollector } from './task-problem-collector';
@injectable()
export class TaskServerImpl implements TaskServer, Disposable {
/** Task clients, to send notifications-to. */
protected clients: TaskClient[] = [];
/** Map of task id and task disposable */
protected readonly toDispose = new Map<number, DisposableCollection>();
/** Map of task id and task background status. */
// Currently there is only one property ('isActive'), but in the future we may want to store more properties
protected readonly backgroundTaskStatusMap = new Map<number, { 'isActive': boolean }>();
@inject(ILogger) @named('task')
protected readonly logger: ILogger;
@inject(TaskManager)
protected readonly taskManager: TaskManager;
@inject(TaskRunnerRegistry)
protected readonly runnerRegistry: TaskRunnerRegistry;
/** task context - {task id - problem collector} */
private problemCollectors: Map<string, Map<number, ProblemCollector>> = new Map();
dispose(): void {
for (const toDispose of this.toDispose.values()) {
toDispose.dispose();
}
this.toDispose.clear();
this.backgroundTaskStatusMap.clear();
}
protected disposeByTaskId(taskId: number): void {
if (this.toDispose.has(taskId)) {
this.toDispose.get(taskId)!.dispose();
this.toDispose.delete(taskId);
}
if (this.backgroundTaskStatusMap.has(taskId)) {
this.backgroundTaskStatusMap.delete(taskId);
}
}
async getTasks(context?: string): Promise<TaskInfo[]> {
const taskInfo: TaskInfo[] = [];
const tasks = this.taskManager.getTasks(context);
if (tasks !== undefined) {
for (const task of tasks) {
taskInfo.push(await task.getRuntimeInfo());
}
}
this.logger.debug(`getTasks(): about to return task information for ${taskInfo.length} tasks`);
return Promise.resolve(taskInfo);
}
async run(taskConfiguration: TaskConfiguration, ctx?: string, option?: RunTaskOption): Promise<TaskInfo> {
const runner = this.runnerRegistry.getRunner(taskConfiguration.type);
const task = await runner.run(taskConfiguration, ctx);
if (!this.toDispose.has(task.id)) {
this.toDispose.set(task.id, new DisposableCollection());
}
if (taskConfiguration.isBackground && !this.backgroundTaskStatusMap.has(task.id)) {
this.backgroundTaskStatusMap.set(task.id, { 'isActive': false });
}
this.toDispose.get(task.id)!.push(
task.onExit(event => {
this.taskManager.delete(task);
this.fireTaskExitedEvent(event, task);
this.removedCachedProblemCollector(event.ctx || '', event.taskId);
this.disposeByTaskId(event.taskId);
})
);
const resolvedMatchers = option && option.customization ? option.customization.problemMatcher || [] : [];
if (resolvedMatchers.length > 0) {
this.toDispose.get(task.id)!.push(
task.onOutput(event => {
let collector: ProblemCollector | undefined = this.getCachedProblemCollector(event.ctx || '', event.taskId);
if (!collector) {
collector = new ProblemCollector(resolvedMatchers);
this.cacheProblemCollector(event.ctx || '', event.taskId, collector);
}
const problems = collector.processLine(event.line);
if (problems.length > 0) {
this.fireTaskOutputProcessedEvent({
taskId: event.taskId,
config: taskConfiguration,
ctx: event.ctx,
problems
});
}
if (taskConfiguration.isBackground) {
const backgroundTaskStatus = this.backgroundTaskStatusMap.get(event.taskId)!;
if (!backgroundTaskStatus.isActive) {
// Get the 'activeOnStart' value of the problem matcher 'background' property
const activeOnStart = collector.isTaskActiveOnStart();
if (activeOnStart) {
backgroundTaskStatus.isActive = true;
} else {
const isBeginsPatternMatch = collector.matchBeginMatcher(event.line);
if (isBeginsPatternMatch) {
backgroundTaskStatus.isActive = true;
}
}
}
if (backgroundTaskStatus.isActive) {
const isEndsPatternMatch = collector.matchEndMatcher(event.line);
// Mark ends pattern as matches, only after begins pattern matches
if (isEndsPatternMatch) {
this.fireBackgroundTaskEndedEvent({
taskId: event.taskId,
ctx: event.ctx
});
}
}
}
})
);
}
this.toDispose.get(task.id)!.push(task);
const taskInfo = await task.getRuntimeInfo();
this.fireTaskCreatedEvent(taskInfo);
return taskInfo;
}
async getRegisteredTaskTypes(): Promise<string[]> {
return this.runnerRegistry.getRunnerTypes();
}
protected fireTaskExitedEvent(event: TaskExitedEvent, task?: Task): void {
this.logger.debug(log => log('task has exited:', event));
if (task instanceof ProcessTask) {
this.clients.forEach(client => {
client.onDidEndTaskProcess(event);
});
}
this.clients.forEach(client => {
client.onTaskExit(event);
});
}
protected fireTaskCreatedEvent(event: TaskInfo, task?: Task): void {
this.logger.debug(log => log('task created:', event));
this.clients.forEach(client => {
client.onTaskCreated(event);
});
if (task && task instanceof ProcessTask) {
this.clients.forEach(client => {
client.onDidStartTaskProcess(event);
});
}
}
protected fireTaskOutputProcessedEvent(event: TaskOutputProcessedEvent): void {
this.clients.forEach(client => client.onDidProcessTaskOutput(event));
}
protected fireBackgroundTaskEndedEvent(event: BackgroundTaskEndedEvent): void {
this.clients.forEach(client => client.onBackgroundTaskEnded(event));
}
/** Kill task for a given id. Rejects if task is not found */
async kill(id: number): Promise<void> {
const taskToKill = this.taskManager.get(id);
if (taskToKill !== undefined) {
this.logger.info(`Killing task id ${id}`);
return taskToKill.kill();
} else {
this.logger.info(`Could not find task to kill, task id ${id}. Already terminated?`);
return Promise.reject(new Error(`Could not find task to kill, task id ${id}. Already terminated?`));
}
}
/** Adds a client to this server */
setClient(client: TaskClient): void {
this.logger.debug('a client has connected - adding it to the list:');
this.clients.push(client);
}
/** Removes a client, from this server */
disconnectClient(client: TaskClient): void {
this.logger.debug('a client has disconnected - removed from list:');
const idx = this.clients.indexOf(client);
if (idx > -1) {
this.clients.splice(idx, 1);
}
}
private getCachedProblemCollector(ctx: string, taskId: number): ProblemCollector | undefined {
if (this.problemCollectors.has(ctx)) {
return this.problemCollectors.get(ctx)!.get(taskId);
}
}
private cacheProblemCollector(ctx: string, taskId: number, problemCollector: ProblemCollector): void {
if (this.problemCollectors.has(ctx)) {
if (!this.problemCollectors.get(ctx)!.has(taskId)) {
this.problemCollectors.get(ctx)!.set(taskId, problemCollector);
}
} else {
const forNewContext = new Map<number, ProblemCollector>();
forNewContext.set(taskId, problemCollector);
this.problemCollectors.set(ctx, forNewContext);
}
}
private removedCachedProblemCollector(ctx: string, taskId: number): void {
if (this.problemCollectors.has(ctx) && this.problemCollectors.get(ctx)!.has(taskId)) {
this.problemCollectors.get(ctx)!.delete(taskId);
}
}
}