2016-10-19 02:43:15 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
|
|
|
var debug = require('../util/debug')('scheduler');
|
|
|
|
|
|
|
|
var forever = require('../util/forever');
|
|
|
|
|
|
|
|
function Scheduler(capacity, taskRunner) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
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 = {};
|
|
|
|
}
|
|
|
|
util.inherits(Scheduler, EventEmitter);
|
|
|
|
|
|
|
|
module.exports = Scheduler;
|
|
|
|
|
|
|
|
Scheduler.prototype.add = function(user) {
|
|
|
|
debug('add(%s)', user);
|
2016-10-19 16:43:24 +08:00
|
|
|
var task = this.users[user];
|
|
|
|
if (task) {
|
|
|
|
if (task.status === STATUS.DONE) {
|
|
|
|
task.status = STATUS.PENDING;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
task = new TaskEntity(user);
|
|
|
|
this.tasks.push(task);
|
|
|
|
this.users[user] = task;
|
2016-10-19 02:43:15 +08:00
|
|
|
}
|
2016-10-19 16:43:24 +08:00
|
|
|
return this.schedule();
|
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) {
|
|
|
|
debug('Trying to acquire user');
|
2016-10-19 16:43:24 +08:00
|
|
|
self.acquire(function(err, taskEntity) {
|
|
|
|
debug('Acquired user=%s', 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 16:43:24 +08:00
|
|
|
taskEntity.status = STATUS.RUNNING;
|
2016-10-19 02:43:15 +08:00
|
|
|
// try to acquire next user
|
|
|
|
// will block until capacity slow is available
|
|
|
|
next();
|
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
debug('Running task for user=%s', taskEntity.user);
|
|
|
|
self.taskRunner.run(taskEntity.user, function(err, userQueueIsEmpty) {
|
|
|
|
taskEntity.status = userQueueIsEmpty ? STATUS.DONE : STATUS.PENDING;
|
|
|
|
|
|
|
|
self.release(err, taskEntity);
|
2016-10-19 02:43:15 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
debug('done: %s', err.message);
|
|
|
|
self.running = false;
|
|
|
|
self.emit('done');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
var self = this;
|
|
|
|
this.capacity.getCapacity(function(err, capacity) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
var running = self.tasks.filter(is(STATUS.RUNNING));
|
2016-10-19 02:43:15 +08:00
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
debug('Trying to acquire users=%j, running=%d, capacity=%d', self.tasks, running.length, capacity);
|
|
|
|
var allUsersRunning = self.tasks.every(is(STATUS.RUNNING));
|
2016-10-19 02:43:15 +08:00
|
|
|
if (running.length >= capacity || allUsersRunning) {
|
|
|
|
debug(
|
|
|
|
'Waiting for slot. capacity=%s, running=%s, all_running=%s',
|
|
|
|
capacity, running.length, allUsersRunning
|
|
|
|
);
|
|
|
|
return self.once('release', function() {
|
|
|
|
debug('Slot was released');
|
|
|
|
self.acquire(callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-19 16:43:24 +08:00
|
|
|
var candidate = self.tasks.filter(is(STATUS.PENDING))[0];
|
|
|
|
|
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);
|
|
|
|
// decide what to do based on status/jobs
|
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 16:43:24 +08:00
|
|
|
function TaskEntity(user) {
|
|
|
|
this.user = user;
|
|
|
|
this.status = STATUS.PENDING;
|
|
|
|
this.jobs = 0;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
function is(status) {
|
|
|
|
return function(taskEntity) {
|
|
|
|
return taskEntity.is(status);
|
|
|
|
};
|
|
|
|
}
|