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');
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function Scheduler (capacity, taskRunner) {
|
2016-10-19 02:43:15 +08:00
|
|
|
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 = {};
|
2019-12-24 01:19:08 +08:00
|
|
|
this.tasksTree = new RBTree(function (taskEntityA, taskEntityB) {
|
2016-10-19 18:39:33 +08:00
|
|
|
// 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 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;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
Scheduler.prototype.add = function (user) {
|
2016-10-19 02:43:15 +08:00
|
|
|
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-21 17:42:27 +08:00
|
|
|
this.tasksTree.insert(taskEntity);
|
|
|
|
this.emit('add');
|
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
|
|
|
|
2016-10-19 22:59:27 +08:00
|
|
|
this.emit('add');
|
|
|
|
|
2016-10-19 17:45:48 +08:00
|
|
|
return false;
|
2016-10-19 02:43:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +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');
|
2019-12-24 01:19:08 +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);
|
2019-12-24 01:19:08 +08:00
|
|
|
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
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
Scheduler.prototype.acquire = function (callback) {
|
2016-10-21 05:47:39 +08:00
|
|
|
this.removeAllListeners('add');
|
|
|
|
this.removeAllListeners('release');
|
|
|
|
|
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;
|
2019-12-24 01:19:08 +08:00
|
|
|
this.capacity.getCapacity(function (err, capacity) {
|
2016-10-19 02:43:15 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:47:39 +08:00
|
|
|
debug('Trying to acquire task');
|
|
|
|
var running = self.tasks.filter(is(STATUS.RUNNING));
|
|
|
|
debug('[capacity=%d, running=%d] candidates=%j', capacity, running.length, self.tasks);
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
self.once('add', function () {
|
2016-10-19 22:59:27 +08:00
|
|
|
debug('Got a new task');
|
|
|
|
self.acquire(callback);
|
2016-10-21 05:47:39 +08:00
|
|
|
});
|
2019-12-24 01:19:08 +08:00
|
|
|
self.once('release', function () {
|
2016-10-19 22:59:27 +08:00
|
|
|
debug('Slot was released');
|
|
|
|
self.acquire(callback);
|
2016-10-21 05:47:39 +08:00
|
|
|
});
|
2016-10-19 22:59:27 +08:00
|
|
|
|
2016-10-21 02:16:57 +08:00
|
|
|
if (running.length >= capacity) {
|
2016-10-21 05:47:39 +08:00
|
|
|
debug('Not enough capacity');
|
|
|
|
return null;
|
2016-10-19 02:43:15 +08:00
|
|
|
}
|
|
|
|
|
2016-10-21 02:16:57 +08:00
|
|
|
var isRunningAny = self.tasks.some(is(STATUS.RUNNING));
|
2016-10-19 18:39:33 +08:00
|
|
|
var candidate = self.tasksTree.min();
|
2016-10-21 02:16:57 +08:00
|
|
|
if (isRunningAny && candidate === null) {
|
|
|
|
debug('Waiting for last task to finish');
|
2016-10-21 05:47:39 +08:00
|
|
|
return null;
|
2016-10-21 02:16:57 +08:00
|
|
|
}
|
2016-10-19 16:43:24 +08:00
|
|
|
|
2016-10-21 17:42:27 +08:00
|
|
|
if (candidate) {
|
|
|
|
self.emit('acquired', candidate.user);
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:43:15 +08:00
|
|
|
return callback(null, candidate);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
Scheduler.prototype.release = function (err, taskEntity) {
|
2016-10-19 16:43:24 +08:00
|
|
|
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'
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +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 02:43:15 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
TaskEntity.prototype.is = function (status) {
|
2016-10-19 16:43:24 +08:00
|
|
|
return this.status === status;
|
2016-10-19 02:43:15 +08:00
|
|
|
};
|
2016-10-19 16:43:24 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
TaskEntity.prototype.running = function () {
|
2016-10-19 18:39:33 +08:00
|
|
|
this.status = STATUS.RUNNING;
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
TaskEntity.prototype.ran = function (userQueueIsEmpty) {
|
2016-10-19 18:39:33 +08:00
|
|
|
this.jobs++;
|
|
|
|
this.status = userQueueIsEmpty ? STATUS.DONE : STATUS.PENDING;
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function is (status) {
|
|
|
|
return function (taskEntity) {
|
2016-10-19 16:43:24 +08:00
|
|
|
return taskEntity.is(status);
|
|
|
|
};
|
|
|
|
}
|