48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
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',
|
|
body: '__VIEWSTATE=%2FwEPDwULLTE5MzAwNTM5OTlkZJTEOkK8qrP%2BTqGLIsu2BOyqAooELsVS8rKLByxL86Wb&__VIEWSTATEGENERATOR=221C5153&cpt=Care&lang=it&type=698Edb&votes=Odeslat',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Origin': 'https://care.drmax.eu/it/pharmacies'
|
|
},
|
|
run: true
|
|
};
|
|
|
|
const workers = [...Array(THREAD_COUNT).keys()].map((id) => workerFactory(
|
|
{ id, ...data },
|
|
onMessage,
|
|
onError,
|
|
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);
|
|
}); |