Skip to content

Commit ae3ae90

Browse files
committed
[jobs] Remove jobs from queue in 55 min timeout, allowing others to start running
1 parent f6765c9 commit ae3ae90

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

api/parts/jobs/manager.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const JOB = require('./job.js'),
1212
later = require('later');
1313

1414
const DELAY_BETWEEN_CHECKS = 1000,
15-
MAXIMUM_IN_LINE_JOBS_PER_NAME = 10;
15+
MAXIMUM_IN_LINE_JOBS_PER_NAME = 10,
16+
THE_VERY_TOP_TIMEOUT = 1000 * 60 * 55; // 55min
1617

1718
/**
1819
* Manager obviously manages jobs running: monitors jobs collection & IPC messages, runs jobs dividing then if necessary, starts and manages
@@ -388,26 +389,29 @@ class Manager {
388389
return new Promise((resolve, reject) => {
389390
job.prepare(this, this.db).then(() => {
390391
log.d('prepared %j', job.id);
391-
this.run(job).then((upd) => {
392-
log.d('result in start, %j', upd);
393392

394-
let idx = this.running[job.name].indexOf('' + job._id);
395-
if (idx !== -1) {
396-
this.running[job.name].splice(idx, 1);
397-
}
398-
log.d('running in start: %j', this.running[job.name]);
393+
/**
394+
* Remove job from master queue allowing other jobs to step in
395+
*/
396+
let clear = () => {
397+
let idx = this.running[job.name].indexOf('' + job._id);
398+
if (idx !== -1) {
399+
this.running[job.name].splice(idx, 1);
400+
}
401+
log.d('cleared manager from job, %s:%s, still running %j', job.name, job.id, this.running[job.name]);
402+
this.schedule(job).catch(e => log.e.bind(log, 'Error when clearing job: %j', e));
403+
},
404+
timeout = setTimeout(clear, THE_VERY_TOP_TIMEOUT);
399405

400-
this.schedule(job);
406+
this.run(job).then((upd) => {
407+
log.d('result in start, %j', upd);
408+
clearTimeout(timeout);
409+
clear();
401410
resolve(upd ? upd.result : undefined);
402411
}, (error) => {
403412
log.d('error in start, %j', error.message || error.code || error.stack || error);
404-
405-
let idx = this.running[job.name].indexOf('' + job._id);
406-
if (idx !== -1) {
407-
this.running[job.name].splice(idx, 1);
408-
}
409-
410-
this.schedule(job);
413+
clearTimeout(timeout);
414+
clear();
411415
reject(error);
412416
});
413417
}, e => {

0 commit comments

Comments
 (0)