adding afterFreed hook

This commit is contained in:
Thomas Harvey 2017-10-10 01:25:43 -04:00
parent 6e0bf97142
commit ca0b0ef153
2 changed files with 39 additions and 43 deletions

View File

@ -4,16 +4,8 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
var util = require('util'); var util = require('util');
var request = require('request'); var request = require('request');
var jsonfile = require('jsonfile');
var lxc = require('../lxc'); var lxc = require('../lxc');
var doapi = require('../doapi')(); var doapi = require('../doapi')();
var settings = require('./workers.json');
// mapping of current used runners for quick loop up based on runner label
var label2runner = {};
//
var tagPrefix = settings.tagPrefix || 'clwV';
var workers = require('./worker_manager.js'); var workers = require('./worker_manager.js');

View File

@ -5,33 +5,26 @@ var lxc = require('../lxc');
var doapi = require('../doapi')(); var doapi = require('../doapi')();
var settings = require('./workers.json'); var settings = require('./workers.json');
var tagPrefix = settings.tagPrefix || 'clwV';
var Runner = (function(){ var Runner = (function(){
var proto = {}; var proto = {};
proto.create = function(config){ proto.create = function(config){
var runner = Object.create(proto); var runner = Object.create(proto);
var __empty = function(){};
Object.assign(runner, config); Object.assign(runner, config);
runner.afterFreed = runner.afterFreed || __empty;
return runner; return runner;
}; };
proto.free = function(){ proto.free = function(callback){
var runner = this; var runner = this;
lxc.stop(runner.name, runner.worker.ip); lxc.stop(runner.name, runner.worker.ip);
runner.worker.usedrunners--; runner.worker.usedrunners--;
if(runner.hasOwnProperty('timeout')){ if(runner.hasOwnProperty('timeout')){
clearTimeout(runner.timeout); clearTimeout(runner.timeout);
}; };
runner.afterFreed();
// TODO: FIX THIS
delete label2runner[runner.label];
console.log(`Runner freed ${runner.label}.`, runner.worker);
// Why does this need to run here?
workers.startRunners({worker: runner.worker});
}; };
proto.setTimeout = function(time){ proto.setTimeout = function(time){
@ -96,8 +89,26 @@ var workers = (function(){
// base array that will be the workers objects. // base array that will be the workers objects.
var workers = []; var workers = [];
var tagPrefix = settings.tagPrefix || 'clwV';
workers.runnerMap = {}; workers.runnerMap = {};
// persistent settings object
// .image is the currently used Digital Ocean snap shot ID
// .lastSnapShotId is the previous ID used Digital Ocean snap shot
// .version is the current worker version
// .size is the base Droplet size for worker creation
// .min is the minimum amount of workers that should exist
// .max is the maximum amount of works that ca exist
// .minAvail is the amount of empty workers there should be
workers.settings = settings;
// How many droplets are currently in the process of being created. It takes
// about 3 minutes to create a worker.
workers.currentCreating = 0;
workers.setRunner = function(runner){ workers.setRunner = function(runner){
runnerMap[runner.label] = runner; runnerMap[runner.label] = runner;
}; };
@ -121,21 +132,6 @@ var workers = (function(){
if(runner) return runner; if(runner) return runner;
}; };
// persistent settings object
// .image is the currently used Digital Ocean snap shot ID
// .lastSnapShotId is the previous ID used Digital Ocean snap shot
// .version is the current worker version
// .size is the base Droplet size for worker creation
// .min is the minimum amount of workers that should exist
// .max is the maximum amount of works that ca exist
// .minAvail is the amount of empty workers there should be
workers.settings = settings;
// How many droplets are currently in the process of being created. It takes
// about 3 minutes to create a worker.
workers.currentCreating = 0;
workers.create = function(config){ workers.create = function(config){
// manages the creation of a work from first call to all runners seeded // manages the creation of a work from first call to all runners seeded
@ -260,7 +256,12 @@ var workers = (function(){
name: name, name: name,
worker: args.worker, worker: args.worker,
label: args.worker.name + ':' + name, label: args.worker.name + ':' + name,
afterFreed: function(runner){
delete workers.runnerMap[runner.label];
console.log(`Runner freed ${runner.label}.`, runner.worker);
// Why does this need to run here?
workers.startRunners({worker: runner.worker});
}
}); });
args.onStart(runner, args); args.onStart(runner, args);
@ -299,16 +300,19 @@ var workers = (function(){
workers.checkForZombies(); workers.checkForZombies();
// if there are workers being created, stop scale up and down check // if there are workers being created, stop scale up and down check
if(workers.currentCreating+workers.length < workers.settings.min) null; if(workers.currentCreating+workers.length < workers.settings.min) {
else if(workers.currentCreating) null;
} else if(workers.currentCreating){
return console.log(`Killing balance, workers are being created.`); return console.log(`Killing balance, workers are being created.`);
}
// hold amount of workers with no used runners // hold amount of workers with no used runners
var lastMinAval = 0; var lastMinAval = 0;
// check to make sure the `workers.settings.minAvail` have free runners // check to make sure the `workers.settings.minAvail` have free runners
for(let worker of workers.slice(-workers.settings.minAvail)){ for(let worker of workers.slice(-workers.settings.minAvail)){
if(worker.usedrunners === 0){ // INVERT this conditional
if(worker.usedrunners !== 0){
lastMinAval++; lastMinAval++;
}else{ }else{
// no need to keep counting, workers need to be created // no need to keep counting, workers need to be created
@ -353,7 +357,7 @@ var workers = (function(){
workers.push(worker); workers.push(worker);
}); });
}; };
// does this have to be last?
// make sure Digital Ocean has a tag for the current worker version // make sure Digital Ocean has a tag for the current worker version
doapi.tagCreate(tagPrefix + workers.settings.version); doapi.tagCreate(tagPrefix + workers.settings.version);