@@ -3,6 +3,7 @@ import dayjs from "dayjs"
3
3
import * as Types from "./types"
4
4
import { DeploymentConfig } from "./types"
5
5
import * as TypesGen from "./typesGenerated"
6
+ import { delay } from "utils/delay"
6
7
7
8
// Adds 304 for the default axios validateStatus function
8
9
// https://github.com/axios/axios#handling-errors Check status here
@@ -476,6 +477,35 @@ export const getWorkspaceByOwnerAndName = async (
476
477
return response . data
477
478
}
478
479
480
+ export function waitForBuild ( build : TypesGen . WorkspaceBuild ) {
481
+ return new Promise < TypesGen . ProvisionerJob | undefined > ( ( res , reject ) => {
482
+ void ( async ( ) => {
483
+ let latestJobInfo : TypesGen . ProvisionerJob | undefined = undefined
484
+
485
+ while (
486
+ ! [ "succeeded" , "canceled" ] . some ( ( status ) =>
487
+ latestJobInfo ?. status . includes ( status ) ,
488
+ )
489
+ ) {
490
+ const { job } = await getWorkspaceBuildByNumber (
491
+ build . workspace_owner_name ,
492
+ build . workspace_name ,
493
+ String ( build . build_number ) ,
494
+ )
495
+ latestJobInfo = job
496
+
497
+ if ( latestJobInfo . status === "failed" ) {
498
+ return reject ( latestJobInfo )
499
+ }
500
+
501
+ await delay ( 1000 )
502
+ }
503
+
504
+ return res ( latestJobInfo )
505
+ } ) ( )
506
+ } )
507
+ }
508
+
479
509
export const postWorkspaceBuild = async (
480
510
workspaceId : string ,
481
511
data : TypesGen . CreateWorkspaceBuildRequest ,
@@ -489,12 +519,12 @@ export const postWorkspaceBuild = async (
489
519
490
520
export const startWorkspace = (
491
521
workspaceId : string ,
492
- templateVersionID : string ,
522
+ templateVersionId : string ,
493
523
logLevel ?: TypesGen . CreateWorkspaceBuildRequest [ "log_level" ] ,
494
524
) =>
495
525
postWorkspaceBuild ( workspaceId , {
496
526
transition : "start" ,
497
- template_version_id : templateVersionID ,
527
+ template_version_id : templateVersionId ,
498
528
log_level : logLevel ,
499
529
} )
500
530
export const stopWorkspace = (
@@ -505,6 +535,7 @@ export const stopWorkspace = (
505
535
transition : "stop" ,
506
536
log_level : logLevel ,
507
537
} )
538
+
508
539
export const deleteWorkspace = (
509
540
workspaceId : string ,
510
541
logLevel ?: TypesGen . CreateWorkspaceBuildRequest [ "log_level" ] ,
@@ -523,6 +554,22 @@ export const cancelWorkspaceBuild = async (
523
554
return response . data
524
555
}
525
556
557
+ export const restartWorkspace = async ( workspace : TypesGen . Workspace ) => {
558
+ const stopBuild = await stopWorkspace ( workspace . id )
559
+ const awaitedStopBuild = await waitForBuild ( stopBuild )
560
+
561
+ // If the restart is canceled halfway through, make sure we bail
562
+ if ( awaitedStopBuild ?. status === "canceled" ) {
563
+ return
564
+ }
565
+
566
+ const startBuild = await startWorkspace (
567
+ workspace . id ,
568
+ workspace . latest_build . template_version_id ,
569
+ )
570
+ await waitForBuild ( startBuild )
571
+ }
572
+
526
573
export const cancelTemplateVersionBuild = async (
527
574
templateVersionId : TypesGen . TemplateVersion [ "id" ] ,
528
575
) : Promise < Types . Message > => {
0 commit comments