@@ -301,41 +301,52 @@ func stopWsF(t *testing.T, instanceID string, api *ComponentAPI) stopWorkspaceFu
301
301
break
302
302
}
303
303
304
- if ! waitForStop {
305
- wm , err := api .WorkspaceManager ()
306
- if err != nil {
307
- return nil , err
308
- }
304
+ wm , err := api .WorkspaceManager ()
305
+ if err != nil {
306
+ return nil , err
307
+ }
309
308
310
- dr , err := wm .DescribeWorkspace (sctx , & wsmanapi.DescribeWorkspaceRequest {
311
- Id : instanceID ,
312
- })
313
- if err != nil {
309
+ dr , err := wm .DescribeWorkspace (sctx , & wsmanapi.DescribeWorkspaceRequest {
310
+ Id : instanceID ,
311
+ })
312
+ if err != nil {
313
+ if s , ok := status .FromError (err ); ok && s .Code () == codes .NotFound {
314
+ t .Log ("the workspace is already gone" )
315
+ return & wsmanapi.WorkspaceStatus {
316
+ Id : instanceID ,
317
+ Phase : wsmanapi .WorkspacePhase_STOPPED ,
318
+ }, nil
319
+ } else if err != nil {
314
320
return nil , err
315
321
}
316
322
317
- s , ok := status .FromError (err )
318
- if ok && s .Code () == codes .NotFound {
319
- return nil , err
320
- }
321
- return dr .Status , err
323
+ }
324
+
325
+ if ! waitForStop {
326
+ return dr .Status , nil
322
327
}
323
328
324
329
var lastStatus * wsmanapi.WorkspaceStatus
325
330
for {
326
331
t .Logf ("waiting for stopping the workspace: %s" , instanceID )
327
332
lastStatus , err = WaitForWorkspaceStop (sctx , api , instanceID )
328
- if st , ok := status .FromError (err ); ok && st .Code () == codes .Unavailable {
329
- api .ClearWorkspaceManagerClientCache ()
330
- t .Logf ("got %v during waiting for stopping the workspace" , st )
331
- time .Sleep (5 * time .Second )
332
- continue
333
+ if st , ok := status .FromError (err ); ok {
334
+ switch st .Code () {
335
+ case codes .Unavailable :
336
+ api .ClearWorkspaceManagerClientCache ()
337
+ t .Logf ("got %v during waiting for stopping the workspace" , st )
338
+ time .Sleep (5 * time .Second )
339
+ continue
340
+ case codes .NotFound :
341
+ t .Log ("the workspace is already gone" )
342
+ return lastStatus , nil
343
+ }
333
344
} else if err != nil {
334
345
return lastStatus , err
335
346
}
336
347
break
337
348
}
338
- return lastStatus , err
349
+ return lastStatus , nil
339
350
}
340
351
}
341
352
@@ -414,11 +425,7 @@ func WaitForWorkspaceStart(ctx context.Context, instanceID string, api *Componen
414
425
}
415
426
416
427
s = resp .GetStatus ()
417
- if s == nil {
418
- time .Sleep (10 * time .Second )
419
- continue
420
- }
421
- if s .Id != instanceID {
428
+ if s == nil || s .Id != instanceID {
422
429
time .Sleep (10 * time .Second )
423
430
continue
424
431
}
@@ -434,13 +441,8 @@ func WaitForWorkspaceStart(ctx context.Context, instanceID string, api *Componen
434
441
if s .Conditions .Failed != "" {
435
442
errStatus <- xerrors .Errorf ("workspace instance %s failed: %s" , instanceID , s .Conditions .Failed )
436
443
return
437
- }
438
- if s .Phase == wsmanapi .WorkspacePhase_STOPPING {
439
- errStatus <- xerrors .Errorf ("workspace instance %s is stopping" , instanceID )
440
- return
441
- }
442
- if s .Phase == wsmanapi .WorkspacePhase_STOPPED {
443
- errStatus <- xerrors .Errorf ("workspace instance %s has stopped" , instanceID )
444
+ } else if s .Phase == wsmanapi .WorkspacePhase_STOPPING || s .Phase == wsmanapi .WorkspacePhase_STOPPED {
445
+ errStatus <- xerrors .Errorf ("workspace instance %s is %s" , instanceID , s .Phase )
444
446
return
445
447
}
446
448
}
@@ -490,6 +492,13 @@ func WaitForWorkspaceStop(ctx context.Context, api *ComponentAPI, instanceID str
490
492
return nil , xerrors .Errorf ("cannot listen for workspace updates: %q" , err )
491
493
}
492
494
495
+ _ , err = wsman .DescribeWorkspace (ctx , & wsmanapi.DescribeWorkspaceRequest {
496
+ Id : instanceID ,
497
+ })
498
+ if err != nil {
499
+ return nil , err
500
+ }
501
+
493
502
sub , err := wsman .Subscribe (ctx , & wsmanapi.SubscribeRequest {})
494
503
if err != nil {
495
504
return nil , xerrors .Errorf ("cannot listen for workspace updates: %q" , err )
@@ -500,55 +509,50 @@ func WaitForWorkspaceStop(ctx context.Context, api *ComponentAPI, instanceID str
500
509
501
510
done := make (chan * wsmanapi.WorkspaceStatus )
502
511
errCh := make (chan error )
512
+ resetSubscriber := func (subscriber wsmanapi.WorkspaceManager_SubscribeClient , sapi * ComponentAPI ) (wsmanapi.WorkspaceManager_SubscribeClient , error ) {
513
+ subscriber .CloseSend ()
514
+ sapi .ClearWorkspaceManagerClientCache ()
515
+ wsman , err := sapi .WorkspaceManager ()
516
+ if err != nil {
517
+ return nil , xerrors .Errorf ("cannot listen for workspace updates: %w" , err )
518
+ }
519
+ new_sub , err := wsman .Subscribe (ctx , & wsmanapi.SubscribeRequest {})
520
+ if err != nil {
521
+ return nil , xerrors .Errorf ("cannot listen for workspace updates: %w" , err )
522
+ }
523
+ return new_sub , nil
524
+ }
525
+
503
526
go func () {
504
- var s * wsmanapi.WorkspaceStatus
527
+ var wss * wsmanapi.WorkspaceStatus
505
528
defer func () {
506
- done <- s
529
+ done <- wss
507
530
close (done )
508
531
}()
509
532
for {
510
533
resp , err := sub .Recv ()
511
534
if err != nil {
512
- if st , ok := status .FromError (err ); ok && st .Code () == codes .Unavailable {
535
+ if s , ok := status .FromError (err ); ok && s .Code () == codes .Unavailable {
513
536
time .Sleep (10 * time .Second )
514
- sub .CloseSend ()
515
- api .ClearWorkspaceManagerClientCache ()
516
- wsman , err := api .WorkspaceManager ()
517
- if err != nil {
518
- errCh <- xerrors .Errorf ("cannot listen for workspace updates: %w" , err )
519
- return
537
+ sub , err = resetSubscriber (sub , api )
538
+ if err == nil {
539
+ continue
520
540
}
521
- sub , err = wsman .Subscribe (ctx , & wsmanapi.SubscribeRequest {})
522
- if err != nil {
523
- errCh <- xerrors .Errorf ("cannot listen for workspace updates: %w" , err )
524
- return
525
- }
526
- time .Sleep (10 * time .Second )
527
- continue
528
541
}
529
542
errCh <- xerrors .Errorf ("workspace update error: %q" , err )
530
543
return
531
544
}
532
- s = resp .GetStatus ()
533
- if s == nil {
534
- continue
535
- }
536
- if s .Id != instanceID {
537
- continue
538
- }
539
-
540
- if s .Conditions .Failed != "" {
541
- // TODO(toru): we have to fix https://github.com/gitpod-io/gitpod/issues/12021
542
- if s .Conditions .Failed == "The container could not be located when the pod was deleted. The container used to be Running" || s .Conditions .Failed == "The container could not be located when the pod was terminated" {
543
- lastStatus = s
545
+ if wss = resp .GetStatus (); wss != nil && wss .Id == instanceID {
546
+ if wss .Conditions .Failed != "" {
547
+ // TODO(toru): we have to fix https://github.com/gitpod-io/gitpod/issues/12021
548
+ if wss .Conditions .Failed != "The container could not be located when the pod was deleted. The container used to be Running" && wss .Conditions .Failed != "The container could not be located when the pod was terminated" {
549
+ errCh <- xerrors .Errorf ("workspace instance %s failed: %s" , instanceID , wss .Conditions .Failed )
550
+ }
551
+ return
552
+ }
553
+ if wss .Phase == wsmanapi .WorkspacePhase_STOPPED {
544
554
return
545
555
}
546
- errCh <- xerrors .Errorf ("workspace instance %s failed: %s" , instanceID , s .Conditions .Failed )
547
- return
548
- }
549
- if s .Phase == wsmanapi .WorkspacePhase_STOPPED {
550
- lastStatus = s
551
- return
552
556
}
553
557
554
558
time .Sleep (10 * time .Second )
0 commit comments