1
0
Fork 0
forked from svrjs/svrjs

Replace "minClusters" and "allClusters" with "minWorkers" and "allWorkers" respectively

This commit is contained in:
Dorian Niemiec 2024-05-20 17:34:29 +02:00
parent fc03b485ef
commit 5781233ff7

68
svr.js
View file

@ -5331,25 +5331,25 @@ function start(init) {
}, 300000); }, 300000);
} else if (cluster.isPrimary) { } else if (cluster.isPrimary) {
setInterval(function () { setInterval(function () {
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
var goodWorkers = []; var goodWorkers = [];
function checkWorker(callback, _id) { function checkWorker(callback, _id) {
if (typeof _id === "undefined") _id = 0; if (typeof _id === "undefined") _id = 0;
if (_id >= allClusters.length) { if (_id >= allWorkers.length) {
callback(); callback();
return; return;
} }
try { try {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
isWorkerHungUpBuff2 = true; isWorkerHungUpBuff2 = true;
cluster.workers[allClusters[_id]].on("message", msgListener); cluster.workers[allWorkers[_id]].on("message", msgListener);
cluster.workers[allClusters[_id]].send("\x14PINGPING"); cluster.workers[allWorkers[_id]].send("\x14PINGPING");
setTimeout(function () { setTimeout(function () {
if (isWorkerHungUpBuff2) { if (isWorkerHungUpBuff2) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} else { } else {
goodWorkers.push(allClusters[_id]); goodWorkers.push(allWorkers[_id]);
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
}, 250); }, 250);
@ -5357,10 +5357,10 @@ function start(init) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
} catch (err) { } catch (err) {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
cluster.workers[allClusters[_id]].removeAllListeners("message"); cluster.workers[allWorkers[_id]].removeAllListeners("message");
cluster.workers[allClusters[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[_id]])); cluster.workers[allWorkers[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allWorkers[_id]]));
cluster.workers[allClusters[_id]].on("message", listenConnListener); cluster.workers[allWorkers[_id]].on("message", listenConnListener);
} }
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
@ -5457,16 +5457,16 @@ function start(init) {
var command = argss.shift(); var command = argss.shift();
if (line != "") { if (line != "") {
if (cluster.isPrimary !== undefined) { if (cluster.isPrimary !== undefined) {
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
if (command == "block") commands.block(argss); if (command == "block") commands.block(argss);
if (command == "unblock") commands.unblock(argss); if (command == "unblock") commands.unblock(argss);
if (command == "restart") { if (command == "restart") {
var stopError = false; var stopError = false;
exiting = true; exiting = true;
for (var i = 0; i < allClusters.length; i++) { for (var i = 0; i < allWorkers.length; i++) {
try { try {
if (cluster.workers[allClusters[i]]) { if (cluster.workers[allWorkers[i]]) {
cluster.workers[allClusters[i]].kill(); cluster.workers[allWorkers[i]].kill();
} }
} catch (err) { } catch (err) {
stopError = true; stopError = true;
@ -5503,9 +5503,9 @@ function start(init) {
} }
if (command == "stop") { if (command == "stop") {
exiting = true; exiting = true;
allClusters = Object.keys(cluster.workers); allWorkers = Object.keys(cluster.workers);
} }
allClusters.forEach(function (clusterID) { allWorkers.forEach(function (clusterID) {
try { try {
if (cluster.workers[clusterID]) { if (cluster.workers[clusterID]) {
cluster.workers[clusterID].on("message", msgListener); cluster.workers[clusterID].on("message", msgListener);
@ -5674,28 +5674,28 @@ function start(init) {
setTimeout(function () { setTimeout(function () {
setInterval(function () { setInterval(function () {
if (!closedMaster && !exiting) { if (!closedMaster && !exiting) {
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
var minClusters = 0; var minWorkers = 0;
minClusters = Math.ceil(cpus * 0.625); minWorkers = Math.ceil(cpus * 0.625);
if (minClusters < 2) minClusters = 2; if (minWorkers < 2) minWorkers = 2;
var goodWorkers = []; var goodWorkers = [];
function checkWorker(callback, _id) { function checkWorker(callback, _id) {
if (typeof _id === "undefined") _id = 0; if (typeof _id === "undefined") _id = 0;
if (_id >= allClusters.length) { if (_id >= allWorkers.length) {
callback(); callback();
return; return;
} }
try { try {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
isWorkerHungUpBuff = true; isWorkerHungUpBuff = true;
cluster.workers[allClusters[_id]].on("message", msgListener); cluster.workers[allWorkers[_id]].on("message", msgListener);
cluster.workers[allClusters[_id]].send("\x14KILLPING"); cluster.workers[allWorkers[_id]].send("\x14KILLPING");
setTimeout(function () { setTimeout(function () {
if (isWorkerHungUpBuff) { if (isWorkerHungUpBuff) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} else { } else {
goodWorkers.push(allClusters[_id]); goodWorkers.push(allWorkers[_id]);
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
}, 250); }, 250);
@ -5703,16 +5703,16 @@ function start(init) {
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
} catch (err) { } catch (err) {
if (cluster.workers[allClusters[_id]]) { if (cluster.workers[allWorkers[_id]]) {
cluster.workers[allClusters[_id]].removeAllListeners("message"); cluster.workers[allWorkers[_id]].removeAllListeners("message");
cluster.workers[allClusters[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allClusters[_id]])); cluster.workers[allWorkers[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allWorkers[_id]]));
cluster.workers[allClusters[_id]].on("message", listenConnListener); cluster.workers[allWorkers[_id]].on("message", listenConnListener);
} }
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
} }
checkWorker(function () { checkWorker(function () {
if (goodWorkers.length > minClusters) { if (goodWorkers.length > minWorkers) {
var wN = Math.floor(Math.random() * goodWorkers.length); var wN = Math.floor(Math.random() * goodWorkers.length);
if (wN == goodWorkers.length) return; if (wN == goodWorkers.length) return;
try { try {
@ -5853,11 +5853,11 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
reallyExiting = true; reallyExiting = true;
if (cluster.isPrimary !== undefined) { if (cluster.isPrimary !== undefined) {
exiting = true; exiting = true;
var allClusters = Object.keys(cluster.workers); var allWorkers = Object.keys(cluster.workers);
for (var i = 0; i < allClusters.length; i++) { for (var i = 0; i < allWorkers.length; i++) {
try { try {
if (cluster.workers[allClusters[i]]) { if (cluster.workers[allWorkers[i]]) {
cluster.workers[allClusters[i]].send("stop"); cluster.workers[allWorkers[i]].send("stop");
} }
} catch (err) { } catch (err) {
// Worker will crash with EPIPE anyway. // Worker will crash with EPIPE anyway.