Merge pull request #752 from RohitR311/macset-fix
fix: macOS compatibility issues
This commit is contained in:
@@ -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,34 +111,6 @@ const recordingWorkerPath = path.resolve(__dirname, isProduction ? './pgboss-wor
|
|||||||
let workerProcess: any;
|
let workerProcess: any;
|
||||||
let recordingWorkerProcess: any;
|
let recordingWorkerProcess: any;
|
||||||
|
|
||||||
if (!isProduction) {
|
|
||||||
workerProcess = fork(workerPath, [], {
|
|
||||||
execArgv: ['--inspect=5859'],
|
|
||||||
});
|
|
||||||
workerProcess.on('message', (message: any) => {
|
|
||||||
console.log(`Message from worker: ${message}`);
|
|
||||||
});
|
|
||||||
workerProcess.on('error', (error: any) => {
|
|
||||||
console.error(`Error in worker: ${error}`);
|
|
||||||
});
|
|
||||||
workerProcess.on('exit', (code: any) => {
|
|
||||||
console.log(`Worker exited with code: ${code}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
recordingWorkerProcess = fork(recordingWorkerPath, [], {
|
|
||||||
execArgv: ['--inspect=5860'],
|
|
||||||
});
|
|
||||||
recordingWorkerProcess.on('message', (message: any) => {
|
|
||||||
console.log(`Message from recording worker: ${message}`);
|
|
||||||
});
|
|
||||||
recordingWorkerProcess.on('error', (error: any) => {
|
|
||||||
console.error(`Error in recording worker: ${error}`);
|
|
||||||
});
|
|
||||||
recordingWorkerProcess.on('exit', (code: any) => {
|
|
||||||
console.log(`Recording worker exited with code: ${code}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
app.get('/', function (req, res) {
|
app.get('/', function (req, res) {
|
||||||
capture(
|
capture(
|
||||||
'maxun-oss-server-run', {
|
'maxun-oss-server-run', {
|
||||||
@@ -160,66 +131,113 @@ app.use((req, res, next) => {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
io.of('/queued-run').on('connection', (socket) => {
|
if (require.main === module) {
|
||||||
const userId = socket.handshake.query.userId as string;
|
setInterval(() => {
|
||||||
|
processQueuedRuns();
|
||||||
if (userId) {
|
}, 5000);
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
setInterval(() => {
|
if (require.main === module) {
|
||||||
processQueuedRuns();
|
server.listen(SERVER_PORT, '0.0.0.0', async () => {
|
||||||
}, 5000);
|
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}`);
|
||||||
|
|
||||||
server.listen(SERVER_PORT, '0.0.0.0', async () => {
|
socket.on('disconnect', () => {
|
||||||
try {
|
logger.log('info', `Client disconnected from queued-run namespace: ${socket.id}`);
|
||||||
await connectDB();
|
});
|
||||||
await syncDB();
|
} else {
|
||||||
logger.log('info', `Server listening on port ${SERVER_PORT}`);
|
logger.log('warn', `Client connected to queued-run namespace without userId: ${socket.id}`);
|
||||||
} catch (error: any) {
|
socket.disconnect();
|
||||||
logger.log('error', `Failed to connect to the database: ${error.message}`);
|
}
|
||||||
process.exit(1);
|
});
|
||||||
}
|
|
||||||
});
|
if (!isProduction) {
|
||||||
|
if (process.platform === 'win32') {
|
||||||
|
workerProcess = fork(workerPath, [], {
|
||||||
|
execArgv: ['--inspect=5859'],
|
||||||
|
});
|
||||||
|
workerProcess.on('message', (message: any) => {
|
||||||
|
console.log(`Message from worker: ${message}`);
|
||||||
|
});
|
||||||
|
workerProcess.on('error', (error: any) => {
|
||||||
|
console.error(`Error in worker: ${error}`);
|
||||||
|
});
|
||||||
|
workerProcess.on('exit', (code: any) => {
|
||||||
|
console.log(`Worker exited with code: ${code}`);
|
||||||
|
});
|
||||||
|
|
||||||
process.on('SIGINT', async () => {
|
recordingWorkerProcess = fork(recordingWorkerPath, [], {
|
||||||
console.log('Main app shutting down...');
|
execArgv: ['--inspect=5860'],
|
||||||
try {
|
});
|
||||||
await Run.update(
|
recordingWorkerProcess.on('message', (message: any) => {
|
||||||
{
|
console.log(`Message from recording worker: ${message}`);
|
||||||
status: 'failed',
|
});
|
||||||
finishedAt: new Date().toLocaleString(),
|
recordingWorkerProcess.on('error', (error: any) => {
|
||||||
log: 'Process interrupted during execution - worker shutdown'
|
console.error(`Error in recording worker: ${error}`);
|
||||||
},
|
});
|
||||||
{
|
recordingWorkerProcess.on('exit', (code: any) => {
|
||||||
where: { status: 'running' }
|
console.log(`Recording worker exited with code: ${code}`);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Run in same process for non-Windows
|
||||||
|
try {
|
||||||
|
await import('./schedule-worker');
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
|
||||||
} catch (error: any) {
|
logger.log('info', `Server listening on port ${SERVER_PORT}`);
|
||||||
console.error('Error updating runs:', error);
|
} catch (error: any) {
|
||||||
}
|
logger.log('error', `Failed to connect to the database: ${error.message}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
if (require.main === module) {
|
||||||
console.log('Closing PostgreSQL connection pool...');
|
process.on('SIGINT', async () => {
|
||||||
await pool.end();
|
console.log('Main app shutting down...');
|
||||||
console.log('PostgreSQL connection pool closed');
|
try {
|
||||||
} catch (error) {
|
await Run.update(
|
||||||
console.error('Error closing PostgreSQL connection pool:', error);
|
{
|
||||||
}
|
status: 'failed',
|
||||||
|
finishedAt: new Date().toLocaleString(),
|
||||||
|
log: 'Process interrupted during execution - worker shutdown'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
where: { status: 'running' }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('Error updating runs:', error);
|
||||||
|
}
|
||||||
|
|
||||||
if (!isProduction) {
|
try {
|
||||||
if (workerProcess) workerProcess.kill();
|
console.log('Closing PostgreSQL connection pool...');
|
||||||
if (recordingWorkerProcess) recordingWorkerProcess.kill();
|
await pool.end();
|
||||||
}
|
console.log('PostgreSQL connection pool closed');
|
||||||
process.exit();
|
} catch (error) {
|
||||||
});
|
console.error('Error closing PostgreSQL connection pool:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isProduction && process.platform === 'win32') {
|
||||||
|
if (workerProcess) workerProcess.kill();
|
||||||
|
if (recordingWorkerProcess) recordingWorkerProcess.kill();
|
||||||
|
}
|
||||||
|
process.exit();
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user