patch for Worker.sync

This commit is contained in:
Thomas Harvey 2017-10-24 16:42:17 -04:00
parent db8914ed7f
commit 3dcfbf4d77

View File

@ -94,6 +94,13 @@ var Worker = (function(){
}); });
worker.availrunners = []; worker.availrunners = [];
// need map of used runners
// need list of all runners
// - sync should probably populate the all runners container
// - availrunners should the diff of used runners and all runners
// runners should probably indicate when they have been used.
worker.ip = worker.publicIP; worker.ip = worker.publicIP;
worker.usedrunners = 0; worker.usedrunners = 0;
worker.age = +(new Date()); worker.age = +(new Date());
@ -112,7 +119,6 @@ var Worker = (function(){
var runner = this.availrunners.pop(); var runner = this.availrunners.pop();
this.usedrunners++; this.usedrunners++;
runner.setTimeout(); runner.setTimeout();
return runner; return runner;
}; };
@ -151,9 +157,9 @@ var Worker = (function(){
// When should this be called // When should this be called
proto.sync = function(callback, errorCallback, maxAttempts){ proto.sync = function(callback, errorCallback, maxAttempts){
maxAttempts = maxAttempts || maxSyncAttempts;
var worker = this; var worker = this;
maxAttempts = maxAttempts || maxSyncAttempts;
worker.isSyncing = true; worker.isSyncing = true;
callback = callback || __empty; callback = callback || __empty;
errorCallback = errorCallback || __empty; errorCallback = errorCallback || __empty;
@ -164,9 +170,6 @@ var Worker = (function(){
// - check memory and check runners // - check memory and check runners
// - when does start runners get called? // - when does start runners get called?
// worker.ramPercentUsed();
lxc.exec('lxc-ls --fancy', worker.ip, function(data, error, stderr){ lxc.exec('lxc-ls --fancy', worker.ip, function(data, error, stderr){
if (error){ if (error){
console.log("Sync Error: \n", error); console.log("Sync Error: \n", error);
@ -175,7 +178,7 @@ var Worker = (function(){
errorCallback(error, worker); errorCallback(error, worker);
}, 0); }, 0);
} else { } else {
console.log("Waiting 10 secongs") console.log("Waiting 15 seconds")
worker.syncAttempts++; worker.syncAttempts++;
setTimeout(function(){ setTimeout(function(){
worker.sync(maxAttempts, callback, errorCallback); worker.sync(maxAttempts, callback, errorCallback);
@ -203,15 +206,18 @@ var Worker = (function(){
} }
console.log(`RUNNERS FOUND[=> ${worker.ip}`); console.log(`RUNNERS FOUND[=> ${worker.ip}`);
console.log(`RUNNERS FOUND[=>`, runners); console.log(`RUNNERS FOUND[=>`, runners);
// bad
worker.availrunners = []; worker.availrunners = [];
for (let idx = 0, stop = runners.length; idx < stop; idx++){ for (let idx = 0, stop = runners.length; idx < stop; idx++){
if(runners[idx].state !== "STOPPED"){ if(runners[idx].state !== "STOPPED" && !Runner.get(worker.name + ':' + runners[idx].name)){
var runner = Runner.create({ var runner = Runner.create({
"name": runners[idx].name, "name": runners[idx].name,
"ipv4": runners[idx].ipv4,
"worker": worker, "worker": worker,
"label": worker.name + ':' + runners[idx].name "label": worker.name + ':' + runners[idx].name
}); });
worker.availrunners.push(runner); worker.availrunners.push(runner);
} }
} }
@ -225,13 +231,17 @@ var Worker = (function(){
}); });
}; };
proto.initialize = function(args, config){ proto.initialize = function(params, config){
// Create droplet // Create droplet
// Once active the droplet begins to create runners // Once active the droplet begins to create runners
var maxMemoryUsage = args.maxMemoryUsage || config.maxMemoryUsage || 80; var maxMemoryUsage = args.maxMemoryUsage || config.maxMemoryUsage || 80;
var worker_uuid = utils.uuid(); var worker_uuid = utils.uuid();
var phone_home = config.home || "/worker/ping"; var phone_home = config.home || "/worker/ping";
var callback = params.callback || __empty;
var errorCallback = params.errorCallback || empty;
fs.readFile(__dirname + "/../allocate_runners.sh", function(error, file){ fs.readFile(__dirname + "/../allocate_runners.sh", function(error, file){
doapi.dropletToActive({ doapi.dropletToActive({
@ -250,11 +260,10 @@ var Worker = (function(){
data.worker_uuid = worker_uuid; data.worker_uuid = worker_uuid;
var worker = Worker.create(data); var worker = Worker.create(data);
// Just send the job over and set a timeout // wait for boot before syncing runners
// to wait before checking runners
setTimeout(function(){ setTimeout(function(){
worker.sync(args.callback); worker.sync(callback, errorCallback);
}, 90000); }, 75000);
} }
}); });
}); });
@ -278,50 +287,6 @@ var Worker = (function(){
return `#!/bin/bash\n\n${createScript} && ${makeScriptExecutable} && ${setupCrontab};`; return `#!/bin/bash\n\n${createScript} && ${makeScriptExecutable} && ${setupCrontab};`;
}; };
proto.startRunners = function(args, count){
if ( count > 3) return;
// onStart is not necessary
var worker = this;
args.stopPercent = args.stopPercent || 80;
args.callback = args.callback || __empty;
args.errorCallback = args.errorCallback || __empty;
// dont make runners on out dated workers
if(!worker || worker.settings.image > worker.image.id || worker.isBuildingRunners){
if(worker) {
console.log(`
Blocked worker(${worker.image.id}), current image ${worker.settings.image}.
Building: ${worker.isBuildingRunners}
`);
}
return;
}
worker.isBuildingRunners = true;
fs.readFile(__dirname + "/../allocate_runners.sh", function(error, file){
var command = worker.__buildCommand(file, args.stopPercent);
lxc.exec(command, worker.ip, function(data, error, stderr){
if (error) {
console.log("ERROR: ",stderr);
setTimeout(function(){
count = count || 0;
worker.startRunners(args, ++count);
}, 0);
} else {
console.log("SUCCESS: ", data , worker.ip);
}
// Just send the job over and set a timeout
// to wait before checking runners
setTimeout(function(){
worker.sync(args.callback);
// 900000 < thats 15 min not 90 sec
}, 90000);
});
});
};
return proto; return proto;
})(); })();
@ -491,7 +456,7 @@ var WorkerCollection = (function(){
}; };
workers.balance = function(){ workers.balance = function(){
console.log(`BALANCING: ${(new Date())}`);
// count workers and locate oldest worker // count workers and locate oldest worker
var oldestWorker, isNotOlder, workerCount = 0; var oldestWorker, isNotOlder, workerCount = 0;