@coya/task-manager
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -61,2 +61,3 @@ "use strict"; | ||
this._logs = new Logs('task_manager'); | ||
this._isEnded = false; | ||
} | ||
@@ -69,8 +70,9 @@ get logs() { | ||
} | ||
/*** SYNCHRONOUS SECTION ***/ | ||
processSynchronousTasks(tasks) { | ||
if (tasks) | ||
this.tasks = tasks; | ||
if (!this.tasks.length) | ||
this._tasks = tasks; | ||
if (!this._tasks.length) | ||
return this._end(); | ||
let timeDiff = this.tasks[0].nextExecutionTime - Date.now(); | ||
let timeDiff = this._tasks[0].nextExecutionTime - Date.now(); | ||
if (timeDiff > 0) { | ||
@@ -81,3 +83,3 @@ this.logs.info('Waiting ' + timeDiff + ' milliseconds until the next execution...'); | ||
} | ||
let task = this.tasks.shift(); // take out the task from the array | ||
let task = this._tasks.shift(); // take out the task from the array | ||
return task.run() | ||
@@ -89,3 +91,3 @@ .then((result) => { | ||
else if (result && result.stopAll) { | ||
this.tasks = []; | ||
this._tasks = []; | ||
this.logs.info('All tasks have been stopped and removed from the tasks list.'); | ||
@@ -102,3 +104,3 @@ } | ||
else if (error && error.stopAll) { | ||
this.tasks = []; | ||
this._tasks = []; | ||
this.logs.error('All tasks have been stopped and removed from the tasks list.'); | ||
@@ -114,21 +116,21 @@ } | ||
task.nextExecutionTime = nextExecutionTime; | ||
for (let i = 0; i < this.tasks.length; ++i) { | ||
if (this.tasks[i].nextExecutionTime > nextExecutionTime) { | ||
this.tasks.splice(i, 0, task); | ||
for (let i = 0; i < this._tasks.length; ++i) { | ||
if (this._tasks[i].nextExecutionTime > nextExecutionTime) { | ||
this._tasks.splice(i, 0, task); | ||
return; | ||
} | ||
} | ||
this.tasks.push(task); // reinsertion at the end | ||
this._tasks.push(task); // reinsertion at the end | ||
} | ||
/*** ASYNCHRONOUS SECTION ***/ | ||
processAsynchronousTasks(tasks) { | ||
this.tasks = tasks; | ||
if (!this.tasks || this.tasks.length == 0) | ||
this._tasks = tasks; | ||
if (!this._tasks || this._tasks.length == 0) | ||
return Promise.resolve(); | ||
return new Promise(function (resolve, reject) { | ||
for (let task of this.tasks) | ||
this.taskLoop(task, this._end); | ||
for (let task of this._tasks) | ||
this.taskLoop(task); | ||
}.bind(this)); | ||
} | ||
taskLoop(task, end) { | ||
taskLoop(task) { | ||
task.run() | ||
@@ -138,21 +140,13 @@ .then((result) => { | ||
if (result && result.stop) { | ||
this.tasks.splice(this.tasks.indexOf(task), 1); | ||
this._tasks.splice(this._tasks.indexOf(task), 1); | ||
this.logs.info('Task "' + task.name + '" stopped and removed from the tasks list.'); | ||
if (this.tasks.length == 0) | ||
end(); | ||
if (this._tasks.length == 0) | ||
this.endTaskManager(); | ||
} | ||
else if (result && result.stopAll) { | ||
this.tasks.forEach((task) => { | ||
task.cancel(); | ||
}); | ||
this.tasks = []; | ||
this.logs.info('All tasks have been stopped and removed from the tasks list.'); | ||
end(); | ||
this.cancelAllTasks(); | ||
this.endTaskManager(); | ||
} | ||
else { | ||
const nextExecutionTime = task.timeInterval * 1000; | ||
task.nextExecutionTime = nextExecutionTime; // useless actually | ||
task.timeout = setTimeout(this.taskLoop.bind(this, task, end), nextExecutionTime); | ||
this.logs.info('Task "' + task.name + '" : ' + nextExecutionTime + ' milliseconds until the next execution...'); | ||
} | ||
else | ||
this.scheduleTask(task); | ||
}) | ||
@@ -162,24 +156,36 @@ .catch((error) => { | ||
if ((error && error.stop) || task.failedExecutionsInARow >= task.maxFailuresInARow) { | ||
this.tasks.splice(this.tasks.indexOf(task), 1); | ||
this.logs.error('Task "' + task.name + '" removed from the tasks list.'); | ||
if (this.tasks.length == 0) | ||
end(); | ||
this._tasks.splice(this._tasks.indexOf(task), 1); | ||
this.logs.error('Task "' + task.name + '" stopped and removed from the tasks list.'); | ||
if (this._tasks.length == 0) | ||
this.endTaskManager(); | ||
} | ||
else if (error && error.stopAll) { | ||
this.tasks.forEach((task) => { | ||
task.cancel(); | ||
}); | ||
this.tasks = []; | ||
this.logs.error('All tasks have been stopped and removed from the tasks list.'); | ||
end(); | ||
this.cancelAllTasks(true); | ||
this.endTaskManager(); | ||
} | ||
else { | ||
const nextExecutionTime = task.timeInterval * 1000; | ||
task.nextExecutionTime = nextExecutionTime; // useless actually | ||
task.timeout = setTimeout(this.taskLoop.bind(this, task, end), nextExecutionTime); | ||
this.logs.info('Task "' + task.name + '" : ' + nextExecutionTime + ' milliseconds until the next execution...'); | ||
} | ||
else | ||
this.scheduleTask(task); | ||
}); | ||
} | ||
scheduleTask(task) { | ||
const nextExecutionTime = task.timeInterval * 1000; | ||
task.nextExecutionTime = nextExecutionTime; // useless actually | ||
task.timeout = setTimeout(this.taskLoop.bind(this, task), nextExecutionTime); | ||
this.logs.info('Task "' + task.name + '" : ' + nextExecutionTime + ' milliseconds until the next execution...'); | ||
} | ||
endTaskManager() { | ||
if (!this._isEnded) { | ||
this._isEnded = true; | ||
if (this._end) | ||
this._end(); | ||
} | ||
} | ||
cancelAllTasks(isError) { | ||
this._tasks.forEach((task) => { | ||
task.cancel(); | ||
}); | ||
this._tasks = []; | ||
(isError ? this.logs.error : this.logs.info)('All tasks have been cancelled and removed from the tasks list.'); | ||
} | ||
} | ||
exports.TaskManager = TaskManager; |
{ | ||
"name": "@coya/task-manager", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Scheduler for run tasks successively or simultaneously", | ||
@@ -5,0 +5,0 @@ "main": "js/TaskManager.js", |
183
9813