Scheduler handles new tasks when there is free slots
This commit is contained in:
parent
604e28533c
commit
9596ac4730
@ -64,6 +64,8 @@ Scheduler.prototype.add = function(user) {
|
|||||||
this.users[user] = taskEntity;
|
this.users[user] = taskEntity;
|
||||||
this.tasksTree.insert(taskEntity);
|
this.tasksTree.insert(taskEntity);
|
||||||
|
|
||||||
|
this.emit('add');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -122,19 +124,33 @@ Scheduler.prototype.acquire = function(callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addListener() {
|
||||||
|
self.removeListener('release', releaseListener);
|
||||||
|
debug('Got a new task');
|
||||||
|
self.acquire(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
function releaseListener() {
|
||||||
|
self.removeListener('add', addListener);
|
||||||
|
debug('Slot was released');
|
||||||
|
self.acquire(callback);
|
||||||
|
}
|
||||||
|
|
||||||
debug('Trying to acquire task');
|
debug('Trying to acquire task');
|
||||||
|
|
||||||
var allRunning = self.tasks.every(is(STATUS.RUNNING));
|
var allRunning = self.tasks.every(is(STATUS.RUNNING));
|
||||||
var running = self.tasks.filter(is(STATUS.RUNNING));
|
var running = self.tasks.filter(is(STATUS.RUNNING));
|
||||||
debug('[capacity=%d, running=%d, all=%s] candidates=%j', capacity, running.length, allRunning, self.tasks);
|
debug('[capacity=%d, running=%d, all=%s] candidates=%j', capacity, running.length, allRunning, self.tasks);
|
||||||
|
|
||||||
|
if (allRunning && running.length < capacity) {
|
||||||
|
debug('Waiting for tasks');
|
||||||
|
self.once('add', addListener);
|
||||||
|
}
|
||||||
|
|
||||||
var isRunningAny = self.tasks.some(is(STATUS.RUNNING));
|
var isRunningAny = self.tasks.some(is(STATUS.RUNNING));
|
||||||
if (isRunningAny || running.length >= capacity) {
|
if (isRunningAny || running.length >= capacity) {
|
||||||
debug('Waiting for slot');
|
debug('Waiting for slot');
|
||||||
return self.once('release', function releaseListener() {
|
return self.once('release', releaseListener);
|
||||||
debug('Slot was released');
|
|
||||||
self.acquire(callback);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var candidate = self.tasksTree.min();
|
var candidate = self.tasksTree.min();
|
||||||
|
Loading…
Reference in New Issue
Block a user