-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.ts
More file actions
39 lines (34 loc) · 1.44 KB
/
start.ts
File metadata and controls
39 lines (34 loc) · 1.44 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
import initBoiler from './initBoiler.ts';
import cluster from 'cluster';
import os from 'os';
import * as server from './server.ts';
import type PluginBridge from './lib/PluginBridge.ts';
import { initCacheMaster } from './lib/cache.ts';
/**
* Start the bridge with clustering.
* @param plugin - Plugin instance to use (optional for backward compat)
* @param configDir - Path to consumer's config/ directory (optional)
*/
export default async function startCluster (plugin?: PluginBridge, configDir?: string): Promise<void> {
const { getConfig, getLogger } = initBoiler(`bridge:${process.pid}`, configDir);
const numCPUs = os.cpus().length;
const logger = getLogger('start');
const config = await getConfig();
if (cluster.isPrimary) {
initCacheMaster();
logger.info(`Master process ${process.pid} is running`);
const configNumProcesses = config.get<number>('start:numProcesses') || numCPUs;
const numProcesses = configNumProcesses < 0 ? Math.max(numCPUs + configNumProcesses, 1) : configNumProcesses;
for (let i = 0; i < numProcesses; i++) {
cluster.fork();
}
cluster.on('exit', (worker) => {
logger.info(`Worker process ${worker.process.pid} died. Restarting...`);
cluster.fork();
});
} else {
await server.launch(plugin);
logger.info(`Api is exposed on: ${config.get('baseURL')}`);
}
}
// Legacy auto-run removed — consumers must call startCluster() explicitly from their own start.ts