running detect

This commit is contained in:
2018-09-29 14:44:15 +08:00
parent 7e782c633a
commit 69ea3cf69b

View File

@ -2,6 +2,7 @@ class Extractor {
constructor() { constructor() {
this._tasks = []; this._tasks = [];
this._tab = undefined; this._tab = undefined;
this._running = false;
this._results = {}; this._results = {};
} }
/** /**
@ -35,6 +36,10 @@ class Extractor {
* Start the task chain. * Start the task chain.
*/ */
async start() { async start() {
if (this._running) {
console.log('The Extractor is running. Please wait..');
return;
}
if (!this._tasks.length) { if (!this._tasks.length) {
console.log('No task to run.'); console.log('No task to run.');
return; return;
@ -47,6 +52,7 @@ class Extractor {
} else { } else {
this._tab = await getActiveTab(false); this._tab = await getActiveTab(false);
} }
this._running = true;
return this._tasks.reduce((pms, args, i, tasks) => { return this._tasks.reduce((pms, args, i, tasks) => {
return pms.then( return pms.then(
result => { result => {
@ -57,15 +63,23 @@ class Extractor {
}, Promise.resolve(undefined)).then( }, Promise.resolve(undefined)).then(
result => { result => {
this._results[this._tasks[this._tasks.length - 1]] = result; this._results[this._tasks[this._tasks.length - 1]] = result;
this._running = false;
this.save(); this.save();
} }
).catch(err => console.log(err)); ).catch(err => {
this._running = false;
console.log(err)
});
} }
/** /**
* restart from specified task, but don't restart the previous tasks. * restart from specified task, but don't restart the previous tasks.
* @param {number} taskid from which restart the tasks * @param {number} taskid from which restart the tasks
*/ */
async restart(taskid) { async restart(taskid) {
if (this._running) {
console.log('The Extractor is running. Please wait..');
return;
}
taskid = this._checkTaskId(taskid, 1); taskid = this._checkTaskId(taskid, 1);
if (!taskid) return; if (!taskid) return;
if (taskid == 1) { if (taskid == 1) {
@ -77,6 +91,7 @@ class Extractor {
console.log(`No result cache for task (id ${taskid}). \nMake sure call ".start()" before ".restart()"?`); console.log(`No result cache for task (id ${taskid}). \nMake sure call ".start()" before ".restart()"?`);
return; return;
} }
this._running = true;
this._tab = await createTab(parseUrls(cache)[0], false) this._tab = await createTab(parseUrls(cache)[0], false)
return this._tasks.slice(taskid - 1).reduce((pms, args, i, tasks) => { return this._tasks.slice(taskid - 1).reduce((pms, args, i, tasks) => {
return pms.then( return pms.then(
@ -87,9 +102,13 @@ class Extractor {
}, Promise.resolve(cache)).then( }, Promise.resolve(cache)).then(
result => { result => {
this._results[this._tasks[this._tasks.length - 1]] = result; this._results[this._tasks[this._tasks.length - 1]] = result;
this._running = false;
this.save(); this.save();
} }
).catch(err => console.log(err)); ).catch(err => {
this._running = false;
console.log(err)
});
} }
/** /**
* Save result of a task * Save result of a task