Skip to content

Commit f2af999

Browse files
Laurie T. Malauroboquat
Laurie T. Malau
authored andcommitted
Stops stuck workspaces
1 parent 00e4328 commit f2af999

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

components/ws-manager-bridge/src/bridge.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,13 @@ export class WorkspaceManagerBridge implements Disposable {
446446
const installation = this.cluster.name;
447447
log.debug("Controlling instances...", { installation });
448448

449-
const runningInstances = await this.workspaceDB
449+
const nonStoppedInstances = await this.workspaceDB
450450
.trace(ctx)
451451
.findRunningInstancesWithWorkspaces(installation, undefined, true);
452452

453453
// Control running workspace instances against ws-manager
454454
try {
455-
await this.controlRunningInstances(ctx, runningInstances, clientProvider);
455+
await this.controlNonStoppedWSManagerManagedInstances(ctx, nonStoppedInstances, clientProvider);
456456

457457
disconnectStarted = Number.MAX_SAFE_INTEGER; // Reset disconnect period
458458
} catch (err) {
@@ -466,7 +466,7 @@ export class WorkspaceManagerBridge implements Disposable {
466466
}
467467

468468
// Control workspace instances against timeouts
469-
await this.controlInstancesTimeouts(ctx, runningInstances);
469+
await this.controlInstancesTimeouts(ctx, nonStoppedInstances);
470470

471471
log.debug("Done controlling instances.", { installation });
472472
} catch (err) {
@@ -485,17 +485,17 @@ export class WorkspaceManagerBridge implements Disposable {
485485
* This methods controls all instances that we have currently marked as "running" in the DB.
486486
* It checks whether they are still running with their respective ws-manager, and if not, marks them as stopped in the DB.
487487
*/
488-
protected async controlRunningInstances(
488+
protected async controlNonStoppedWSManagerManagedInstances(
489489
parentCtx: TraceContext,
490490
runningInstances: RunningWorkspaceInfo[],
491491
clientProvider: ClientProvider,
492492
) {
493493
const installation = this.config.installation;
494494

495-
const span = TraceContext.startSpan("controlRunningInstances", parentCtx);
495+
const span = TraceContext.startSpan("controlNonStoppedWSManagerManagedInstances", parentCtx);
496496
const ctx = { span };
497497
try {
498-
log.debug("Controlling running instances...", { installation });
498+
log.debug("Controlling non-stopped instances that are managed by WS Manager...", { installation });
499499

500500
const runningInstancesIdx = new Map<string, RunningWorkspaceInfo>();
501501
runningInstances.forEach((i) => runningInstancesIdx.set(i.latestInstance.id, i));
@@ -504,9 +504,27 @@ export class WorkspaceManagerBridge implements Disposable {
504504
const actuallyRunningInstances = await client.getWorkspaces(ctx, new GetWorkspacesRequest());
505505
actuallyRunningInstances.getStatusList().forEach((s) => runningInstancesIdx.delete(s.getId()));
506506

507+
// runningInstancesIdx only contains instances that ws-manager is not aware of
507508
for (const [instanceId, ri] of runningInstancesIdx.entries()) {
508509
const instance = ri.latestInstance;
509-
if (instance.status.phase !== "running") {
510+
const phase = instance.status.phase;
511+
if (phase !== "running") {
512+
// This below if block is to validate the planned fix
513+
if (
514+
phase === "pending" ||
515+
phase === "creating" ||
516+
phase === "initializing" ||
517+
(phase === "stopping" &&
518+
instance.stoppingTime &&
519+
durationLongerThanSeconds(Date.parse(instance.stoppingTime), 10))
520+
) {
521+
log.info("Logging to validate #12902. Should mark as stopped in database.", {
522+
instanceId,
523+
workspaceId: instance.workspaceId,
524+
installation,
525+
phase,
526+
});
527+
}
510528
log.debug({ instanceId }, "Skipping instance", {
511529
phase: instance.status.phase,
512530
creationTime: instance.creationTime,
@@ -516,9 +534,8 @@ export class WorkspaceManagerBridge implements Disposable {
516534
}
517535

518536
log.info(
519-
{ instanceId, workspaceId: instance.workspaceId },
520537
"Database says the instance is running, but wsman does not know about it. Marking as stopped in database.",
521-
{ installation },
538+
{ instanceId, workspaceId: instance.workspaceId, installation, phase },
522539
);
523540
await this.markWorkspaceInstanceAsStopped(ctx, ri, new Date());
524541
}
@@ -603,6 +620,7 @@ export class WorkspaceManagerBridge implements Disposable {
603620
const nowISO = now.toISOString();
604621
info.latestInstance.stoppingTime = nowISO;
605622
info.latestInstance.stoppedTime = nowISO;
623+
info.latestInstance.status.message = `Stopped by ws-manager-bridge. Previously in phase ${info.latestInstance.status.phase}`;
606624
info.latestInstance.status.phase = "stopped";
607625
await this.workspaceDB.trace(ctx).storeInstance(info.latestInstance);
608626

0 commit comments

Comments
 (0)