@@ -14,6 +14,7 @@ const { setGlobalDispatcher, Agent } = require("undici");
1414
1515const Server = require ( "./server" ) ;
1616const Utils = require ( "./utils" ) ;
17+ const { ConfigError } = require ( "./utils" ) ;
1718
1819const { getEnvVarsAsObj } = require ( "#server_functions" ) ;
1920// common timeout value, provide environment override in case
@@ -183,80 +184,92 @@ function App () {
183184 * @returns {Promise<object> } the config used
184185 */
185186 this . start = async function ( ) {
186- const configObj = Utils . loadConfig ( ) ;
187- global . config = configObj . fullConf ;
188- const config = global . config ;
189- Utils . checkConfigFile ( configObj ) ;
190-
191- global . defaultModulesDir = config . defaultModulesDir ;
192- defaultModules = require ( `${ global . root_path } /${ global . defaultModulesDir } /defaultmodules` ) ;
193-
194- Log . setLogLevel ( config . logLevel ) ;
195-
196- env = getEnvVarsAsObj ( ) ;
197- // check for deprecated css/custom.css and move it to new location
198- if ( ( ! fs . existsSync ( `${ global . root_path } /${ env . customCss } ` ) ) && ( fs . existsSync ( `${ global . root_path } /css/custom.css` ) ) ) {
199- try {
200- fs . renameSync ( `${ global . root_path } /css/custom.css` , `${ global . root_path } /${ env . customCss } ` ) ;
201- Log . warn ( `WARNING! Your custom css file was moved from ${ global . root_path } /css/custom.css to ${ global . root_path } /${ env . customCss } ` ) ;
202- } catch {
203- Log . warn ( "WARNING! Your custom css file is currently located in the css folder. Please move it to the config folder!" ) ;
187+ try {
188+ const configObj = Utils . loadConfig ( ) ;
189+ global . config = configObj . fullConf ;
190+ const config = global . config ;
191+ Utils . checkConfigFile ( configObj ) ;
192+
193+ global . defaultModulesDir = config . defaultModulesDir ;
194+ defaultModules = require ( `${ global . root_path } /${ global . defaultModulesDir } /defaultmodules` ) ;
195+
196+ Log . setLogLevel ( config . logLevel ) ;
197+
198+ env = getEnvVarsAsObj ( ) ;
199+ // check for deprecated css/custom.css and move it to new location
200+ if ( ( ! fs . existsSync ( `${ global . root_path } /${ env . customCss } ` ) ) && ( fs . existsSync ( `${ global . root_path } /css/custom.css` ) ) ) {
201+ try {
202+ fs . renameSync ( `${ global . root_path } /css/custom.css` , `${ global . root_path } /${ env . customCss } ` ) ;
203+ Log . warn ( `WARNING! Your custom css file was moved from ${ global . root_path } /css/custom.css to ${ global . root_path } /${ env . customCss } ` ) ;
204+ } catch {
205+ Log . warn ( "WARNING! Your custom css file is currently located in the css folder. Please move it to the config folder!" ) ;
206+ }
204207 }
205- }
206208
207- // get the used module positions
208- Utils . getModulePositions ( ) ;
209-
210- let modules = [ ] ;
211- for ( const module of config . modules ) {
212- if ( module . disabled ) continue ;
213- if ( module . module ) {
214- if ( Utils . moduleHasValidPosition ( module . position ) || typeof ( module . position ) === "undefined" ) {
215- // Only add this module to be loaded if it is not a duplicate (repeated instance of the same module)
216- if ( ! modules . includes ( module . module ) ) {
217- modules . push ( module . module ) ;
209+ // get the used module positions
210+ Utils . getModulePositions ( ) ;
211+
212+ let modules = [ ] ;
213+ for ( const module of config . modules ) {
214+ if ( module . disabled ) continue ;
215+ if ( module . module ) {
216+ if ( Utils . moduleHasValidPosition ( module . position ) || typeof ( module . position ) === "undefined" ) {
217+ // Only add this module to be loaded if it is not a duplicate (repeated instance of the same module)
218+ if ( ! modules . includes ( module . module ) ) {
219+ modules . push ( module . module ) ;
220+ }
221+ } else {
222+ Log . warn ( "Invalid module position found for this configuration:" + `\n${ JSON . stringify ( module , null , 2 ) } ` ) ;
218223 }
219224 } else {
220- Log . warn ( "Invalid module position found for this configuration:" + `\n${ JSON . stringify ( module , null , 2 ) } ` ) ;
225+ Log . warn ( "No module name found for this configuration:" + `\n${ JSON . stringify ( module , null , 2 ) } ` ) ;
221226 }
222- } else {
223- Log . warn ( "No module name found for this configuration:" + `\n${ JSON . stringify ( module , null , 2 ) } ` ) ;
224227 }
225- }
226228
227- setGlobalDispatcher ( new Agent ( { connect : { timeout : fetch_timeout } } ) ) ;
229+ setGlobalDispatcher ( new Agent ( { connect : { timeout : fetch_timeout } } ) ) ;
228230
229- await loadModules ( modules ) ;
231+ await loadModules ( modules ) ;
230232
231- httpServer = new Server ( configObj ) ;
232- const { app, io } = await httpServer . open ( ) ;
233- Log . log ( "Server started ..." ) ;
233+ httpServer = new Server ( configObj ) ;
234+ const { app, io } = await httpServer . open ( ) ;
235+ Log . log ( "Server started ..." ) ;
234236
235- const nodePromises = [ ] ;
236- for ( let nodeHelper of nodeHelpers ) {
237- nodeHelper . setExpressApp ( app ) ;
238- nodeHelper . setSocketIO ( io ) ;
237+ const nodePromises = [ ] ;
238+ for ( let nodeHelper of nodeHelpers ) {
239+ nodeHelper . setExpressApp ( app ) ;
240+ nodeHelper . setSocketIO ( io ) ;
239241
240- try {
241- nodePromises . push ( nodeHelper . start ( ) ) ;
242- } catch ( error ) {
243- Log . error ( `Error when starting node_helper for module ${ nodeHelper . name } :` ) ;
244- Log . error ( error ) ;
242+ try {
243+ nodePromises . push ( nodeHelper . start ( ) ) ;
244+ } catch ( error ) {
245+ Log . error ( `Error when starting node_helper for module ${ nodeHelper . name } :` ) ;
246+ Log . error ( error ) ;
247+ }
245248 }
246- }
247249
248- const results = await Promise . allSettled ( nodePromises ) ;
250+ const results = await Promise . allSettled ( nodePromises ) ;
249251
250- // Log errors that happened during async node_helper startup
251- results . forEach ( ( result ) => {
252- if ( result . status === "rejected" ) {
253- Log . error ( result . reason ) ;
254- }
255- } ) ;
252+ // Log errors that happened during async node_helper startup
253+ results . forEach ( ( result ) => {
254+ if ( result . status === "rejected" ) {
255+ Log . error ( result . reason ) ;
256+ }
257+ } ) ;
256258
257- Log . log ( "Sockets connected & modules started ..." ) ;
259+ Log . log ( "Sockets connected & modules started ..." ) ;
260+
261+ return global . config ;
262+ } catch ( err ) {
263+ // planned ConfigErrors already logged their message before throwing
264+ if ( ! ( err instanceof ConfigError ) ) {
265+ Log . error ( "Unexpected error during startup:" , err ) ;
266+ }
258267
259- return global . config ;
268+ const int32 = new Int32Array ( new SharedArrayBuffer ( 4 ) ) ;
269+ // wait 1000ms before exiting so that child processes (e.g. systeminformation) have some additional time
270+ Atomics . wait ( int32 , 0 , 0 , 1000 ) ;
271+ process . exit ( 1 ) ;
272+ }
260273 } ;
261274
262275 /**
@@ -328,20 +341,6 @@ function App () {
328341 await this . stop ( ) ;
329342 process . exit ( 0 ) ;
330343 } ) ;
331-
332- /**
333- *
334- * @param {number } ms milliseconds to wait
335- */
336- function blockingSleep ( ms ) {
337- const int32 = new Int32Array ( new SharedArrayBuffer ( 4 ) ) ;
338- Atomics . wait ( int32 , 0 , 0 , ms ) ;
339- }
340-
341- process . on ( "exit" , ( ) => {
342- // wait before exiting so that child processes (e.g. systeminformation) have some additional time
343- blockingSleep ( 1000 ) ;
344- } ) ;
345344}
346345
347346module . exports = new App ( ) ;
0 commit comments