Merge pull request #752 from RohitR311/macset-fix

fix: macOS compatibility issues
This commit is contained in:
Karishma Shukla
2025-08-25 20:10:41 +05:30
committed by GitHub

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();
@@ -10,7 +11,6 @@ import logger from './logger';
import { connectDB, syncDB } from './storage/db' import { connectDB, syncDB } from './storage/db'
import cookieParser from 'cookie-parser'; import cookieParser from 'cookie-parser';
import { SERVER_PORT } from "./constants/config"; import { SERVER_PORT } from "./constants/config";
import { Server } from "socket.io";
import { readdirSync } from "fs" import { readdirSync } from "fs"
import { fork } from 'child_process'; import { fork } from 'child_process';
import { capture } from "./utils/analytics"; import { capture } from "./utils/analytics";
@@ -75,9 +75,8 @@ 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.
@@ -112,7 +111,58 @@ const recordingWorkerPath = path.resolve(__dirname, isProduction ? './pgboss-wor
let workerProcess: any; let workerProcess: any;
let recordingWorkerProcess: any; let recordingWorkerProcess: any;
if (!isProduction) { app.get('/', function (req, res) {
capture(
'maxun-oss-server-run', {
event: 'server_started',
}
);
return res.send('Maxun server started 🚀');
});
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', process.env.PUBLIC_URL || 'http://localhost:5173');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', 'true');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
if (require.main === module) {
setInterval(() => {
processQueuedRuns();
}, 5000);
}
if (require.main === module) {
server.listen(SERVER_PORT, '0.0.0.0', async () => {
try {
await connectDB();
await syncDB();
io = new Server(server);
io.of('/queued-run').on('connection', (socket) => {
const userId = socket.handshake.query.userId as string;
if (userId) {
socket.join(`user-${userId}`);
logger.log('info', `Client joined queued-run namespace for user: ${userId}, socket: ${socket.id}`);
socket.on('disconnect', () => {
logger.log('info', `Client disconnected from queued-run namespace: ${socket.id}`);
});
} else {
logger.log('warn', `Client connected to queued-run namespace without userId: ${socket.id}`);
socket.disconnect();
}
});
if (!isProduction) {
if (process.platform === 'win32') {
workerProcess = fork(workerPath, [], { workerProcess = fork(workerPath, [], {
execArgv: ['--inspect=5859'], execArgv: ['--inspect=5859'],
}); });
@@ -138,61 +188,28 @@ if (!isProduction) {
recordingWorkerProcess.on('exit', (code: any) => { recordingWorkerProcess.on('exit', (code: any) => {
console.log(`Recording worker exited with code: ${code}`); console.log(`Recording worker exited with code: ${code}`);
}); });
}
app.get('/', function (req, res) {
capture(
'maxun-oss-server-run', {
event: 'server_started',
}
);
return res.send('Maxun server started 🚀');
});
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', process.env.PUBLIC_URL || 'http://localhost:5173');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', 'true');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
io.of('/queued-run').on('connection', (socket) => {
const userId = socket.handshake.query.userId as string;
if (userId) {
socket.join(`user-${userId}`);
logger.log('info', `Client joined queued-run namespace for user: ${userId}, socket: ${socket.id}`);
socket.on('disconnect', () => {
logger.log('info', `Client disconnected from queued-run namespace: ${socket.id}`);
});
} else { } else {
logger.log('warn', `Client connected to queued-run namespace without userId: ${socket.id}`); // Run in same process for non-Windows
socket.disconnect();
}
});
setInterval(() => {
processQueuedRuns();
}, 5000);
server.listen(SERVER_PORT, '0.0.0.0', async () => {
try { try {
await connectDB(); await import('./schedule-worker');
await syncDB(); await import('./pgboss-worker');
console.log('Workers started in main process for memory sharing');
} catch (error) {
console.error('Failed to start workers in main process:', error);
}
}
}
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);
} }
}); });
}
process.on('SIGINT', async () => { if (require.main === module) {
process.on('SIGINT', async () => {
console.log('Main app shutting down...'); console.log('Main app shutting down...');
try { try {
await Run.update( await Run.update(
@@ -217,9 +234,10 @@ process.on('SIGINT', async () => {
console.error('Error closing PostgreSQL connection pool:', error); console.error('Error closing PostgreSQL connection pool:', error);
} }
if (!isProduction) { if (!isProduction && process.platform === 'win32') {
if (workerProcess) workerProcess.kill(); if (workerProcess) workerProcess.kill();
if (recordingWorkerProcess) recordingWorkerProcess.kill(); if (recordingWorkerProcess) recordingWorkerProcess.kill();
} }
process.exit(); process.exit();
}); });
}