feat: rand port assign worker, check module required
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();
|
||||||
@@ -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,49 +183,58 @@ app.use((req, res, next) => {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
setInterval(() => {
|
if (require.main === module) {
|
||||||
processQueuedRuns();
|
setInterval(() => {
|
||||||
}, 5000);
|
processQueuedRuns();
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
server.listen(SERVER_PORT, '0.0.0.0', async () => {
|
if (require.main === module) {
|
||||||
try {
|
server.listen(SERVER_PORT, '0.0.0.0', async () => {
|
||||||
await connectDB();
|
try {
|
||||||
await syncDB();
|
await connectDB();
|
||||||
logger.log('info', `Server listening on port ${SERVER_PORT}`);
|
await syncDB();
|
||||||
} catch (error: any) {
|
|
||||||
logger.log('error', `Failed to connect to the database: ${error.message}`);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('SIGINT', async () => {
|
io = new Server(server);
|
||||||
console.log('Main app shutting down...');
|
|
||||||
try {
|
|
||||||
await Run.update(
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
logger.log('info', `Server listening on port ${SERVER_PORT}`);
|
||||||
console.log('Closing PostgreSQL connection pool...');
|
} catch (error: any) {
|
||||||
await pool.end();
|
logger.log('error', `Failed to connect to the database: ${error.message}`);
|
||||||
console.log('PostgreSQL connection pool closed');
|
process.exit(1);
|
||||||
} catch (error) {
|
}
|
||||||
console.error('Error closing PostgreSQL connection pool:', error);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isProduction) {
|
if (require.main === module) {
|
||||||
if (workerProcess) workerProcess.kill();
|
process.on('SIGINT', async () => {
|
||||||
if (recordingWorkerProcess) recordingWorkerProcess.kill();
|
console.log('Main app shutting down...');
|
||||||
}
|
try {
|
||||||
process.exit();
|
await Run.update(
|
||||||
});
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log('Closing PostgreSQL connection pool...');
|
||||||
|
await pool.end();
|
||||||
|
console.log('PostgreSQL connection pool closed');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error closing PostgreSQL connection pool:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isProduction) {
|
||||||
|
if (workerProcess) workerProcess.kill();
|
||||||
|
if (recordingWorkerProcess) recordingWorkerProcess.kill();
|
||||||
|
}
|
||||||
|
process.exit();
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user