diff --git a/common.js b/common.js index e2af5165..2d35597b 100644 --- a/common.js +++ b/common.js @@ -179,17 +179,19 @@ module.exports.checkPasswordRequirements = function(password, requirements) { // This is useful to limit the number of agents upgrading at the same time, to not swamp // the network with traffic. -// taskLimiterQueue.launch(somethingToDo, argument); +// taskLimiterQueue.launch(somethingToDo, argument, priority); // // function somethingToDo(argument, taskid, taskLimiterQueue) { // setTimeout(function () { taskLimiterQueue.completed(taskid); }, Math.random() * 2000); // } -module.exports.createTaskLimiterQueue = function(maxTasks, maxTaskTime, cleaningInterval) { - var obj = { maxTasks: maxTasks, maxTaskTime: (maxTaskTime * 1000), nextTaskId: 0, currentCount: 0, current: {}, pending: [], timer: null }; +module.exports.createTaskLimiterQueue = function (maxTasks, maxTaskTime, cleaningInterval) { + var obj = { maxTasks: maxTasks, maxTaskTime: (maxTaskTime * 1000), nextTaskId: 0, currentCount: 0, current: {}, pending: [[], [], []], timer: null }; // Add a task to the super queue - obj.launch = function (func, arg) { + // Priority: 0 = High, 1 = Medium, 2 = Low + obj.launch = function (func, arg, pri) { + if (typeof pri != 'number') { pri = 2; } if (obj.currentCount < obj.maxTasks) { // Run this task now const id = obj.nextTaskId++; @@ -201,7 +203,7 @@ module.exports.createTaskLimiterQueue = function(maxTasks, maxTaskTime, cleaning } else { // Hold this task //console.log('Holding'); - obj.pending.push({ func: func, arg: arg }); + obj.pending[pri].push({ func: func, arg: arg }); } } @@ -209,15 +211,22 @@ module.exports.createTaskLimiterQueue = function(maxTasks, maxTaskTime, cleaning obj.completed = function (taskid) { //console.log('Completed ' + taskid); if (obj.current[taskid]) { delete obj.current[taskid]; obj.currentCount--; } else { return; } - while ((obj.pending.length > 0) && (obj.currentCount < obj.maxTasks)) { + while ((obj.currentCount < obj.maxTasks) && ((obj.pending[0].length > 0) || (obj.pending[1].length > 0) || (obj.pending[2].length > 0))) { // Run this task now - const t = obj.pending.shift(), id = obj.nextTaskId++; + var t = null; + if (obj.pending[0].length > 0) { t = obj.pending[0].shift(); } + else if (obj.pending[1].length > 0) { t = obj.pending[1].shift(); } + else if (obj.pending[2].length > 0) { t = obj.pending[2].shift(); } + const id = obj.nextTaskId++; obj.current[id] = Date.now() + obj.maxTaskTime; obj.currentCount++; //console.log('PendingLaunch ' + id); t.func(t.arg, id, obj); // Start the task } - if ((obj.pending.length == 0) && (obj.timer != null)) { clearInterval(obj.timer); obj.timer = null; } // All done, clear the timer + if ((obj.pending[0].length == 0) && (obj.pending[1].length == 0) && (obj.pending[2].length == 0) && (obj.timer != null)) { + // All done, clear the timer + clearInterval(obj.timer); obj.timer = null; + } } // Look for long standing tasks and clean them up @@ -227,4 +236,4 @@ module.exports.createTaskLimiterQueue = function(maxTasks, maxTaskTime, cleaning } return obj; -} \ No newline at end of file +} diff --git a/meshagent.js b/meshagent.js index f6b559ca..6fe73f05 100644 --- a/meshagent.js +++ b/meshagent.js @@ -125,12 +125,12 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { //obj.send(obj.common.ShortToStr(10) + obj.common.ShortToStr(0) + meshcorehash + obj.parent.parent.defaultMeshCores[corename]); // MeshCommand_CoreModule, start core update //obj.parent.parent.debug(1, 'Updating code ' + corename); - // Update new core with task limiting so not to flood the server. + // Update new core with task limiting so not to flood the server. This is a high priority task. obj.parent.parent.taskLimiter.launch(function (argument, taskid, taskLimiterQueue) { obj.send(obj.common.ShortToStr(10) + obj.common.ShortToStr(0) + argument.hash + argument.core, function () { obj.parent.parent.taskLimiter.completed(taskid); }); // MeshCommand_CoreModule, start core update obj.parent.parent.debug(1, 'Updating code ' + argument.name); agentCoreIsStable(); - }, { hash: meshcorehash, core: obj.parent.parent.defaultMeshCores[corename], name: corename }); + }, { hash: meshcorehash, core: obj.parent.parent.defaultMeshCores[corename], name: corename }, 0); } obj.agentCoreCheck++; } @@ -169,7 +169,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { if ((msg.length == 52) && (obj.agentExeInfo != null) && (obj.agentExeInfo.update == true)) { var agenthash = obj.common.rstr2hex(msg.substring(4)).toLowerCase(); if ((agenthash != obj.agentExeInfo.hash) && (agenthash != '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) { - // Mesh agent update required, do it using task limiter so not to flood the network. + // Mesh agent update required, do it using task limiter so not to flood the network. Medium priority task. obj.parent.parent.taskLimiter.launch(function (argument, taskid, taskLimiterQueue) { if (obj.nodeid != null) { obj.parent.parent.debug(1, 'Agent update required, NodeID=0x' + obj.nodeid.substring(0, 16) + ', ' + obj.agentExeInfo.desc); } obj.fs.open(obj.agentExeInfo.path, 'r', function (err, fd) { @@ -203,7 +203,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { obj.send(obj.agentUpdate.buf); // Command 14, mesh agent first data block } }); - }, null); + }, null, 1); } else { // Check the mesh core, if the agent is capable of running one diff --git a/package.json b/package.json index 2b957f8d..5beced05 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meshcentral", - "version": "0.2.7-o", + "version": "0.2.7-p", "keywords": [ "Remote Management", "Intel AMT", diff --git a/swarmserver.js b/swarmserver.js index 1613dd0a..c974441b 100644 --- a/swarmserver.js +++ b/swarmserver.js @@ -215,11 +215,11 @@ module.exports.CreateSwarmServer = function (parent, db, args, certificates) { socket.tag.updatePtr = 0; //console.log('Performing legacy agent update from ' + nodeblock.agentversion + '.' + nodeblock.agenttype + ' to ' + socket.tag.update.ver + '.' + socket.tag.update.arch + ' on ' + nodeblock.agentname + '.'); - // Start the agent download using the task limiter so not to flood the server. + // Start the agent download using the task limiter so not to flood the server. Low priority task obj.parent.taskLimiter.launch(function (socket, taskid, taskLimiterQueue) { socket.tag.taskid = taskid; obj.SendCommand(socket, LegacyMeshProtocol.GETSTATE, common.IntToStr(5) + common.IntToStr(0)); // agent.SendQuery(5, 0); // Start the agent download - }, socket); + }, socket, 2); } else { //console.log('No legacy agent update for ' + nodeblock.agentversion + '.' + nodeblock.agenttype + ' on ' + nodeblock.agentname + '.'); } diff --git a/views/default-min.handlebars b/views/default-min.handlebars index d688c160..2efffb3e 100644 --- a/views/default-min.handlebars +++ b/views/default-min.handlebars @@ -1 +1 @@ -
{{{logoutControl}}}
My Devices | My Account | My Events | My Files |
{{{logoutControl}}}
My Devices | My Account | My Events | My Files |