33// This script polls to check whether the application has started at localhost:5000, and exits when it has started up, 
44// or exits with a failure if it hasn't started in 5 minutes. 
55
6+ const  programName  =  'await-application-startup' ; 
67const  pollUrl  =  'http://localhost:5000/projects' ; 
78const  pollInterval  =  1000 ; 
89const  timeout  =  5  *  60_000 ; 
910
11+ const  startTime  =  Date . now ( ) ; 
12+ let  lastError : string  |  null  =  null ; 
13+ 
14+ function  output ( message : string )  { 
15+   console . log ( `${ programName }  : ${ message }  ` ) ; 
16+ } 
17+ 
18+ function  outputWithoutNewline ( message : string )  { 
19+   Deno . stdout . writeSync ( new  TextEncoder ( ) . encode ( `${ programName }  : ${ message }  ` ) ) ; 
20+ } 
21+ 
1022setTimeout ( ( )  =>  { 
11-   console . log ( 'Failed to start in ' ,  timeout ,  ' milliseconds. Exiting.' ) ; 
23+   console . log ( ) ;  // New line after dots 
24+   if  ( lastError  !=  null )  { 
25+     output ( `Error: ${ lastError }  ` ) ; 
26+   } 
27+   output ( `Failed to start in ${ timeout }   milliseconds. Exiting.` ) ; 
1228  Deno . exit ( 1 ) ; 
1329} ,  timeout ) ; 
1430
15- const  startTime  =  Date . now ( ) ; 
1631function  elapsedTime ( )  { 
17-   const  currentTIme  =  Date . now ( ) ; 
18-   const  elapsed  =  currentTIme  -  startTime ; 
32+   const  currentTime  =  Date . now ( ) ; 
33+   const  elapsed  =  currentTime  -  startTime ; 
1934  const  minutes  =  Math . floor ( elapsed  /  60_000 ) ; 
2035  const  seconds  =  Math . floor ( ( elapsed  %  60_000 )  /  1000 ) ; 
21-   return  `${ minutes }  :${ seconds   <   10  ?  '0'   +   seconds  :  seconds }  ` ; 
36+   return  `${ minutes }  :${ seconds . toString ( ) . padStart ( 2 ,   '0' ) }  ` ; 
2237} 
2338
2439async  function  check ( )  { 
@@ -27,15 +42,20 @@ async function check() {
2742      headers : {  Accept : 'text/html'  } 
2843    } ) ; 
2944    if  ( response . ok )  { 
30-       console . log ( elapsedTime ( ) ,  'Startup check passed. Exiting.' ) ; 
45+       console . log ( ) ;  // New line after dots 
46+       output ( `${ elapsedTime ( ) }   Startup check passed. Exiting.` ) ; 
3147      Deno . exit ( 0 ) ; 
3248    }  else  { 
33-       console . log ( elapsedTime ( ) ,  'Startup check failed: ' ,  response . status ,  response . statusText ) ; 
49+       lastError  =  `${ response . status }   ${ response . statusText }  ` ; 
50+       Deno . stdout . writeSync ( new  TextEncoder ( ) . encode ( '.' ) ) ; 
3451    } 
3552  }  catch  ( error )  { 
36-     console . log ( elapsedTime ( ) ,  'Startup check failed: '  +  error . message ) ; 
53+     const  message  =  error  instanceof  Error  ? error . message  : String ( error ) ; 
54+     lastError  =  message ; 
55+     Deno . stdout . writeSync ( new  TextEncoder ( ) . encode ( '.' ) ) ; 
3756  } 
3857} 
3958
59+ outputWithoutNewline ( 'Awaiting application startup before running tests ...' ) ; 
4060setTimeout ( check ,  0 ) ; 
4161setInterval ( check ,  pollInterval ) ; 
0 commit comments