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 pollUrl = 'http://localhost:5000/projects' ;
6+ const programName = "await-application-startup" ;
7+ const pollUrl = "http://localhost:5000/projects" ;
78const pollInterval = 1000 ;
9+ const quietPeriodSec = 50_000 ;
810const timeout = 5 * 60_000 ;
911
12+ const startTime = Date . now ( ) ;
13+
14+ function output ( message : string ) {
15+ console . log ( programName + ": " + message ) ;
16+ }
17+
1018setTimeout ( ( ) => {
11- console . log ( ' Failed to start in ' , timeout , ' milliseconds. Exiting.' )
19+ output ( ` Failed to start in ${ timeout } milliseconds. Exiting.` ) ;
1220 Deno . exit ( 1 ) ;
1321} , timeout ) ;
1422
15- const startTime = Date . now ( ) ;
1623function elapsedTime ( ) {
17- const currentTIme = Date . now ( ) ;
18- const elapsed = currentTIme - startTime ;
24+ const currentTime = Date . now ( ) ;
25+ const elapsed = currentTime - startTime ;
1926 const minutes = Math . floor ( elapsed / 60_000 ) ;
20- const seconds = Math . floor ( ( elapsed % 60_000 ) / 1000 )
21- return `${ minutes } :${ seconds < 10 ? '0' + seconds : seconds } `
27+ const seconds = Math . floor ( ( elapsed % 60_000 ) / 1000 ) ;
28+ return `${ minutes } :${ seconds . toString ( ) . padStart ( 2 , "0" ) } ` ;
2229}
2330
2431async function check ( ) {
32+ const elapsedSec : number = Date . now ( ) - startTime ;
33+ const isQuietPeriod : boolean = elapsedSec < quietPeriodSec ;
34+
2535 try {
2636 const response = await fetch ( pollUrl , {
27- headers : { ' Accept' : ' text/html' }
37+ headers : { Accept : " text/html" }
2838 } ) ;
2939 if ( response . ok ) {
30- console . log ( elapsedTime ( ) , 'Startup check passed. Exiting.' )
40+ if ( isQuietPeriod ) {
41+ console . log ( ) ; // New line after dots
42+ }
43+ output ( `${ elapsedTime ( ) } Startup check passed. Exiting.` ) ;
3144 Deno . exit ( 0 ) ;
3245 } else {
33- console . log ( elapsedTime ( ) , 'Startup check failed: ' , response . status , response . statusText )
34- } } catch ( error ) {
35- console . log ( elapsedTime ( ) , 'Startup check failed: ' + error . message )
46+ if ( isQuietPeriod ) {
47+ Deno . stdout . writeSync ( new TextEncoder ( ) . encode ( "." ) ) ;
48+ } else {
49+ output ( `${ elapsedTime ( ) } Startup check failed: ${ response . status } ${ response . statusText } ` ) ;
50+ }
51+ }
52+ } catch ( error ) {
53+ if ( isQuietPeriod ) {
54+ Deno . stdout . writeSync ( new TextEncoder ( ) . encode ( "." ) ) ;
55+ return ;
56+ }
57+ const message = error instanceof Error ? error . message : String ( error ) ;
58+ output ( `${ elapsedTime ( ) } Startup check failed: ${ message } ` ) ;
3659 }
3760}
3861
3962setTimeout ( check , 0 ) ;
40- setInterval ( check , pollInterval ) ;
63+ setInterval ( check , pollInterval ) ;
0 commit comments