2016-10-19 02:43:15 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-19 18:39:33 +08:00
|
|
|
// Inspiration from:
|
|
|
|
// - https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt
|
|
|
|
// - https://www.kernel.org/doc/Documentation/rbtree.txt
|
|
|
|
// - http://www.ibm.com/developerworks/linux/library/l-completely-fair-scheduler/
|
|
|
|
|
2016-10-19 02:43:15 +08:00
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2016-10-19 18:39:33 +08:00
|
|
|
var RBTree = require('bintrees').RBTree;
|
2016-10-19 02:43:15 +08:00
|
|
|
|
|
|
|
var debug = require('../util/debug')('scheduler');
|
|
|
|
|
|
|
|
var forever = require('../util/forever');
|
|
|
|
|
|
|
|
function Scheduler(capacity, taskRunner) {
|
|
|
|
EventEmitter.call(this);
|
2016-10-19 22:58:00 +08:00
|
|
|
debug('new Scheduler');
|
2016-10-19 02:43:15 +08:00
|
|
|
this.taskRunner = taskRunner;
|
|
|
|
this.capacity = capacity;
|
2016-10-19 16:43:24 +08:00
|
|
|
this.tasks = [];
|
2016-10-19 02:43:15 +08:00
|
|
|
this.users = {};
|
2016-10-19 18:39:33 +08:00
|
|
|
this.tasksTree = new RBTree(function(taskEntityA, taskEntityB) {
|
|
|
|
// if the user is the same it's the same entity
|
|
|
|
if (taskEntityA.user === taskEntityB.user) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// priority for entity with less executed jobs
|
|
|
|
if (taskEntityA.jobs !== taskEntityB.jobs) {
|
|
|
|
return taskEntityA.jobs - taskEntityB.jobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// priority for entity with oldest executed job
|
|
|
|
if (taskEntityA.runAt !== taskEntityB.runAt) {
|
|
|
|
return taskEntityA.runAt - taskEntityB.runAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// priority for oldest job
|
|
|
|
if (taskEntityA.createdAt !== taskEntityB.createdAt) {
|
|
|
|
return taskEntityA.createdAt - taskEntityB.createdAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't care if we arrive here
|
|
|
|
return -1;
|
|
|
|
});
|
2016-10-19 02:43:15 +08:00
|
|
|
}
|
|
|
|
util.inherits(Scheduler, EventEmitter);
|
|
|
|
|
|
|
|
module.exports = Scheduler;
|
|
|
|
|
|
|
|
Scheduler.prototype.add = function(user) {
|
|
|
|
debug('add(%s)', user);
|
2016-10-19 18:39:33 +08:00
|
|
|
var taskEntity = this.users[user];
|
|
|
|
if (taskEntity) {
|
|
|
|
if (taskEntity.status === STATUS.DONE) {
|
|
|
|
taskEntity.status = STATUS.PENDING;
|
2016-10-19 16:43:24 +08:00
|
|
|
}
|
2016-10-19 17:45:48 +08:00
|
|
|
|
|
|
|
return true;
|
2016-10-19 16:43:24 +08:00
|
|
|
} else {
|
2016-10-19 18:39:33 +08:00
|
|
|
taskEntity = new TaskEntity(user, this.tasks.length);
|
|
|
|
this.tasks.push(taskEntity);
|
|
|
|
this.users[user] = taskEntity;
|
|
|
|
this.tasksTree.insert(taskEntity);
|
2016-10-19 17:45:48 +08:00
|
|
|
|
|
|
|
return false;
|
2016-10-19 02:43:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
Scheduler.prototype.schedule = function() {
|
2016-10-19 02:43:15 +08:00
|
|
|
if (this.running) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this.running = true;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
forever(
|
|
|
|
function (next) {
|
2016-10-19 18:39:33 +08:00
|
|
|
debug('Waiting for task');
|
2016-10-19 16:43:24 +08:00
|
|
|
self.acquire(function(err, taskEntity) {
|
2016-10-19 17:46:02 +08:00
|
|
|
debug('Acquired user=%j', taskEntity);
|
2016-10-19 02:43:15 +08:00
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
if (!taskEntity) {
|
2016-10-19 02:43:15 +08:00
|
|
|
return next(new Error('all users finished'));
|
|
|
|
}
|
|
|
|
|
2016-10-19 18:39:33 +08:00
|
|
|
self.tasksTree.remove(taskEntity);
|
|
|
|
taskEntity.running();
|
2016-10-19 02:43:15 +08:00
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
debug('Running task for user=%s', taskEntity.user);
|
|
|
|
self.taskRunner.run(taskEntity.user, function(err, userQueueIsEmpty) {
|
2016-10-19 18:39:33 +08:00
|
|
|
debug('Run task=%j, done=%s', taskEntity, userQueueIsEmpty);
|
|
|
|
taskEntity.ran(userQueueIsEmpty);
|
2016-10-19 16:43:24 +08:00
|
|
|
self.release(err, taskEntity);
|
2016-10-19 02:43:15 +08:00
|
|
|
});
|
2016-10-19 18:39:33 +08:00
|
|
|
|
|
|
|
// try to acquire next user
|
|
|
|
// will block until capacity slot is available
|
|
|
|
next();
|
2016-10-19 02:43:15 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
debug('done: %s', err.message);
|
|
|
|
self.running = false;
|
|
|
|
self.emit('done');
|
2016-10-19 18:39:33 +08:00
|
|
|
self.removeAllListeners();
|
2016-10-19 02:43:15 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
return false;
|
|
|
|
};
|
2016-10-19 02:43:15 +08:00
|
|
|
|
|
|
|
Scheduler.prototype.acquire = function(callback) {
|
2016-10-19 16:43:24 +08:00
|
|
|
if (this.tasks.every(is(STATUS.DONE))) {
|
2016-10-19 02:43:15 +08:00
|
|
|
return callback(null, null);
|
|
|
|
}
|
2016-10-19 18:39:33 +08:00
|
|
|
|
2016-10-19 02:43:15 +08:00
|
|
|
var self = this;
|
|
|
|
this.capacity.getCapacity(function(err, capacity) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-10-19 18:39:33 +08:00
|
|
|
debug('Trying to acquire task');
|
|
|
|
|
|
|
|
var allRunning = self.tasks.every(is(STATUS.RUNNING));
|
2016-10-19 16:43:24 +08:00
|
|
|
var running = self.tasks.filter(is(STATUS.RUNNING));
|
2016-10-19 18:39:33 +08:00
|
|
|
debug('[capacity=%d, running=%d, all=%s] candidates=%j', capacity, running.length, allRunning, self.tasks);
|
2016-10-19 02:43:15 +08:00
|
|
|
|
2016-10-19 18:39:33 +08:00
|
|
|
var isRunningAny = self.tasks.some(is(STATUS.RUNNING));
|
|
|
|
if (isRunningAny || running.length >= capacity) {
|
|
|
|
debug('Waiting for slot');
|
|
|
|
return self.once('release', function releaseListener() {
|
2016-10-19 02:43:15 +08:00
|
|
|
debug('Slot was released');
|
|
|
|
self.acquire(callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-19 18:39:33 +08:00
|
|
|
var candidate = self.tasksTree.min();
|
2016-10-19 16:43:24 +08:00
|
|
|
|
2016-10-19 02:43:15 +08:00
|
|
|
return callback(null, candidate);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
Scheduler.prototype.release = function(err, taskEntity) {
|
|
|
|
debug('Released %j', taskEntity);
|
2016-10-19 18:39:33 +08:00
|
|
|
if (taskEntity.is(STATUS.PENDING)) {
|
|
|
|
this.tasksTree.insert(taskEntity);
|
|
|
|
}
|
2016-10-19 02:43:15 +08:00
|
|
|
this.emit('release');
|
2016-10-19 16:43:24 +08:00
|
|
|
};
|
|
|
|
|
2016-10-19 16:45:37 +08:00
|
|
|
|
|
|
|
/* Task entities */
|
|
|
|
|
|
|
|
var STATUS = {
|
|
|
|
PENDING: 'pending',
|
|
|
|
RUNNING: 'running',
|
|
|
|
DONE: 'done'
|
|
|
|
};
|
|
|
|
|
2016-10-19 18:39:33 +08:00
|
|
|
function TaskEntity(user, createdAt) {
|
2016-10-19 16:43:24 +08:00
|
|
|
this.user = user;
|
2016-10-19 18:39:33 +08:00
|
|
|
this.createdAt = createdAt;
|
2016-10-19 16:43:24 +08:00
|
|
|
this.status = STATUS.PENDING;
|
|
|
|
this.jobs = 0;
|
2016-10-19 18:39:33 +08:00
|
|
|
this.runAt = 0;
|
2016-10-19 16:43:24 +08:00
|
|
|
}
|
2016-10-19 02:43:15 +08:00
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
TaskEntity.prototype.is = function(status) {
|
|
|
|
return this.status === status;
|
2016-10-19 02:43:15 +08:00
|
|
|
};
|
2016-10-19 16:43:24 +08:00
|
|
|
|
2016-10-19 18:39:33 +08:00
|
|
|
TaskEntity.prototype.running = function() {
|
|
|
|
this.status = STATUS.RUNNING;
|
|
|
|
this.runAt = Date.now();
|
|
|
|
};
|
|
|
|
|
|
|
|
TaskEntity.prototype.ran = function(userQueueIsEmpty) {
|
|
|
|
this.jobs++;
|
|
|
|
this.status = userQueueIsEmpty ? STATUS.DONE : STATUS.PENDING;
|
|
|
|
};
|
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
function is(status) {
|
|
|
|
return function(taskEntity) {
|
|
|
|
return taskEntity.is(status);
|
|
|
|
};
|
|
|
|
}
|