forked from vsuaste/server-graphql-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrateDbAndStartServer.js
55 lines (48 loc) · 1.42 KB
/
migrateDbAndStartServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const childProcess = require("child_process");
const path = require("path");
const dev = process.argv[2] === "dev";
runCheck(() => {
// Run migrations
childProcess.spawnSync('node', ['-e', 'require("./utils/migration").up()'], {stdio:"inherit"});
// Start GraphQL-server; uncomment the "acl" to turn on the acl rules
if (dev)
childProcess.spawnSync('npm', [
'run',
'dev'
// ,'acl'
], {stdio: "inherit", shell: process.platform === "win32"});
else
childProcess.spawnSync('npm', [
'start'
//,'acl'
], {stdio: "inherit", shell: process.platform === "win32"});
});
/**
* Periodically check database connection until every connection is established.
* Timeout after 60 tries. Execute callback after Success
*/
async function runCheck(callback) {
let waited = 0;
while (waited <= 60) {
if (waited === 60) {
console.error("\nERROR: Time out reached while waiting for database connections to be available.\n");
process.exit(1);
}
const exitCode = await checkConnections();
if(exitCode === 0)
break;
waited ++;
}
callback();
}
/**
* start child process to check database connections
*/
function checkConnections() {
return new Promise((resolve) => {
let child = childProcess.fork(path.normalize("./scripts/testDatabaseConnectionsAvailable.js"), {stdio: "ignore"});
child.on("exit", (code) => {
resolve(code)
});
});
}