arguments, fixes, dockerfile, ci
This commit is contained in:
21
main.js
21
main.js
@@ -1,6 +1,19 @@
|
||||
const {workerFactory, onMessage, onError, onExit} = require('./util');
|
||||
const THREAD_COUNT = 4; // Number of threads you want to run
|
||||
const {workerFactory, onMessage, onError, onExit, logger} = require('./util');
|
||||
const os = require('os');
|
||||
|
||||
let THREAD_COUNT = Math.max(1, os.cpus().length - 1);;
|
||||
// Get thread count from first positional argument or use default (all cores - 1)
|
||||
if (process.argv.length > 2) {
|
||||
const requestedThreads = parseInt(process.argv[2], 10);
|
||||
if (!isNaN(requestedThreads) && requestedThreads > 0) {
|
||||
THREAD_COUNT = requestedThreads;
|
||||
}
|
||||
}
|
||||
|
||||
const startDateTime = new Date().toISOString();
|
||||
console.log(`main\t🕒 Started at ${startDateTime}`);
|
||||
console.log(`main\t🧵 Using ${THREAD_COUNT} worker threads`);
|
||||
logger().info(`Application started with ${THREAD_COUNT} worker threads`);
|
||||
|
||||
const data = {
|
||||
url: 'https://care.drmax.eu/it/pharmacy/698Edb',
|
||||
@@ -12,7 +25,6 @@ const data = {
|
||||
run: true
|
||||
};
|
||||
|
||||
|
||||
const workers = [...Array(THREAD_COUNT).keys()].map((id) => workerFactory(
|
||||
{ id, ...data },
|
||||
onMessage,
|
||||
@@ -20,14 +32,17 @@ const workers = [...Array(THREAD_COUNT).keys()].map((id) => workerFactory(
|
||||
onExit
|
||||
));
|
||||
console.log(`main\t✅ Initialized ${workers.length} workers.`);
|
||||
logger().info(`Initialized ${workers.length} workers`);
|
||||
|
||||
// Handle Ctrl+C
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('\nmain\t🛑 Terminating workers...');
|
||||
logger().info('Received SIGINT signal. Terminating workers...');
|
||||
|
||||
// Gracefully terminate all workers
|
||||
await Promise.all(workers.map(worker => worker.terminate()));
|
||||
|
||||
console.log('main\t✅ All workers terminated. Exiting.');
|
||||
logger().info('All workers terminated. Exiting.');
|
||||
process.exit(0);
|
||||
});
|
||||
Reference in New Issue
Block a user