-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbridge.ts
688 lines (610 loc) · 30.1 KB
/
bridge.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/
import { inject, injectable, interfaces } from "inversify";
import { MessageBusIntegration } from "./messagebus-integration";
import {
Disposable,
WorkspaceInstance,
Queue,
WorkspaceInstancePort,
PortVisibility,
RunningWorkspaceInfo,
DisposableCollection,
} from "@gitpod/gitpod-protocol";
import {
WorkspaceStatus,
WorkspacePhase,
GetWorkspacesRequest,
WorkspaceConditionBool,
PortVisibility as WsManPortVisibility,
} from "@gitpod/ws-manager/lib";
import { WorkspaceDB } from "@gitpod/gitpod-db/lib/workspace-db";
import { UserDB } from "@gitpod/gitpod-db/lib/user-db";
import { log, LogContext } from "@gitpod/gitpod-protocol/lib/util/logging";
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
import { IAnalyticsWriter } from "@gitpod/gitpod-protocol/lib/analytics";
import { TracedWorkspaceDB, TracedUserDB, DBWithTracing } from "@gitpod/gitpod-db/lib/traced-db";
import { PrometheusMetricsExporter } from "./prometheus-metrics-exporter";
import { ClientProvider, WsmanSubscriber } from "./wsman-subscriber";
import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
import { Configuration } from "./config";
import { WorkspaceCluster } from "@gitpod/gitpod-protocol/lib/workspace-cluster";
import { repeat } from "@gitpod/gitpod-protocol/lib/util/repeat";
import { PreparingUpdateEmulator, PreparingUpdateEmulatorFactory } from "./preparing-update-emulator";
import { performance } from "perf_hooks";
import { PrebuildUpdater } from "./prebuild-updater";
export const WorkspaceManagerBridgeFactory = Symbol("WorkspaceManagerBridgeFactory");
function toBool(b: WorkspaceConditionBool | undefined): boolean | undefined {
if (b === WorkspaceConditionBool.EMPTY) {
return;
}
return b === WorkspaceConditionBool.TRUE;
}
export type WorkspaceClusterInfo = Pick<WorkspaceCluster, "name" | "url" | "govern">;
@injectable()
export class WorkspaceManagerBridge implements Disposable {
@inject(TracedWorkspaceDB)
protected readonly workspaceDB: DBWithTracing<WorkspaceDB>;
@inject(TracedUserDB)
protected readonly userDB: DBWithTracing<UserDB>;
@inject(MessageBusIntegration)
protected readonly messagebus: MessageBusIntegration;
@inject(PrometheusMetricsExporter)
protected readonly prometheusExporter: PrometheusMetricsExporter;
@inject(Configuration)
protected readonly config: Configuration;
@inject(PreparingUpdateEmulatorFactory)
protected readonly preparingUpdateEmulatorFactory: interfaces.Factory<PreparingUpdateEmulator>;
@inject(IAnalyticsWriter)
protected readonly analytics: IAnalyticsWriter;
@inject(PrebuildUpdater)
protected readonly prebuildUpdater: PrebuildUpdater;
protected readonly disposables = new DisposableCollection();
protected readonly queues = new Map<string, Queue>();
protected cluster: WorkspaceClusterInfo;
public start(cluster: WorkspaceClusterInfo, clientProvider: ClientProvider) {
const logPayload = { name: cluster.name, url: cluster.url, govern: cluster.govern };
log.info(`Starting bridge to cluster...`, logPayload);
this.cluster = cluster;
const startStatusUpdateHandler = (writeToDB: boolean) => {
log.debug(`Starting status update handler: ${cluster.name}`, logPayload);
/* no await */ this.startStatusUpdateHandler(clientProvider, writeToDB, logPayload)
// this is a mere safe-guard: we do not expect the code inside to fail
.catch((err) => log.error("Cannot start status update handler", err));
};
if (cluster.govern) {
// notify servers and _update the DB_
startStatusUpdateHandler(true);
// the actual "governing" part
const controllerIntervalSeconds = this.config.controllerIntervalSeconds;
if (controllerIntervalSeconds <= 0) {
throw new Error("controllerIntervalSeconds <= 0!");
}
log.debug(`Starting controller: ${cluster.name}`, logPayload);
// Control all workspace instances, either against ws-manager or configured timeouts
this.startController(clientProvider, controllerIntervalSeconds, this.config.controllerMaxDisconnectSeconds);
} else {
// _DO NOT_ update the DB (another bridge is responsible for that)
// Still, listen to all updates, generate/derive new state and distribute it locally!
startStatusUpdateHandler(false);
// emulate WorkspaceInstance updates for all Workspaces in the "preparing" or "building" phase in this cluster
const updateEmulator = this.preparingUpdateEmulatorFactory() as PreparingUpdateEmulator;
this.disposables.push(updateEmulator);
updateEmulator.start(cluster.name);
}
log.info(`Started bridge to cluster.`, logPayload);
}
public stop() {
this.dispose();
}
protected async startStatusUpdateHandler(
clientProvider: ClientProvider,
writeToDB: boolean,
logPayload: {},
): Promise<void> {
const subscriber = new WsmanSubscriber(clientProvider);
this.disposables.push(subscriber);
const onReconnect = (ctx: TraceContext, s: WorkspaceStatus[]) => {
s.forEach((sx) =>
this.serializeMessagesByInstanceId<WorkspaceStatus>(
ctx,
sx,
(m) => m.getId(),
(ctx, msg) => this.handleStatusUpdate(ctx, msg, writeToDB),
),
);
};
const onStatusUpdate = (ctx: TraceContext, s: WorkspaceStatus) => {
this.serializeMessagesByInstanceId<WorkspaceStatus>(
ctx,
s,
(msg) => msg.getId(),
(ctx, s) => this.handleStatusUpdate(ctx, s, writeToDB),
);
};
await subscriber.subscribe({ onReconnect, onStatusUpdate }, logPayload);
}
protected serializeMessagesByInstanceId<M>(
ctx: TraceContext,
msg: M,
getInstanceId: (msg: M) => string,
handler: (ctx: TraceContext, msg: M) => Promise<void>,
) {
const instanceId = getInstanceId(msg);
if (!instanceId) {
log.warn("Received invalid message, could not read instanceId!", { msg });
return;
}
// We can't just handle the status update directly, but have to "serialize" it to ensure the updates stay in order.
// If we did not do this, the async nature of our code would allow for one message to overtake the other.
let q = this.queues.get(instanceId) || new Queue();
q.enqueue(() => handler(ctx, msg)).catch((e) => log.error({ instanceId }, e));
this.queues.set(instanceId, q);
}
protected async handleStatusUpdate(ctx: TraceContext, rawStatus: WorkspaceStatus, writeToDB: boolean) {
const start = performance.now();
const status = rawStatus.toObject();
log.info("Handling WorkspaceStatus update", status);
if (!status.spec || !status.metadata || !status.conditions) {
log.warn("Received invalid status update", status);
return;
}
const logCtx = {
instanceId: status.id!,
workspaceId: status.metadata!.metaId!,
userId: status.metadata!.owner!,
};
try {
this.prometheusExporter.reportWorkspaceInstanceUpdateStarted(
writeToDB,
this.cluster.name,
status.spec.type,
);
await this.statusUpdate(ctx, rawStatus, writeToDB);
} catch (e) {
const durationMs = performance.now() - start;
this.prometheusExporter.reportWorkspaceInstanceUpdateCompleted(
durationMs / 1000,
writeToDB,
this.cluster.name,
status.spec.type,
e,
);
log.error(logCtx, "Failed to complete WorkspaceInstance status update", e);
throw e;
}
const durationMs = performance.now() - start;
this.prometheusExporter.reportWorkspaceInstanceUpdateCompleted(
durationMs / 1000,
writeToDB,
this.cluster.name,
status.spec.type,
);
log.info(logCtx, "Successfully completed WorkspaceInstance status update");
}
private async statusUpdate(ctx: TraceContext, rawStatus: WorkspaceStatus, writeToDB: boolean) {
const status = rawStatus.toObject();
if (!status.spec || !status.metadata || !status.conditions) {
return;
}
const span = TraceContext.startSpan("handleStatusUpdate", ctx);
span.setTag("status", JSON.stringify(filterStatus(status)));
span.setTag("writeToDB", writeToDB);
span.setTag("statusVersion", status.statusVersion);
try {
// Beware of the ID mapping here: What's a workspace to the ws-manager is a workspace instance to the rest of the system.
// The workspace ID of ws-manager is the workspace instance ID in the database.
// The meta ID of ws-manager is the workspace ID in the database.
const instanceId = status.id!;
const workspaceId = status.metadata!.metaId!;
const userId = status.metadata!.owner!;
const logContext: LogContext = {
userId,
instanceId,
workspaceId,
};
const instance = await this.workspaceDB.trace({ span }).findInstanceById(instanceId);
if (instance) {
this.prometheusExporter.statusUpdateReceived(this.cluster.name, true);
} else {
// This scenario happens when the update for a WorkspaceInstance is picked up by a ws-manager-bridge in a different region,
// before db-sync finished running. This is because all ws-manager-bridge instances receive updates from all WorkspaceClusters.
// We ignore this update because we do not have anything to reconcile this update against, but also because we assume it is handled
// by another instance of ws-manager-bridge that is in the region where the WorkspaceInstance record was created.
this.prometheusExporter.statusUpdateReceived(this.cluster.name, false);
return;
}
const currentStatusVersion = instance.status.version || 0;
if (currentStatusVersion > 0 && currentStatusVersion >= status.statusVersion) {
// We've gotten an event which is older than one we've already processed. We shouldn't process the stale one.
span.setTag("statusUpdate.staleEvent", true);
this.prometheusExporter.recordStaleStatusUpdate();
log.debug(ctx, "Stale status update received, skipping.");
}
if (!!status.spec.exposedPortsList) {
instance.status.exposedPorts = status.spec.exposedPortsList.map((p) => {
return <WorkspaceInstancePort>{
port: p.port,
visibility: mapPortVisibility(p.visibility),
url: p.url,
};
});
}
if (!instance.status.conditions.firstUserActivity && status.conditions.firstUserActivity) {
// Only report this when it's observed the first time
const firstUserActivity = mapFirstUserActivity(rawStatus.getConditions()!.getFirstUserActivity())!;
this.prometheusExporter.observeFirstUserActivity(instance, firstUserActivity);
}
instance.ideUrl = status.spec.url!;
instance.status.version = status.statusVersion;
instance.status.timeout = status.spec.timeout;
if (!!instance.status.conditions.failed && !status.conditions.failed) {
// We already have a "failed" condition, and received an empty one: This is a bug, "failed" conditions are terminal per definition.
// Do not override!
log.error(logContext, 'We received an empty "failed" condition overriding an existing one!', {
current: instance.status.conditions.failed,
});
// TODO(gpl) To make ensure we do not break anything big time we keep the unconditional override for now, and observe for some time.
instance.status.conditions.failed = status.conditions.failed;
} else {
instance.status.conditions.failed = status.conditions.failed;
}
instance.status.conditions.pullingImages = toBool(status.conditions.pullingImages!);
instance.status.conditions.deployed = toBool(status.conditions.deployed);
instance.status.conditions.timeout = status.conditions.timeout;
instance.status.conditions.firstUserActivity = mapFirstUserActivity(
rawStatus.getConditions()!.getFirstUserActivity(),
);
instance.status.conditions.headlessTaskFailed = status.conditions.headlessTaskFailed;
instance.status.conditions.stoppedByRequest = toBool(status.conditions.stoppedByRequest);
instance.status.message = status.message;
instance.status.nodeName = instance.status.nodeName || status.runtime?.nodeName;
instance.status.podName = instance.status.podName || status.runtime?.podName;
instance.status.nodeIp = instance.status.nodeIp || status.runtime?.nodeIp;
instance.status.ownerToken = status.auth!.ownerToken;
if (status.repo) {
const r = status.repo;
const undefinedIfEmpty = <T>(l: T[]) => (l.length > 0 ? l : undefined);
instance.status.repo = {
branch: r.branch,
latestCommit: r.latestCommit,
uncommitedFiles: undefinedIfEmpty(r.uncommitedFilesList),
totalUncommitedFiles: r.totalUncommitedFiles,
unpushedCommits: undefinedIfEmpty(r.unpushedCommitsList),
totalUntrackedFiles: r.totalUntrackedFiles,
untrackedFiles: undefinedIfEmpty(r.untrackedFilesList),
totalUnpushedCommits: r.totalUnpushedCommits,
};
}
let lifecycleHandler: (() => Promise<void>) | undefined;
switch (status.phase) {
case WorkspacePhase.PENDING:
instance.status.phase = "pending";
break;
case WorkspacePhase.CREATING:
instance.status.phase = "creating";
break;
case WorkspacePhase.INITIALIZING:
instance.status.phase = "initializing";
break;
case WorkspacePhase.RUNNING:
if (!instance.startedTime) {
instance.startedTime = new Date().toISOString();
this.prometheusExporter.observeWorkspaceStartupTime(instance);
this.analytics.track({
event: "workspace_running",
messageId: `bridge-wsrun-${instance.id}`,
properties: { instanceId: instance.id, workspaceId: workspaceId },
userId,
});
}
instance.status.phase = "running";
// let's check if the state is inconsistent and be loud if it is.
if (instance.stoppedTime || instance.stoppingTime) {
log.error("Resetting already stopped workspace to running.", {
instanceId: instance.id,
stoppedTime: instance.stoppedTime,
});
instance.stoppedTime = undefined;
instance.stoppingTime = undefined;
}
break;
case WorkspacePhase.INTERRUPTED:
instance.status.phase = "interrupted";
break;
case WorkspacePhase.STOPPING:
if (instance.status.phase != "stopped") {
instance.status.phase = "stopping";
if (!instance.stoppingTime) {
// The first time a workspace enters stopping we record that as it's stoppingTime time.
// This way we don't take the time a workspace requires to stop into account when
// computing the time a workspace instance was running.
instance.stoppingTime = new Date().toISOString();
}
} else {
log.warn(logContext, "Got a stopping event for an already stopped workspace.", instance);
}
break;
case WorkspacePhase.STOPPED:
const now = new Date().toISOString();
instance.stoppedTime = now;
instance.status.phase = "stopped";
if (!instance.stoppingTime) {
// It's possible we've never seen a stopping update, hence have not set the `stoppingTime`
// yet. Just for this case we need to set it now.
instance.stoppingTime = now;
}
lifecycleHandler = () => this.onInstanceStopped({ span }, userId, instance);
break;
}
span.setTag("after", JSON.stringify(instance));
// now notify all prebuild listeners about updates - and update DB if needed
await this.prebuildUpdater.updatePrebuiltWorkspace({ span }, userId, status, writeToDB);
// update volume snapshot information
if (
status.conditions.volumeSnapshot &&
status.conditions.volumeSnapshot.volumeSnapshotName != "" &&
writeToDB
) {
if (status.conditions.volumeSnapshot.volumeSnapshotName != instance.id) {
log.error(logContext, "volume snapshot name doesn't match workspace instance id", {
volumeSnapshotName: status.conditions.volumeSnapshot.volumeSnapshotName,
});
} else {
let existingSnapshot = await this.workspaceDB
.trace(ctx)
.findVolumeSnapshotById(status.conditions.volumeSnapshot.volumeSnapshotName);
if (existingSnapshot === undefined) {
await this.workspaceDB.trace(ctx).storeVolumeSnapshot({
id: status.conditions.volumeSnapshot.volumeSnapshotName,
workspaceId: workspaceId,
creationTime: new Date().toISOString(),
volumeHandle: status.conditions.volumeSnapshot.volumeSnapshotHandle,
});
}
}
}
if (writeToDB) {
await this.workspaceDB.trace(ctx).storeInstance(instance);
// cleanup
// important: call this after the DB update
if (!!lifecycleHandler) {
await lifecycleHandler();
}
}
await this.messagebus.notifyOnInstanceUpdate(ctx, userId, instance);
} catch (e) {
TraceContext.setError({ span }, e);
throw e;
} finally {
span.finish();
}
}
protected startController(
clientProvider: ClientProvider,
controllerIntervalSeconds: number,
controllerMaxDisconnectSeconds: number,
) {
let disconnectStarted = Number.MAX_SAFE_INTEGER;
this.disposables.push(
repeat(async () => {
const span = TraceContext.startSpan("controlInstances");
const ctx = { span };
try {
const installation = this.cluster.name;
log.debug("Controlling instances...", { installation });
const runningInstances = await this.workspaceDB
.trace(ctx)
.findRunningInstancesWithWorkspaces(installation, undefined, true);
// Control running workspace instances against ws-manager
try {
await this.controlRunningInstances(
ctx,
runningInstances,
clientProvider,
this.config.maxTimeToRunningPhaseSeconds,
);
disconnectStarted = Number.MAX_SAFE_INTEGER; // Reset disconnect period
} catch (err) {
if (durationLongerThanSeconds(disconnectStarted, controllerMaxDisconnectSeconds)) {
log.warn("Error while controlling installation's workspaces", err, {
installation: this.cluster.name,
});
} else if (disconnectStarted > Date.now()) {
disconnectStarted = Date.now();
}
}
// Control workspace instances against timeouts
await this.controlInstancesTimeouts(ctx, runningInstances);
log.debug("Done controlling instances.", { installation });
} catch (err) {
TraceContext.setError(ctx, err);
log.error("Error while controlling installation's workspaces", err, {
installation: this.cluster.name,
});
} finally {
span.finish();
}
}, controllerIntervalSeconds * 1000),
);
}
/**
* This methods controls all instances that we have currently marked as "running" in the DB.
* It checks whether they are still running with their respective ws-manager, and if not, marks them as stopped in the DB.
*/
protected async controlRunningInstances(
parentCtx: TraceContext,
runningInstances: RunningWorkspaceInfo[],
clientProvider: ClientProvider,
maxTimeToRunningPhaseSeconds: number,
) {
const installation = this.config.installation;
const span = TraceContext.startSpan("controlRunningInstances", parentCtx);
const ctx = { span };
try {
log.debug("Controlling running instances...", { installation });
const runningInstancesIdx = new Map<string, RunningWorkspaceInfo>();
runningInstances.forEach((i) => runningInstancesIdx.set(i.latestInstance.id, i));
const client = await clientProvider();
const actuallyRunningInstances = await client.getWorkspaces(ctx, new GetWorkspacesRequest());
actuallyRunningInstances.getStatusList().forEach((s) => runningInstancesIdx.delete(s.getId()));
for (const [instanceId, ri] of runningInstancesIdx.entries()) {
const instance = ri.latestInstance;
if (
!(
instance.status.phase === "running" ||
durationLongerThanSeconds(Date.parse(instance.creationTime), maxTimeToRunningPhaseSeconds)
)
) {
log.debug({ instanceId }, "Skipping instance", {
phase: instance.status.phase,
creationTime: instance.creationTime,
region: instance.region,
});
continue;
}
log.info(
{ instanceId, workspaceId: instance.workspaceId },
"Database says the instance is running, but wsman does not know about it. Marking as stopped in database.",
{ installation },
);
await this.markWorkspaceInstanceAsStopped(ctx, ri, new Date());
}
log.debug("Done controlling running instances.", { installation });
} catch (err) {
TraceContext.setError(ctx, err);
throw err; // required by caller
}
}
/**
* This methods controls all instances of this installation during periods where ws-manager does not control them, but we have them in our DB.
* These currently are:
* - preparing
* - building
* It also covers these phases, as fallback, when - for whatever reason - we no longer receive updates from ws-manager.
* - unknown (fallback)
*/
protected async controlInstancesTimeouts(parentCtx: TraceContext, runningInstances: RunningWorkspaceInfo[]) {
const installation = this.config.installation;
const span = TraceContext.startSpan("controlDBInstances", parentCtx);
const ctx = { span };
try {
log.debug("Controlling DB instances...", { installation });
await Promise.all(runningInstances.map((info) => this.controlInstanceTimeouts(ctx, info)));
log.debug("Done controlling DB instances.", { installation });
} catch (err) {
log.error("Error while running controlDBInstances", err, {
installation: this.cluster.name,
});
TraceContext.setError(ctx, err);
} finally {
span.finish();
}
}
protected async controlInstanceTimeouts(parentCtx: TraceContext, info: RunningWorkspaceInfo) {
const logContext: LogContext = {
userId: info.workspace.ownerId,
workspaceId: info.workspace.id,
instanceId: info.latestInstance.id,
};
const ctx = TraceContext.childContext("controlDBInstance", parentCtx);
try {
const now = Date.now();
const creationTime = new Date(info.latestInstance.creationTime).getTime();
const timedOutInPreparing = now >= creationTime + this.config.timeouts.preparingPhaseSeconds * 1000;
const timedOutInBuilding = now >= creationTime + this.config.timeouts.buildingPhaseSeconds * 1000;
const timedOutInUnknown = now >= creationTime + this.config.timeouts.unknownPhaseSeconds * 1000;
const currentPhase = info.latestInstance.status.phase;
log.debug(logContext, "Controller: Checking for instances in the DB to mark as stopped", {
creationTime,
timedOutInPreparing,
currentPhase,
});
if (
(currentPhase === "preparing" && timedOutInPreparing) ||
(currentPhase === "building" && timedOutInBuilding) ||
(currentPhase === "unknown" && timedOutInUnknown)
) {
log.info(logContext, "Controller: Marking workspace instance as stopped", {
creationTime,
currentPhase,
});
await this.markWorkspaceInstanceAsStopped(ctx, info, new Date(now));
}
} catch (err) {
log.warn(logContext, "Controller: Error while marking workspace instance as stopped", err);
TraceContext.setError(ctx, err);
} finally {
ctx.span.finish();
}
}
protected async markWorkspaceInstanceAsStopped(ctx: TraceContext, info: RunningWorkspaceInfo, now: Date) {
const nowISO = now.toISOString();
info.latestInstance.stoppingTime = nowISO;
info.latestInstance.stoppedTime = nowISO;
info.latestInstance.status.phase = "stopped";
await this.workspaceDB.trace(ctx).storeInstance(info.latestInstance);
await this.messagebus.notifyOnInstanceUpdate(ctx, info.workspace.ownerId, info.latestInstance);
await this.prebuildUpdater.stopPrebuildInstance(ctx, info.latestInstance);
}
protected async onInstanceStopped(
ctx: TraceContext,
ownerUserID: string,
instance: WorkspaceInstance,
): Promise<void> {
const span = TraceContext.startSpan("onInstanceStopped", ctx);
try {
await this.userDB.trace({ span }).deleteGitpodTokensNamedLike(ownerUserID, `${instance.id}-%`);
this.analytics.track({
userId: ownerUserID,
event: "workspace_stopped",
messageId: `bridge-wsstopped-${instance.id}`,
properties: { instanceId: instance.id, workspaceId: instance.workspaceId },
});
} catch (err) {
TraceContext.setError({ span }, err);
throw err;
} finally {
span.finish();
}
}
public dispose() {
this.disposables.dispose();
}
}
const mapFirstUserActivity = (firstUserActivity: Timestamp | undefined): string | undefined => {
if (!firstUserActivity) {
return undefined;
}
return firstUserActivity.toDate().toISOString();
};
const mapPortVisibility = (visibility: WsManPortVisibility | undefined): PortVisibility | undefined => {
switch (visibility) {
case undefined:
return undefined;
case WsManPortVisibility.PORT_VISIBILITY_PRIVATE:
return "private";
case WsManPortVisibility.PORT_VISIBILITY_PUBLIC:
return "public";
}
};
const durationLongerThanSeconds = (time: number, durationSeconds: number, now: number = Date.now()) => {
return (now - time) / 1000 > durationSeconds;
};
/**
* Filter here to avoid overloading spans
* @param status
*/
const filterStatus = (status: WorkspaceStatus.AsObject): Partial<WorkspaceStatus.AsObject> => {
return {
id: status.id,
metadata: status.metadata,
phase: status.phase,
message: status.message,
conditions: status.conditions,
runtime: status.runtime,
};
};