Skip to content

Instantly share code, notes, and snippets.

@pbruna
Created September 6, 2016 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbruna/dc429585d1833b1189a9498efe9fd875 to your computer and use it in GitHub Desktop.
Save pbruna/dc429585d1833b1189a9498efe9fd875 to your computer and use it in GitHub Desktop.
var exec = require('child_process').exec;
var hash = {
'A': {els: [1,2], result: 0},
'B': {els: [1,2,3,4,5], result: 0},
'C': {els: [1,2,3,4,5], result: 0},
'D': {els: [1,4,5], result: 0},
}
var COMANDOS = [
{
'name': 'imapsync',
'command': "a=$(( ( RANDOM % 5 ) + 1)); sleep $a; echo \"imapsync-$a-"
},
{
'name': 'moveBlobs',
'command': "a=$(( ( RANDOM % 5 ) + 1)); sleep $a; echo \"moveBlobs-$a-"
},
{
'name': 'backup',
'command': "a=$(( ( RANDOM % 5 ) + 1)); sleep $a; echo \"backup-$a-"
}
]
var runCommand = function(server, mailboxIndex, commandIndex) {
command = COMANDOS[commandIndex];
if (!command) {
mailboxIndex++;
return runCommand(server, mailboxIndex, 0);
}
if (!hash[server].els[mailboxIndex]) {
console.log('NADA + para ' + server + '. Envio Email');
return true;
}
var line = server + '-' + mailboxIndex + "\"";
var comandoFull = command.command + line;
exec(comandoFull, (e, stdout, stderr) => {
printLog(stdout);
commandIndex++;
runCommand(server, mailboxIndex, commandIndex);
printLine();
})
}
var printLog = function(stdout) {
var comandos = {'imapsync': 0, 'moveBlobs': 1, 'backup': 2}
var time = Math.floor(Date.now() / 1000);
var command = stdout.trim().split('-')[0];
var sleep = stdout.trim().split('-')[1];
var server = stdout.trim().split('-')[2];
var mailboxIndex = stdout.trim().split('-')[3];
var commandIndex = comandos[command];
hash[server].result++;
var log = `${command}-${server}-${mailboxIndex}-${time}`;
console.log(log);
}
var letras = ['A', 'B', 'C', 'D'];
letras.forEach((l) => {
runCommand(l, 0)
})
var printLine = function() {
var total = 0;
Object.keys(hash).forEach((k) => {
total += hash[k].result
});
if (total === 0) return true;
if ((total % 4 === 0)) console.log('-----------');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment