@@ -446,13 +446,13 @@ export class WorkspaceManagerBridge implements Disposable {
446
446
const installation = this . cluster . name ;
447
447
log . debug ( "Controlling instances..." , { installation } ) ;
448
448
449
- const runningInstances = await this . workspaceDB
449
+ const nonStoppedInstances = await this . workspaceDB
450
450
. trace ( ctx )
451
451
. findRunningInstancesWithWorkspaces ( installation , undefined , true ) ;
452
452
453
453
// Control running workspace instances against ws-manager
454
454
try {
455
- await this . controlRunningInstances ( ctx , runningInstances , clientProvider ) ;
455
+ await this . controlNonStoppedWSManagerManagedInstances ( ctx , nonStoppedInstances , clientProvider ) ;
456
456
457
457
disconnectStarted = Number . MAX_SAFE_INTEGER ; // Reset disconnect period
458
458
} catch ( err ) {
@@ -466,7 +466,7 @@ export class WorkspaceManagerBridge implements Disposable {
466
466
}
467
467
468
468
// Control workspace instances against timeouts
469
- await this . controlInstancesTimeouts ( ctx , runningInstances ) ;
469
+ await this . controlInstancesTimeouts ( ctx , nonStoppedInstances ) ;
470
470
471
471
log . debug ( "Done controlling instances." , { installation } ) ;
472
472
} catch ( err ) {
@@ -485,17 +485,17 @@ export class WorkspaceManagerBridge implements Disposable {
485
485
* This methods controls all instances that we have currently marked as "running" in the DB.
486
486
* It checks whether they are still running with their respective ws-manager, and if not, marks them as stopped in the DB.
487
487
*/
488
- protected async controlRunningInstances (
488
+ protected async controlNonStoppedWSManagerManagedInstances (
489
489
parentCtx : TraceContext ,
490
490
runningInstances : RunningWorkspaceInfo [ ] ,
491
491
clientProvider : ClientProvider ,
492
492
) {
493
493
const installation = this . config . installation ;
494
494
495
- const span = TraceContext . startSpan ( "controlRunningInstances " , parentCtx ) ;
495
+ const span = TraceContext . startSpan ( "controlNonStoppedWSManagerManagedInstances " , parentCtx ) ;
496
496
const ctx = { span } ;
497
497
try {
498
- log . debug ( "Controlling running instances..." , { installation } ) ;
498
+ log . debug ( "Controlling non-stopped instances that are managed by WS Manager ..." , { installation } ) ;
499
499
500
500
const runningInstancesIdx = new Map < string , RunningWorkspaceInfo > ( ) ;
501
501
runningInstances . forEach ( ( i ) => runningInstancesIdx . set ( i . latestInstance . id , i ) ) ;
@@ -504,9 +504,27 @@ export class WorkspaceManagerBridge implements Disposable {
504
504
const actuallyRunningInstances = await client . getWorkspaces ( ctx , new GetWorkspacesRequest ( ) ) ;
505
505
actuallyRunningInstances . getStatusList ( ) . forEach ( ( s ) => runningInstancesIdx . delete ( s . getId ( ) ) ) ;
506
506
507
+ // runningInstancesIdx only contains instances that ws-manager is not aware of
507
508
for ( const [ instanceId , ri ] of runningInstancesIdx . entries ( ) ) {
508
509
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
+ }
510
528
log . debug ( { instanceId } , "Skipping instance" , {
511
529
phase : instance . status . phase ,
512
530
creationTime : instance . creationTime ,
@@ -516,9 +534,8 @@ export class WorkspaceManagerBridge implements Disposable {
516
534
}
517
535
518
536
log . info (
519
- { instanceId, workspaceId : instance . workspaceId } ,
520
537
"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 } ,
522
539
) ;
523
540
await this . markWorkspaceInstanceAsStopped ( ctx , ri , new Date ( ) ) ;
524
541
}
@@ -603,6 +620,7 @@ export class WorkspaceManagerBridge implements Disposable {
603
620
const nowISO = now . toISOString ( ) ;
604
621
info . latestInstance . stoppingTime = nowISO ;
605
622
info . latestInstance . stoppedTime = nowISO ;
623
+ info . latestInstance . status . message = `Stopped by ws-manager-bridge. Previously in phase ${ info . latestInstance . status . phase } ` ;
606
624
info . latestInstance . status . phase = "stopped" ;
607
625
await this . workspaceDB . trace ( ctx ) . storeInstance ( info . latestInstance ) ;
608
626
0 commit comments