feat: rand port assign worker, check module required

This commit is contained in:
Rohit Rajan
2025-08-24 16:00:13 +05:30
parent 37e9f29d8f
commit 527b23bb92

View File

@@ -1,6 +1,7 @@
import express from 'express'; import express from 'express';
import path from 'path'; import path from 'path';
import http from 'http'; import http from 'http';
import { Server } from "socket.io";
import cors from 'cors'; import cors from 'cors';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
dotenv.config(); dotenv.config();
@@ -74,14 +75,13 @@ const server = http.createServer(app);
/** /**
* Globally exported singleton instance of socket.io for socket communication with the client. * Globally exported singleton instance of socket.io for socket communication with the client.
* @type {Server}
*/ */
// export const io = new Server(server); export let io: Server;
/** /**
* {@link BrowserPool} globally exported singleton instance for managing browsers. * {@link BrowserPool} globally exported singleton instance for managing browsers.
*/ */
// export const browserPool = new BrowserPool(); export const browserPool = new BrowserPool();
app.use(cookieParser()) app.use(cookieParser())
@@ -111,11 +111,11 @@ const recordingWorkerPath = path.resolve(__dirname, isProduction ? './pgboss-wor
let workerProcess: any; let workerProcess: any;
let recordingWorkerProcess: any; let recordingWorkerProcess: any;
if (!isProduction) { if (!isProduction && require.main === module) {
workerProcess = fork(workerPath, [], { workerProcess = fork(workerPath, [], {
execArgv: [ execArgv: [
'--require', 'ts-node/register', '--require', 'ts-node/register',
'--inspect=5859' '--inspect=0'
], ],
env: { env: {
...process.env, ...process.env,
@@ -140,7 +140,7 @@ if (!isProduction) {
recordingWorkerProcess = fork(recordingWorkerPath, [], { recordingWorkerProcess = fork(recordingWorkerPath, [], {
execArgv: [ execArgv: [
'--require', 'ts-node/register', '--require', 'ts-node/register',
'--inspect=5860' '--inspect=0'
], ],
env: { env: {
...process.env, ...process.env,
@@ -183,21 +183,29 @@ app.use((req, res, next) => {
next(); next();
}); });
if (require.main === module) {
setInterval(() => { setInterval(() => {
processQueuedRuns(); processQueuedRuns();
}, 5000); }, 5000);
}
if (require.main === module) {
server.listen(SERVER_PORT, '0.0.0.0', async () => { server.listen(SERVER_PORT, '0.0.0.0', async () => {
try { try {
await connectDB(); await connectDB();
await syncDB(); await syncDB();
io = new Server(server);
logger.log('info', `Server listening on port ${SERVER_PORT}`); logger.log('info', `Server listening on port ${SERVER_PORT}`);
} catch (error: any) { } catch (error: any) {
logger.log('error', `Failed to connect to the database: ${error.message}`); logger.log('error', `Failed to connect to the database: ${error.message}`);
process.exit(1); process.exit(1);
} }
}); });
}
if (require.main === module) {
process.on('SIGINT', async () => { process.on('SIGINT', async () => {
console.log('Main app shutting down...'); console.log('Main app shutting down...');
try { try {
@@ -229,3 +237,4 @@ process.on('SIGINT', async () => {
} }
process.exit(); process.exit();
}); });
}