@@ -8,13 +8,14 @@ import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_schemas.jso
88import { getPort } from "get-port-please" ;
99import pTimeout from "p-timeout" ;
1010
11- // Local imports
11+ // Local imports
1212import { commandExistsSync , waitForReady } from "./scripts.js" ;
1313import { microservicesMetadatasPath , projectMicroservices } from "./cleanup.js" ;
1414import { executablePath } from "./path.js" ;
1515
16- const DEFAULT_TIMEOUT_SECONDS = 60 ;
1716const MILLISECONDS_PER_SECOND = 1000 ;
17+ const DEFAULT_TIMEOUT_SECONDS = 30 ;
18+ const MAX_ERROR_BUFFER_BYTES = 64 * 1024 ;
1819
1920function getAvailablePort ( ) {
2021 return getPort ( {
@@ -23,40 +24,46 @@ function getAvailablePort() {
2324 } ) ;
2425}
2526
27+ function resolveCommand ( execPath , execName ) {
28+ const command = commandExistsSync ( execName )
29+ ? execName
30+ : executablePath ( execPath , execName ) ;
31+ return command ;
32+ }
33+
2634async function runScript (
2735 execPath ,
2836 execName ,
2937 args ,
3038 expectedResponse ,
3139 timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
3240) {
33- let command = "" ;
34- if ( commandExistsSync ( execName ) ) {
35- command = execName ;
36- } else {
37- command = path . join ( executablePath ( execPath , execName ) ) ;
38- }
41+ const command = resolveCommand ( execPath , execName ) ;
3942 console . log ( "runScript" , command , args ) ;
4043
41- const child = child_process . spawn ( process . platform === "win32" ? command : `"${ command } "` , args , {
42- encoding : "utf8" ,
43- shell : true ,
44+ const child = child_process . spawn ( command , args , {
45+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
4446 } ) ;
45- child . stdout . on ( "data" , ( data ) => console . log ( `[${ execName } ] ${ data . toString ( ) } ` ) ) ;
46- child . stderr . on ( "data" , ( data ) => console . log ( `[${ execName } ] ${ data . toString ( ) } ` ) ) ;
4747
48- child . on ( "close" , ( code ) => console . log ( `[${ execName } ] exited with code ${ code } ` ) ) ;
49- child . on ( "kill" , ( ) => {
50- console . log ( `[${ execName } ] process killed` ) ;
51- } ) ;
5248 child . name = command . replace ( / ^ .* [ \\ / ] / u, "" ) ;
5349
50+ child . on ( "spawn" , ( ) => {
51+ console . log ( `[${ child . name } ] spawned, pid=${ child . pid } ` ) ;
52+ } ) ;
53+
54+ const controller = new AbortController ( ) ;
55+ const timer = setTimeout (
56+ ( ) => controller . abort ( ) ,
57+ timeoutSeconds * MILLISECONDS_PER_SECOND ,
58+ ) ;
59+ if ( typeof timer . unref === "function" ) timer . unref ( ) ;
60+
5461 try {
55- return await pTimeout ( waitForReady ( child , expectedResponse ) , {
56- milliseconds : timeoutSeconds * MILLISECONDS_PER_SECOND ,
57- message : `Timed out after ${ timeoutSeconds } seconds` ,
58- } ) ;
62+ const result = await waitForReady ( child , expectedResponse , controller . signal ) ;
63+ clearTimeout ( timer ) ;
64+ return result ;
5965 } catch ( error ) {
66+ clearTimeout ( timer ) ;
6067 child . kill ( ) ;
6168 throw error ;
6269 }
@@ -73,11 +80,11 @@ async function runBack(execName, execPath, args = {}) {
7380 }
7481 const port = await getAvailablePort ( ) ;
7582 const backArgs = [
76- ` --port ${ port } ` ,
77- ` --data_folder_path ${ projectFolderPath } ` ,
78- ` --upload_folder_path ${ uploadFolderPath } ` ,
79- ` --allowed_origin http://localhost:*` ,
80- ` --timeout ${ 0 } ` ,
83+ " --port" , String ( port ) ,
84+ " --data_folder_path" , projectFolderPath ,
85+ " --upload_folder_path" , uploadFolderPath ,
86+ " --allowed_origin" , " http://localhost:*" ,
87+ " --timeout" , "0" ,
8188 ] ;
8289 if ( process . env . NODE_ENV === "development" || ! process . env . NODE_ENV ) {
8390 backArgs . push ( "--debug" ) ;
@@ -94,9 +101,9 @@ async function runViewer(execName, execPath, args = {}) {
94101 }
95102 const port = await getAvailablePort ( ) ;
96103 const viewerArgs = [
97- ` --port ${ port } ` ,
98- ` --data_folder_path ${ projectFolderPath } ` ,
99- ` --timeout ${ 0 } ` ,
104+ " --port" , String ( port ) ,
105+ " --data_folder_path" , projectFolderPath ,
106+ " --timeout" , "0" ,
100107 ] ;
101108 console . log ( "runViewer" , execPath , execName , viewerArgs ) ;
102109 await runScript ( execPath , execName , viewerArgs , "Starting factory" ) ;
0 commit comments