-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmanagerReady.ts
More file actions
286 lines (259 loc) · 11.3 KB
/
managerReady.ts
File metadata and controls
286 lines (259 loc) · 11.3 KB
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
import { Disposable, l10n, Uri } from 'vscode';
import { allExtensions, getExtension } from '../../common/extension.apis';
import { WorkbenchStrings } from '../../common/localize';
import { traceError, traceInfo, traceWarn } from '../../common/logging';
import { EventNames } from '../../common/telemetry/constants';
import { sendTelemetryEvent } from '../../common/telemetry/sender';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { showErrorMessage } from '../../common/window.apis';
import { installExtension } from '../../common/workbenchCommands';
import { getConfiguration } from '../../common/workspace.apis';
import { EnvironmentManagers, PythonProjectManager } from '../../internal.api';
import { getDefaultEnvManagerSetting, getDefaultPkgManagerSetting } from '../settings/settingHelpers';
const DEFAULT_MANAGER_READY_TIMEOUT_MS = 15_000;
function getManagerReadyTimeoutMs(): number {
const config = getConfiguration('python-envs');
const userValue = config.get<number>('experimental.managerReadyTimeout');
if (typeof userValue === 'number' && userValue >= 5 && userValue <= 120) {
return userValue * 1000;
}
return DEFAULT_MANAGER_READY_TIMEOUT_MS;
}
interface ManagerReady extends Disposable {
waitForEnvManager(uris?: Uri[]): Promise<void>;
waitForEnvManagerId(managerIds: string[]): Promise<void>;
waitForAllEnvManagers(): Promise<void>;
waitForPkgManager(uris?: Uri[]): Promise<void>;
waitForPkgManagerId(managerIds: string[]): Promise<void>;
}
function getExtensionId(managerId: string): string | undefined {
// format <extension-id>:<manager-name>
const regex = /^(.*):([a-zA-Z0-9-_]*)$/;
const parts = regex.exec(managerId);
return parts ? parts[1] : undefined;
}
class ManagerReadyImpl implements ManagerReady {
private readonly envManagers: Map<string, Deferred<void>> = new Map();
private readonly pkgManagers: Map<string, Deferred<void>> = new Map();
private readonly checked: Set<string> = new Set();
private readonly disposables: Disposable[] = [];
constructor(
em: EnvironmentManagers,
private readonly pm: PythonProjectManager,
) {
this.disposables.push(
em.onDidChangeEnvironmentManager((e) => {
if (this.envManagers.has(e.manager.id)) {
this.envManagers.get(e.manager.id)?.resolve();
} else {
const deferred = createDeferred<void>();
this.envManagers.set(e.manager.id, deferred);
deferred.resolve();
}
}),
em.onDidChangePackageManager((e) => {
if (this.pkgManagers.has(e.manager.id)) {
this.pkgManagers.get(e.manager.id)?.resolve();
} else {
const deferred = createDeferred<void>();
this.pkgManagers.set(e.manager.id, deferred);
deferred.resolve();
}
}),
);
}
private checkExtension(managerId: string) {
const installed = allExtensions().some((ext) => managerId.startsWith(`${ext.id}:`));
if (this.checked.has(managerId)) {
return;
}
this.checked.add(managerId);
const extId = getExtensionId(managerId);
if (extId) {
setImmediate(async () => {
if (installed) {
const ext = getExtension(extId);
if (ext && !ext.isActive) {
traceInfo(`Extension for manager ${managerId} is not active: Activating...`);
try {
await ext.activate();
traceInfo(`Extension for manager ${managerId} is now active.`);
} catch (err) {
traceError(`Failed to activate extension ${extId}, required for: ${managerId}`, err);
}
}
} else {
traceError(`Extension for manager ${managerId} is not installed.`);
const result = await showErrorMessage(
l10n.t(`Do you want to install extension {0} to enable {1} support.`, extId, managerId),
WorkbenchStrings.installExtension,
);
if (result === WorkbenchStrings.installExtension) {
traceInfo(`Installing extension: ${extId}`);
try {
await installExtension(extId);
traceInfo(`Extension ${extId} installed.`);
} catch (err) {
traceError(`Failed to install extension: ${extId}`, err);
}
try {
const ext = getExtension(extId);
if (ext && !ext.isActive) {
traceInfo(`Extension for manager ${managerId} is not active: Activating...`);
await ext.activate();
}
} catch (err) {
traceError(`Failed to activate extension ${extId}, required for: ${managerId}`, err);
}
}
}
});
} else {
showErrorMessage(l10n.t(`Extension for {0} is not installed or enabled for this workspace.`, managerId));
}
}
/**
* Wraps a deferred with a timeout so a missing/dead manager cannot block the API forever.
* On timeout the deferred is resolved (not rejected) so callers proceed with degraded results
* instead of hanging.
*/
private _withTimeout(deferred: Deferred<void>, managerId: string, kind: string): Promise<void> {
if (deferred.completed) {
return deferred.promise;
}
const timeoutMs = getManagerReadyTimeoutMs();
return new Promise<void>((resolve) => {
const timer = setTimeout(() => {
if (!deferred.completed) {
traceWarn(
`Timed out after ${timeoutMs / 1000}s waiting for ${kind} manager "${managerId}" to register. ` +
`The manager may not be installed or its extension failed to activate. Proceeding without it. ` +
`To prevent this, check your "python-envs.defaultEnvManager" and "python-envs.pythonProjects" settings. ` +
`If the manager is slow to start (e.g. on a remote or network filesystem), increase the timeout via ` +
`"python-envs.experimental.managerReadyTimeout" (current: ${timeoutMs / 1000}s, range: 5–120s).`,
);
sendTelemetryEvent(EventNames.MANAGER_READY_TIMEOUT, undefined, {
managerId,
managerKind: kind as 'environment' | 'package',
});
deferred.resolve();
}
}, timeoutMs);
deferred.promise.then(
() => {
clearTimeout(timer);
resolve();
},
() => {
clearTimeout(timer);
resolve();
},
);
});
}
public dispose(): void {
this.disposables.forEach((d) => d.dispose());
this.envManagers.clear();
this.pkgManagers.clear();
}
private _waitForEnvManager(managerId: string): Promise<void> {
if (this.envManagers.has(managerId)) {
return this.envManagers.get(managerId)!.promise;
}
const deferred = createDeferred<void>();
this.envManagers.set(managerId, deferred);
return this._withTimeout(deferred, managerId, 'environment');
}
public async waitForEnvManager(uris?: Uri[]): Promise<void> {
const ids: Set<string> = new Set();
if (uris) {
uris.forEach((uri) => {
const m = getDefaultEnvManagerSetting(this.pm, uri);
if (!ids.has(m)) {
ids.add(m);
}
});
} else {
const m = getDefaultEnvManagerSetting(this.pm, undefined);
if (m) {
ids.add(m);
}
}
await this.waitForEnvManagerId(Array.from(ids));
}
public async waitForEnvManagerId(managerIds: string[]): Promise<void> {
managerIds.forEach((managerId) => this.checkExtension(managerId));
await Promise.all(managerIds.map((managerId) => this._waitForEnvManager(managerId)));
}
public async waitForAllEnvManagers(): Promise<void> {
const ids: Set<string> = new Set();
this.pm.getProjects().forEach((project) => {
const m = getDefaultEnvManagerSetting(this.pm, project.uri);
if (m && !ids.has(m)) {
ids.add(m);
}
});
const m = getDefaultEnvManagerSetting(this.pm, undefined);
if (m) {
ids.add(m);
}
await this.waitForEnvManagerId(Array.from(ids));
}
private _waitForPkgManager(managerId: string): Promise<void> {
if (this.pkgManagers.has(managerId)) {
return this.pkgManagers.get(managerId)!.promise;
}
const deferred = createDeferred<void>();
this.pkgManagers.set(managerId, deferred);
return this._withTimeout(deferred, managerId, 'package');
}
public async waitForPkgManager(uris?: Uri[]): Promise<void> {
const ids: Set<string> = new Set();
if (uris) {
uris.forEach((uri) => {
const m = getDefaultPkgManagerSetting(this.pm, uri);
if (!ids.has(m)) {
ids.add(m);
}
});
} else {
const m = getDefaultPkgManagerSetting(this.pm, undefined);
if (m) {
ids.add(m);
}
}
await this.waitForPkgManagerId(Array.from(ids));
}
public async waitForPkgManagerId(managerIds: string[]): Promise<void> {
managerIds.forEach((managerId) => this.checkExtension(managerId));
await Promise.all(managerIds.map((managerId) => this._waitForPkgManager(managerId)));
}
}
let _deferred = createDeferred<ManagerReady>();
export function createManagerReady(em: EnvironmentManagers, pm: PythonProjectManager, disposables: Disposable[]) {
if (!_deferred.completed) {
const mr = new ManagerReadyImpl(em, pm);
disposables.push(mr);
_deferred.resolve(mr);
}
}
export async function waitForEnvManager(uris?: Uri[]): Promise<void> {
const mr = await _deferred.promise;
return mr.waitForEnvManager(uris);
}
export async function waitForEnvManagerId(managerIds: string[]): Promise<void> {
const mr = await _deferred.promise;
return mr.waitForEnvManagerId(managerIds);
}
export async function waitForAllEnvManagers(): Promise<void> {
const mr = await _deferred.promise;
return mr.waitForAllEnvManagers();
}
export async function waitForPkgManager(uris?: Uri[]): Promise<void> {
const mr = await _deferred.promise;
return mr.waitForPkgManager(uris);
}
export async function waitForPkgManagerId(managerIds: string[]): Promise<void> {
const mr = await _deferred.promise;
return mr.waitForPkgManagerId(managerIds);
}