-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #348 from mStirner/dev
Split startup phases
- Loading branch information
Showing
5 changed files
with
289 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const path = require("path"); | ||
|
||
module.exports = (logger) => { | ||
return () => { | ||
return new Promise((resolve) => { | ||
|
||
logger.debug("Init components..."); | ||
|
||
const componentNames = [ | ||
"devices", | ||
"endpoints", | ||
"plugins", | ||
"rooms", | ||
"ssdp", | ||
"store", | ||
"users", | ||
"vault", | ||
"webhooks", | ||
"mqtt", | ||
"mdns", | ||
"scenes" | ||
].sort(() => { | ||
|
||
// pseudo randomize start/init of components | ||
// https://stackoverflow.com/a/18650169/5781499 | ||
return 0.5 - Math.random(); | ||
|
||
}); | ||
|
||
let componentConter = 0; | ||
//let counter = componentNames.length; | ||
|
||
|
||
// map over array | ||
// create from each promise | ||
// use Promise.all() ? | ||
// better/quicker start? | ||
componentNames.forEach((name) => { | ||
try { | ||
|
||
// this should be trace method | ||
logger.verbose(`Starting component "${name}"`); | ||
|
||
let component = require(path.resolve(process.cwd(), `components/${name}/index.js`)); | ||
|
||
component.events.on("ready", () => { | ||
|
||
componentConter += 1; | ||
|
||
logger.debug(`Component "${name}" ready to use. (${componentConter}/${componentNames.length})`); | ||
|
||
if (componentConter === componentNames.length) { | ||
logger.info(`All ${componentNames.length} Components ready`); | ||
resolve(); | ||
} | ||
|
||
}); | ||
|
||
// see issue #53, this should fire: | ||
// the procces should not exit with a "unhandled execption" | ||
// the try/catch block is for unhandled exception, not for startup errors | ||
component.events.on("error", (err) => { | ||
logger.error(err, `Component "${name}" error!`); | ||
process.exit(1); // fix #53 | ||
}); | ||
|
||
} catch (err) { | ||
|
||
console.error(err, "Component error"); | ||
process.exit(800); | ||
|
||
} | ||
}); | ||
|
||
}); | ||
}; | ||
}; |
Oops, something went wrong.