-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ide.service.ts
350 lines (304 loc) · 11 KB
/
ide.service.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (c) 2015-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';
import {CheAPI} from '../../components/api/che-api.factory';
import {CheWorkspace} from '../../components/api/che-workspace.factory';
import {RouteHistory} from '../../components/routing/route-history.service';
/**
* This class is handling the service for viewing the IDE
* @author Florent Benoit
*/
class IdeSvc {
$location: ng.ILocationService;
$log: ng.ILogService;
$mdDialog: ng.material.IDialogService;
$q: ng.IQService;
$rootScope: ng.IRootScopeService;
$sce: ng.ISCEService;
$timeout: ng.ITimeoutService;
$websocket: ng.websocket.IWebSocketProvider;
cheAPI: CheAPI;
cheWorkspace: CheWorkspace;
lodash: any;
proxySettings: any;
routeHistory: RouteHistory;
userDashboardConfig: any;
ideParams: Map<string, string>;
lastWorkspace: any;
openedWorkspace: any;
listeningChannels: string[];
websocketReconnect: number;
ideAction: string;
/**
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor($location: ng.ILocationService, $log: ng.ILogService, $mdDialog: ng.material.IDialogService,
$q: ng.IQService, $rootScope: ng.IRootScopeService, $sce: ng.ISCEService, $timeout: ng.ITimeoutService,
$websocket: ng.websocket.IWebSocketProvider, cheAPI: CheAPI, cheWorkspace: CheWorkspace, lodash: any,
proxySettings: any, routeHistory: RouteHistory, userDashboardConfig: any) {
this.$location = $location;
this.$log = $log;
this.$mdDialog = $mdDialog;
this.$q = $q;
this.$rootScope = $rootScope;
this.$sce = $sce;
this.$timeout = $timeout;
this.$websocket = $websocket;
this.cheAPI = cheAPI;
this.cheWorkspace = cheWorkspace;
this.lodash = lodash;
this.proxySettings = proxySettings;
this.routeHistory = routeHistory;
this.userDashboardConfig = userDashboardConfig;
this.ideParams = new Map();
this.lastWorkspace = null;
this.openedWorkspace = null;
this.listeningChannels = [];
}
displayIDE(): void {
(this.$rootScope as any).showIDE = true;
}
restoreIDE(): void {
(this.$rootScope as any).restoringIDE = true;
this.displayIDE();
}
hasIdeLink(): boolean {
return (this.$rootScope as any).ideIframeLink && ((this.$rootScope as any).ideIframeLink !== null);
}
handleError(error: any): void {
this.$log.error(error);
}
startIde(workspace: any): ng.IPromise<any> {
if (this.lastWorkspace) {
this.cleanupChannels(this.lastWorkspace.id);
}
this.lastWorkspace = workspace;
if (this.openedWorkspace && this.openedWorkspace.id === workspace.id) {
this.openedWorkspace = null;
}
this.updateRecentWorkspace(workspace.id);
let bus = this.cheAPI.getWebsocket().getBus();
let startWorkspaceDefer = this.$q.defer();
this.startWorkspace(bus, workspace).then(() => {
// update list of workspaces
// for new workspace to show in recent workspaces
this.cheAPI.cheWorkspace.fetchWorkspaces();
this.cheWorkspace.fetchStatusChange(workspace.id, 'RUNNING').then(() => {
return this.cheWorkspace.fetchWorkspaceDetails(workspace.id);
}).then(() => {
startWorkspaceDefer.resolve();
}, (error: any) => {
this.handleError(error);
startWorkspaceDefer.reject(error);
});
this.cheWorkspace.fetchStatusChange(workspace.id, 'ERROR').then((data: any) => {
startWorkspaceDefer.reject(data);
});
}, (error: any) => {
startWorkspaceDefer.reject(error);
});
return startWorkspaceDefer.promise.then(() => {
if (this.lastWorkspace && workspace.id === this.lastWorkspace.id) {
// now that the container is started, wait for the extension server. For this, needs to get runtime details
let websocketUrl = this.cheWorkspace.getWebsocketUrl(workspace.id);
// try to connect
this.websocketReconnect = 50;
this.connectToExtensionServer(websocketUrl, workspace.id);
} else {
this.cleanupChannels(workspace.id);
}
return this.$q.resolve();
}, (error: any) => {
if (this.lastWorkspace && workspace.id === this.lastWorkspace.id) {
this.cleanupChannels(workspace.id);
}
return this.$q.reject(error);
});
}
startWorkspace(bus: any, data: any): ng.IPromise<any> {
let startWorkspacePromise = this.cheAPI.getWorkspace().startWorkspace(data.id, data.config.defaultEnv);
startWorkspacePromise.then((data: any) => {
let statusLink = this.lodash.find(data.links, (link: any) => {
return link.rel === 'environment.status_channel';
});
let workspaceId = data.id;
let agentChannel = 'workspace:' + data.id + ':ext-server:output';
let statusChannel = statusLink ? statusLink.parameters[0].defaultValue : null;
this.listeningChannels.push(statusChannel);
// for now, display log of status channel in case of errors
bus.subscribe(statusChannel, (message: any) => {
if (message.status === 'DESTROYED' && message.workspaceId === data.id && !(this.$rootScope as any).showIDE) {
// need to show the error
this.$mdDialog.show(
this.$mdDialog.alert()
.title('Unable to start the workspace runtime')
.content('Your workspace runtime is no longer available. It was either destroyed or ran out of memory.')
.ariaLabel('Workspace start')
.ok('OK')
);
}
if (message.status === 'STOPPED' && message.error && message.workspaceId === data.id) {
let errorMessage = 'Error when trying to start the workspace';
if (message.error) {
errorMessage += ': ' + message.error;
} else {
errorMessage += '.';
}
// need to show the error
this.$mdDialog.show(
this.$mdDialog.alert()
.title('Error when starting workspace')
.content('Unable to start workspace. ' + errorMessage)
.ariaLabel('Workspace start')
.ok('OK')
);
}
this.$log.log('Status channel of workspaceID', workspaceId, message);
});
this.listeningChannels.push(agentChannel);
bus.subscribe(agentChannel, (message: any) => {
if (message.status === 'STOPPED' && message.error && message.workspaceId === data.id) {
// need to show the error
this.$mdDialog.show(
this.$mdDialog.alert()
.title('Error when starting agent')
.content('Unable to start workspace agent. Error when trying to start the workspace agent: ' + message.error)
.ariaLabel('Workspace agent start')
.ok('OK')
);
}
});
}, (error: any) => {
this.handleError(error);
this.$q.reject(error);
});
return startWorkspacePromise;
}
connectToExtensionServer(websocketURL: string, workspaceId: string): void {
// try to connect
let websocketStream = this.$websocket(websocketURL);
// on success, create project
websocketStream.onOpen(() => {
this.cleanupChannels(workspaceId, websocketStream);
});
// on error, retry to connect or after a delay, abort
websocketStream.onError((error: any) => {
this.websocketReconnect--;
if (this.websocketReconnect > 0) {
this.$timeout(() => {
this.connectToExtensionServer(websocketURL, workspaceId);
}, 1000);
} else {
this.cleanupChannels(workspaceId, websocketStream);
this.$log.error('error when starting remote extension', error);
// need to show the error
this.$mdDialog.show(
this.$mdDialog.alert()
.title('Unable to create project')
.content('Unable to connect to the remote extension server after workspace creation')
.ariaLabel('Project creation')
.ok('OK')
);
}
});
}
setLoadingParameter(paramName: string, paramValue: string): void {
this.ideParams.set(paramName, paramValue);
}
setIDEAction(ideAction: string): void {
this.ideAction = ideAction;
}
openIde(workspaceId: string): void {
(this.$rootScope as any).hideNavbar = false;
this.updateRecentWorkspace(workspaceId);
let inDevMode = this.userDashboardConfig.developmentMode;
let randVal = Math.floor((Math.random() * 1000000) + 1);
let appendUrl = '?uid=' + randVal;
let workspace = this.cheWorkspace.getWorkspaceById(workspaceId);
this.openedWorkspace = workspace;
let ideUrlLink = this.getHrefLink(workspace, 'ide url');
if (this.ideAction != null) {
appendUrl = appendUrl + '&action=' + this.ideAction;
// reset action
this.ideAction = null;
}
if (this.ideParams) {
for (let [key, val] of this.ideParams) {
appendUrl = appendUrl + '&' + key + '=' + val;
}
this.ideParams.clear();
}
if (inDevMode) {
(this.$rootScope as any).ideIframeLink = this.$sce.trustAsResourceUrl(ideUrlLink + appendUrl);
} else {
(this.$rootScope as any).ideIframeLink = ideUrlLink + appendUrl;
}
let defer = this.$q.defer();
if (workspace.status === 'RUNNING') {
defer.resolve();
} else {
this.cheWorkspace.fetchStatusChange(workspace.id, 'STARTING').then(() => {
defer.resolve();
}, (error: any) => {
defer.reject(error);
this.$log.error('Unable to start workspace: ', error);
});
}
defer.promise.then(() => {
// update list of recent workspaces
this.cheWorkspace.fetchWorkspaces();
});
}
/**
* Cleanup the websocket channels (unsubscribe)
*/
cleanupChannels(workspaceId: string, websocketStream?: any): void {
if (websocketStream != null) {
websocketStream.close();
}
let workspaceBus = this.cheAPI.getWebsocket().getBus();
if (workspaceBus != null) {
this.listeningChannels.forEach((channel: any) => {
workspaceBus.unsubscribe(channel);
});
this.listeningChannels.length = 0;
}
}
/**
* Gets link from a workspace
* @param workspace the workspace on which analyze the links
* @param name the name of the link to find (rel attribute)
* @returns empty or the href attribute of the link
*/
getHrefLink(workspace: any, name: string): string {
let links = workspace.links;
let i = 0;
while (i < links.length) {
let link = links[i];
if (link.rel === name) {
return link.href;
}
i++;
}
return '';
}
/**
* Emit event to move workspace immediately
* to top of the recent workspaces list
*
* @param workspaceId
*/
updateRecentWorkspace(workspaceId: string): void {
this.$rootScope.$broadcast('recent-workspace:set', workspaceId);
}
}
export default IdeSvc;