Skip to content

Commit

Permalink
server cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
octopoulos authored and Aloril committed Feb 16, 2021
1 parent c9e644d commit ee12ac1
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 606 deletions.
8 changes: 0 additions & 8 deletions a.js

This file was deleted.

132 changes: 52 additions & 80 deletions cluster.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,63 @@
/*
globals
console, require, setInterval
require, setInterval
*/
'use strict';

const cluster = require('cluster');
const os = require('os');
const numServer = 1;
const argv = require('yargs').argv;
let {DefaultInt, LS} = require('./js/common.js'),
cluster = require('cluster'),
os = require('os'),
argv = require('yargs').argv;

if (argv.port == undefined)
{
argv.port = 8000;
portnum = 8000;
}
else
{
portnum = argv.port;
}

if (argv.bonus != undefined)
{
if (isNaN(argv.bonus))
{
bonus = 0;
}
else
{
bonus = parseInt(argv.bonus);
}
}
let // bonus = DefaultInt(argv.bonus, 0),
numServer = 1,
portnum = DefaultInt(argv.port, 8000);

if (cluster.isMaster)
{
const cpus = os.cpus().length;
var count = 0;
var clientCount = 0;
for (let i = 0; i < numServer; i++)
{
console.log(`Forking for ${cpus} CPUs`);
var worker = cluster.fork();
count = 0;
worker.on('message', function(msg)
{
if (typeof msg.users != 'undefined')
{
console.log ("CLUSTER: Count is :" + count + " ,got count:" + parseInt(msg.users) + ",clientCount:" + clientCount);
count = parseInt(count) + parseInt(msg.users);
clientCount ++;
}
});
}
if (argv.port != undefined)
argv.port = portnum;

function eachWorker(callback)
{
for (const id in cluster.workers)
{
callback(cluster.workers[id]);
}
}

const updateWorkers = () => {
eachWorker(worker => {
if (clientCount == numServer)
{
worker.send({'count':count});
}
});
count = 0;
clientCount = 0;
};

updateWorkers();
setInterval(updateWorkers, 10000);
function eachWorker(callback) {
for (let id in cluster.workers)
callback(cluster.workers[id]);
}

cluster.on('exit', (worker, code, signal) =>
{
if (code !== 0 && !worker.exitedAfterDisconnect)
{
console.log(`Worker ${worker.id} crashed. ` +
'Starting a new worker...');
cluster.fork();
}
});
if (cluster.isMaster) {
let clientCount = 0,
count = 0,
cpus = os.cpus().length;

for (let i = 0; i < numServer; i ++) {
LS(`Forking for ${cpus} CPUs`);
count = 0;
let worker = cluster.fork();

worker.on('message', function(msg) {
if (typeof msg.users != 'undefined') {
LS(`CLUSTER: count=${count} : msg.users=${parseInt(msg.users)} : clientCount=${clientCount}`);
count = parseInt(count) + parseInt(msg.users);
clientCount ++;
}
});
}

let updateWorkers = () => {
eachWorker(worker => {
if (clientCount == numServer)
worker.send({'count':count});
});
count = 0;
clientCount = 0;
};

updateWorkers();
setInterval(updateWorkers, 10000);

cluster.on('exit', (worker, code) => {
if (code !== 0 && !worker.exitedAfterDisconnect) {
LS(`Worker ${worker.id} crashed. Starting a new worker...`);
cluster.fork();
}
});
}
else
{
require("./server");
}

require('./server-new.js');
10 changes: 5 additions & 5 deletions js/3d.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 3d.js
// @author octopoulo <[email protected]>
// @version 2021-02-02
// @version 2021-02-03
//
// general 3d rendering code
//
Expand All @@ -15,7 +15,7 @@ S, save_option, set_modal_events, Show, Stats, Style, T:true, THREE, translate_n
'use strict';

// <<
if (typeof global != 'undefined') {
if (typeof global != 'undefined' && typeof require != 'undefined') {
['common'].forEach(key => {
Object.assign(global, require(`./${key}.js`));
});
Expand Down Expand Up @@ -343,7 +343,7 @@ function interpolate_store(part) {
* Load a model
* @param {string} name key for model storage
* @param {string} filename
* @param {function} callback
* @param {Function} callback
*/
function load_model(name, filename, callback) {
// 0) need T
Expand Down Expand Up @@ -394,7 +394,7 @@ function load_model(name, filename, callback) {
/**
* Load multiple models
* @param {Object} filenames
* @param {function} callback
* @param {Function} callback
*/
function load_models(filenames, callback) {
let keys = Keys(filenames),
Expand Down Expand Up @@ -968,7 +968,7 @@ function gamepad_update() {
* @param {number=} cycle end of the cycle
* @param {boolean=} inside
* @param {boolean=} interrupt play the sound again even if it's being played
* @param {function=} loaded only load the audio
* @param {Function=} loaded only load the audio
* @param {number=} start start of the 2nd cycle
* @param {boolean=} voice
* @param {number=} volume
Expand Down
Loading

0 comments on commit ee12ac1

Please sign in to comment.