-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathne-launcher.js
150 lines (141 loc) · 4.48 KB
/
ne-launcher.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* The `local.js` file may get updated by the api_sails process at startup.
* We wait for that to happen before starting up Notification Email.
* The `ne-launcher.js` name also helps to differentiate this process from
* all the other `node app.js` ones that may be running at the same time.
*
* 1. Check that /app/config/local.js exists
* 2. Wait for Docker to initialize `api_sails` domain
* 3. Wait until Sails is accessible
* 4. Launch notification email service
*/
const fs = require('fs');
const http = require('http');
const dns = require('dns');
const async = require('async3');
const SAILS_HOST = 'api_sails';
const SAILS_PORT = 1337;
const TIMEOUT_MS = 1000 * 60 * 2; // 2 minutes
/**
* Step 1: Check local.js file
*/
if (!fs.existsSync('/app/config/local.js')) {
console.error('Error: /app/config/local.js file not found.');
process.exit(1);
}
async.series(
[
(done) => {
// Wait 5 seconds
setTimeout(() => {
done();
}, 5000);
},
/**
* Step 2: Wait for Docker stack to initialize `api_sails` domain
*/
(done) => {
console.log('Looking for api_sails domain...');
var isDomainThere = false;
var startTime = Date.now();
async.doUntil(
// do iterations
(next) => {
dns.lookup(SAILS_HOST, 4, (err, address, family) => {
if (err) {
isDomainThere = false;
setTimeout(() => {
next();
}, 500)
}
else {
isDomainThere = true;
next();
}
});
},
// terminating condition
(until) => {
// Success
if (isDomainThere) {
until(null, true);
}
// Timed out
else if (Date.now() - startTime >= TIMEOUT_MS) {
console.error(`Timeout while waiting for domain`)
process.exit(1);
}
// Try again
else {
until(null, false);
}
},
// final
(err) => {
done();
}
);
},
/**
* Step 3: Wait until Sails is accessible
*/
(done) => {
var isSailsLifted = false;
var startTime = Date.now();
console.log('Waiting for Sails...');
async.doUntil(
// do iterations
(next) => {
http.get(
`http://${SAILS_HOST}:${SAILS_PORT}/robots.txt`,
{ timeout: 5 * 1000 },
(res) => {
isSailsLifted = true;
next();
}
)
.on('error', (err) => {
isSailsLifted = false;
setTimeout(() => {
next();
}, 500);
});
},
// terminating condition
(until) => {
// Success
if (isSailsLifted) {
until(null, true);
}
// Timed out
else if (Date.now() - startTime >= TIMEOUT_MS) {
console.error(`Timeout while waiting for Sails to lift`)
process.exit(1);
}
// Keep trying
else {
until(null, false);
}
},
// final
(err) => {
console.log('OK!');
done();
}
);
},
],
(err) => {
if (err) {
console.error(err);
process.exit(1);
}
else {
/**
* Step 4: Start Notification Email
*/
console.log('Starting Notification Email');
require(__dirname + '/app.js');
}
}
);