From 983b76afce891cc857272dfa7ec64cedc73bfa33 Mon Sep 17 00:00:00 2001 From: Thomas Harvey Date: Tue, 10 Oct 2017 16:44:48 -0400 Subject: [PATCH] Moving some code. - grouped runnerMap code in workers object - created isZombie function in Worker --- routes/worker_collection.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/routes/worker_collection.js b/routes/worker_collection.js index 73bd6ec..d082890 100644 --- a/routes/worker_collection.js +++ b/routes/worker_collection.js @@ -96,6 +96,10 @@ var Worker = (function(){ }); }; + proto.isZombie = function(){ + return this.availrunners.length === 0 && this.usedrunners === 0; + }; + proto.startRunners = function(args){ // console.log('starting runners on', args.worker.name, args.worker.ip) @@ -160,7 +164,6 @@ var WorkerCollection = (function(){ var tagPrefix = settings.tagPrefix || 'clwV'; - workers.runnerMap = {}; // persistent settings object // .image is the currently used Digital Ocean snap shot ID @@ -176,6 +179,11 @@ var WorkerCollection = (function(){ // about 3 minutes to create a worker. workers.currentCreating = 0; + //************************************************** + //************************************************** + // temporary until a better location is found + workers.runnerMap = {}; + workers.__runnerCleanUp = function(label){ delete workers.runnerMap[label]; }; @@ -194,6 +202,8 @@ var WorkerCollection = (function(){ return runnerMap[runner.label]; }; + //************************************************** + //************************************************** workers.getAvailableRunner = function(runner){ for(let worker of workers){ @@ -259,7 +269,7 @@ var WorkerCollection = (function(){ workers.destroy = function(worker){ // removes last one - // todo: If worker is passed, check for it in the workers array and + // X TODO: If worker is passed, check for it in the workers array and // remove it if found. if ( worker ){ var worker_idx = workers.indexOf(worker); @@ -290,6 +300,7 @@ var WorkerCollection = (function(){ }); } + // TODO: move to seperate method doapi.dropletsByTag(tag, function(data){ data = JSON.parse(data); console.log(`Deleting ${data['droplets'].length} workers tagged ${tag}. Workers`, @@ -312,8 +323,8 @@ var WorkerCollection = (function(){ // if a runner has no available runners and no used runners, its a // zombie. This should happen when a newer image ID has been added // and old workers slowly lose there usefulness. - if(worker.availrunners.length === 0 && worker.usedrunners === 0){ - workers.splice(workers.indexOf(worker), 1); + + if(worker.isZombie()){ console.log(`Zombie! Worker ${worker.name}, destroying.`); workers.destroy(worker); zombies++;