-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathServerOrchestrator.js
More file actions
334 lines (295 loc) · 9.9 KB
/
ServerOrchestrator.js
File metadata and controls
334 lines (295 loc) · 9.9 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const { spawn } = require('child_process');
const express = require('express');
// ESM-only modules - use .default for CommonJS compatibility
const getPortModule = require('get-port');
const getPort = getPortModule.default || getPortModule;
const fetchModule = require('node-fetch');
const fetch = fetchModule.default || fetchModule;
/**
* ServerOrchestrator
*
* Manages Parse Server and Parse Dashboard instances for browser testing.
* Handles starting servers on dynamic ports, health checks, and cleanup.
*
* Features:
* - Programmatically start Parse Server via child process
* - Start Parse Dashboard on available port
* - Health check utilities to wait for server readiness
* - Clean shutdown and resource cleanup
*/
class ServerOrchestrator {
constructor() {
this.parseServerProcess = null;
this.dashboardServer = null;
this.parseServerConfig = null;
this.dashboardConfig = null;
}
/**
* Start Parse Server programmatically
* @param {Object} options - Configuration options
* @param {number} options.port - Port for Parse Server (default: find available)
* @param {string} options.appId - Application ID (default: 'testAppId')
* @param {string} options.masterKey - Master key (default: 'testMasterKey')
* @param {string} options.databaseURI - Database URI (default: 'mongodb://localhost:27017/test')
* @param {string} options.mountPath - Mount path (default: '/parse')
* @returns {Promise<Object>} - { process, port, serverURL, appId, masterKey }
*/
async startParseServer(options = {}) {
// Find available port
const port = options.port || await getPort({ port: 1337 });
const appId = options.appId || 'testAppId';
const masterKey = options.masterKey || 'testMasterKey';
const databaseURI = options.databaseURI || 'mongodb://localhost:27017/test';
const mountPath = options.mountPath || '/parse';
const serverURL = `http://localhost:${port}${mountPath}`;
// Check if parse-server is available
try {
require.resolve('parse-server');
} catch {
throw new Error(
'parse-server is not installed. Please install it with: npm install parse-server --save-dev'
);
}
return new Promise((resolve, reject) => {
// Spawn parse-server process
const parseServer = spawn('npx', [
'parse-server',
'--appId', appId,
'--masterKey', masterKey,
'--databaseURI', databaseURI,
'--port', port.toString(),
'--mountPath', mountPath,
'--serverURL', serverURL
], {
stdio: 'pipe',
env: { ...process.env }
});
let serverStarted = false;
let startupOutput = '';
// Collect stdout for diagnostics (but don't use for startup detection)
parseServer.stdout.on('data', (data) => {
startupOutput += data.toString();
});
parseServer.stderr.on('data', (data) => {
console.error('[Parse Server Error]:', data.toString());
});
parseServer.on('error', (error) => {
if (!serverStarted) {
reject(new Error(`Failed to start Parse Server: ${error.message}`));
}
});
parseServer.on('exit', (code) => {
if (!serverStarted && code !== 0) {
reject(new Error(`Parse Server exited with code ${code}. Output: ${startupOutput}`));
}
});
// Build health check URL from configured mount path
// Normalize mount path: ensure single leading slash, no trailing slash
const normalizedMountPath = mountPath.startsWith('/') ? mountPath : `/${mountPath}`;
const healthPath = normalizedMountPath.endsWith('/')
? `${normalizedMountPath}health`
: `${normalizedMountPath}/health`;
const healthUrl = `http://localhost:${port}${healthPath}`;
// Poll health endpoint to detect when server is ready (more reliable than string matching)
this.waitForServer(healthUrl, 20000)
.then(() => {
serverStarted = true;
this.parseServerProcess = parseServer;
this.parseServerConfig = {
process: parseServer,
port,
serverURL,
appId,
masterKey,
databaseURI,
mountPath
};
resolve(this.parseServerConfig);
})
.catch(err => {
if (!serverStarted) {
parseServer.kill();
reject(new Error(`Parse Server health check failed: ${err.message}. Output: ${startupOutput}`));
}
});
});
}
/**
* Start Parse Dashboard
* @param {Object} parseServerConfig - Parse Server configuration from startParseServer()
* @param {Object} options - Dashboard options
* @param {number} options.port - Port for Dashboard (default: find available)
* @param {string} options.appName - App name (default: 'TestApp')
* @param {string} options.mount - Mount path (default: '/')
* @returns {Promise<Object>} - { server, port, url }
*/
async startDashboard(parseServerConfig, options = {}) {
if (!parseServerConfig) {
throw new Error('parseServerConfig is required');
}
// Find available port
const port = options.port || await getPort({ port: 4040 });
const appName = options.appName || 'TestApp';
const mount = options.mount || '/';
try {
// Import Parse Dashboard app
const ParseDashboard = require('../app');
// Dashboard settings
const dashboardSettings = {
apps: [{
serverURL: parseServerConfig.serverURL,
appId: parseServerConfig.appId,
masterKey: parseServerConfig.masterKey,
appName: appName
}],
trustProxy: 1
};
// Create Express app
const app = express();
// Mount Parse Dashboard
app.use(mount, ParseDashboard(dashboardSettings, { allowInsecureHTTP: true }));
// Start server
return new Promise((resolve, reject) => {
const server = app.listen(port, () => {
const url = `http://localhost:${port}${mount}`;
this.dashboardServer = server;
this.dashboardConfig = {
server,
port,
url,
mount,
appName,
settings: dashboardSettings
};
console.log(`Parse Dashboard started at ${url}`);
resolve(this.dashboardConfig);
});
// Handle errors via server's 'error' event (correct approach)
server.on('error', (err) => {
reject(new Error(`Failed to start Dashboard: ${err.message}`));
});
});
} catch (error) {
throw new Error(`Failed to start Dashboard: ${error.message}`);
}
}
/**
* Wait for a server to respond to health check
* @param {string} healthUrl - Health check URL
* @param {number} timeout - Timeout in milliseconds (default: 10000)
* @returns {Promise<void>}
* @private
*/
async waitForServer(healthUrl, timeout = 10000) {
const startTime = Date.now();
const interval = 500; // Check every 500ms
while (Date.now() - startTime < timeout) {
try {
const response = await fetch(healthUrl);
if (response.ok || response.status === 200 || response.status === 404) {
return; // Server is responding
}
} catch {
// Server not ready yet, continue waiting
}
// Wait before next attempt
await new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error(`Server health check timeout after ${timeout}ms`);
}
/**
* Stop Parse Server
* @returns {Promise<void>}
*/
async stopParseServer() {
if (!this.parseServerProcess) {
return;
}
// Capture reference to avoid race conditions
const proc = this.parseServerProcess;
// Check if process already exited
if (proc.exitCode !== null || proc.killed) {
this.parseServerProcess = null;
this.parseServerConfig = null;
console.log('Parse Server already stopped');
return;
}
return new Promise((resolve) => {
let forceKillTimeout = null;
// Attach exit handler BEFORE sending signals
proc.once('exit', () => {
// Clear the force kill timeout
if (forceKillTimeout) {
clearTimeout(forceKillTimeout);
}
// Null references exactly once
this.parseServerProcess = null;
this.parseServerConfig = null;
console.log('Parse Server stopped');
resolve();
});
// Try graceful shutdown first
proc.kill('SIGTERM');
// Force kill after 5 seconds if still running
forceKillTimeout = setTimeout(() => {
// Use captured reference, not this.parseServerProcess
if (!proc.killed && proc.exitCode === null) {
proc.kill('SIGKILL');
}
}, 5000);
});
}
/**
* Stop Parse Dashboard
* @returns {Promise<void>}
*/
async stopDashboard() {
if (!this.dashboardServer) {
return;
}
// Capture reference to avoid race conditions
const server = this.dashboardServer;
return new Promise((resolve) => {
server.close(() => {
this.dashboardServer = null;
this.dashboardConfig = null;
console.log('Parse Dashboard stopped');
resolve();
});
});
}
/**
* Stop all servers
* @returns {Promise<void>}
*/
async stopAll() {
await Promise.all([
this.stopDashboard(),
this.stopParseServer()
]);
}
/**
* Get current server status
* @returns {Object} - Status information
*/
getStatus() {
return {
parseServer: this.parseServerConfig ? {
running: true,
port: this.parseServerConfig.port,
serverURL: this.parseServerConfig.serverURL,
appId: this.parseServerConfig.appId
} : {
running: false
},
dashboard: this.dashboardConfig ? {
running: true,
port: this.dashboardConfig.port,
url: this.dashboardConfig.url
} : {
running: false
}
};
}
}
module.exports = ServerOrchestrator;