Skip to content

Instantly share code, notes, and snippets.

@ragar90
Last active August 29, 2015 14:13
Show Gist options
  • Save ragar90/facf01d2d50df06e6379 to your computer and use it in GitHub Desktop.
Save ragar90/facf01d2d50df06e6379 to your computer and use it in GitHub Desktop.
function _cancelQueuedTasks(configs, tasks, callback){
var worker = new ironworker.Client({token: config.IRON_WORKER_TOKEN, project_id: config.IRON_WORKER_PROJECT_ID});
newerTasks = tasks.slice(2, tasks.length);
log(newerTasks.length + " newerTasks are going to be deleted")
async.eachSeries(newerTasks, function(task, cb){
log("Canceling task: " + task.id + " [RMCaseJobScheduler]")
worker.tasksCancel(task.id,function(error, results){
log("=======Results======")
log(JSON.stringify(results))
log("======Errors========")
log(JSON.stringify(error))
if(error){
cb(error);
}
else{
log("Task " + task.id + "was " + results.msg)
cb()
}
});
}, function(error){
callback(error);
})
callback()
}
function _getQueuedIronWorkerTasks(configs, callback){
log("Getting Queued Tasks.....");
if (!configs || !configs.max_queue_length) {
callback(new Error('Invalid Configuration'));
}
else {
maxQueueLength = configs.max_queue_length;
var worker = new ironworker.Client({token: config.IRON_WORKER_TOKEN, project_id: config.IRON_WORKER_PROJECT_ID});
worker.tasksList({queued: 1, code_name: "RMCaseJobScheduler"},function(error, tasks){
log(tasks.length + " tasks were returned.")
if (!error) {
if(tasks instanceof Array && tasks.length > maxQueueLength){
//The oldest task will be the first elements of the array
tasks.sort(function(t1,t2){
d1 = new Date(t1.created_at);
d2 = new Date(t2.created_at);
return d2 - d1;
});
}
}
callback(error, tasks);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment