jsreport-scheduling
Advanced tools
Comparing version 2.1.0 to 2.1.1
const parseCron = require('./parseCron') | ||
module.exports = class JobProcessor { | ||
constructor ({ | ||
beforeProcessJobListeners, | ||
executionHandler, | ||
documentStore, | ||
logger, | ||
Request, | ||
TaskType, | ||
options | ||
}) { | ||
constructor ({ beforeProcessJobListeners, executionHandler, documentStore, logger, Request, TaskType, options }) { | ||
if (!options.taskPingTimeout) { | ||
@@ -26,5 +18,7 @@ options.taskPingTimeout = 2 * options.interval | ||
options.now = options.now || function () { | ||
return new Date() | ||
} | ||
options.now = | ||
options.now || | ||
function () { | ||
return new Date() | ||
} | ||
} | ||
@@ -53,6 +47,8 @@ | ||
const recoveryPromise = Promise.all(tasks.map((task) => { | ||
this.logger.info('Recovering task ' + task.schedule.name) | ||
return this.processOne(task.schedule, task) | ||
})) | ||
const recoveryPromise = Promise.all( | ||
tasks.map(task => { | ||
this.logger.info('Recovering task ' + task.schedule.name) | ||
return this.processOne(task.schedule, task) | ||
}) | ||
) | ||
@@ -64,3 +60,3 @@ if (options.waitForJobToFinish) { | ||
const schedules = await this.documentStore.collection('schedules').find(this._schedulesToProcessFilter()) | ||
const processPromise = Promise.all(schedules.map((s) => this.processOne(s, null))) | ||
const processPromise = Promise.all(schedules.map(s => this.processOne(s, null))) | ||
@@ -72,3 +68,2 @@ if (options.waitForJobToFinish) { | ||
this.logger.error('unable to load planned schedules ' + e.stack) | ||
throw e | ||
} | ||
@@ -79,3 +74,3 @@ } | ||
return { | ||
$and: [{nextRun: {$lte: this.options.now()}}, {state: 'planned'}, {enabled: true}] | ||
$and: [{ nextRun: { $lte: this.options.now() } }, { state: 'planned' }, { enabled: true }] | ||
} | ||
@@ -86,3 +81,3 @@ } | ||
return { | ||
$and: [{ping: {$lt: new Date(this.options.now().getTime() - this.options.taskPingTimeout)}}, {state: 'running'}] | ||
$and: [{ ping: { $lt: new Date(this.options.now().getTime() - this.options.taskPingTimeout) } }, { state: 'running' }] | ||
} | ||
@@ -92,4 +87,4 @@ } | ||
_pingRunningTasks () { | ||
const ids = this.currentlyRunningTasks.map((t) => t._id) | ||
return this.documentStore.collection('tasks').update({_id: {$in: ids}}, {$set: {ping: this.options.now()}}) | ||
const ids = this.currentlyRunningTasks.map(t => t._id) | ||
return this.documentStore.collection('tasks').update({ _id: { $in: ids } }, { $set: { ping: this.options.now() } }) | ||
} | ||
@@ -106,8 +101,8 @@ | ||
tasks.forEach((t) => { | ||
t.schedule = schedules.find((s) => s.shortid === t.scheduleShortid) | ||
tasks.forEach(t => { | ||
t.schedule = schedules.find(s => s.shortid === t.scheduleShortid) | ||
}) | ||
// someone could have deleted schedule or disable the schedule, ignore these tasks | ||
return tasks.filter((t) => { | ||
return tasks.filter(t => { | ||
let result = t.schedule != null | ||
@@ -128,7 +123,9 @@ | ||
const req = this.Request ? this.Request({ | ||
context: { | ||
skipAuthorization: true | ||
} | ||
}) : undefined | ||
const req = this.Request | ||
? this.Request({ | ||
context: { | ||
skipAuthorization: true | ||
} | ||
}) | ||
: undefined | ||
@@ -138,6 +135,10 @@ await this.beforeProcessJobListeners.fire(schedule, task, req) | ||
try { | ||
const count = await this.documentStore.collection('schedules').update({ | ||
_id: schedule._id, | ||
state: 'planned' | ||
}, { $set: { state: 'planning' } }, req) | ||
const count = await this.documentStore.collection('schedules').update( | ||
{ | ||
_id: schedule._id, | ||
state: 'planned' | ||
}, | ||
{ $set: { state: 'planning' } }, | ||
req | ||
) | ||
@@ -182,15 +183,22 @@ if (count === 1) { | ||
await this.documentStore.collection('schedules').update({ | ||
_id: schedule._id | ||
}, { | ||
$set: { | ||
state: 'planned', | ||
nextRun: new Date(nextRun.getTime()) | ||
} | ||
}, req) | ||
await this.documentStore.collection('schedules').update( | ||
{ | ||
_id: schedule._id | ||
}, | ||
{ | ||
$set: { | ||
state: 'planned', | ||
nextRun: new Date(nextRun.getTime()) | ||
} | ||
}, | ||
req | ||
) | ||
} | ||
const templates = await this.documentStore.collection('templates').find({ | ||
shortid: schedule.templateShortid | ||
}, req) | ||
const templates = await this.documentStore.collection('templates').find( | ||
{ | ||
shortid: schedule.templateShortid | ||
}, | ||
req | ||
) | ||
@@ -202,10 +210,14 @@ if (templates.length > 0) { | ||
await this.documentStore.collection('tasks').update({ | ||
_id: task._id | ||
}, { | ||
$set: { | ||
state: 'success', | ||
finishDate: this.options.now() | ||
} | ||
}, req) | ||
await this.documentStore.collection('tasks').update( | ||
{ | ||
_id: task._id | ||
}, | ||
{ | ||
$set: { | ||
state: 'success', | ||
finishDate: this.options.now() | ||
} | ||
}, | ||
req | ||
) | ||
@@ -217,27 +229,35 @@ return | ||
await this.documentStore.collection('tasks').update({ | ||
_id: task._id | ||
}, { | ||
$set: { | ||
state: 'error', | ||
error: 'Template shortid:' + schedule.templateShortid + ' was not found', | ||
finishDate: this.options.now() | ||
} | ||
}, req) | ||
await this.documentStore.collection('tasks').update( | ||
{ | ||
_id: task._id | ||
}, | ||
{ | ||
$set: { | ||
state: 'error', | ||
error: 'Template shortid:' + schedule.templateShortid + ' was not found', | ||
finishDate: this.options.now() | ||
} | ||
}, | ||
req | ||
) | ||
} catch (e) { | ||
this.logger.debug(`Processing schedule ${schedule.name} failed with : ${e.stack}`) | ||
await this.documentStore.collection('tasks').update({ | ||
_id: task._id | ||
}, { | ||
$set: { | ||
state: e.canceled ? 'canceled' : 'error', | ||
error: e.stack, | ||
finishDate: this.options.now() | ||
} | ||
}, req) | ||
await this.documentStore.collection('tasks').update( | ||
{ | ||
_id: task._id | ||
}, | ||
{ | ||
$set: { | ||
state: e.canceled ? 'canceled' : 'error', | ||
error: e.stack, | ||
finishDate: this.options.now() | ||
} | ||
}, | ||
req | ||
) | ||
} finally { | ||
this.currentlyRunningTasks = this.currentlyRunningTasks.filter((t) => t._id !== task._id) | ||
this.currentlyRunningTasks = this.currentlyRunningTasks.filter(t => t._id !== task._id) | ||
} | ||
} | ||
} |
{ | ||
"name": "jsreport-scheduling", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "jsreport extension for scheduling background rendering jobs", | ||
@@ -48,6 +48,6 @@ "scripts": { | ||
"jsreport-authorization": "2.2.4", | ||
"jsreport-core": "2.6.1", | ||
"jsreport-core": "2.6.2", | ||
"jsreport-reports": "2.2.0", | ||
"jsreport-studio-dev": "1.5.0", | ||
"jsreport-templates": "2.3.1", | ||
"jsreport-templates": "2.3.2", | ||
"lodash": "4.17.15", | ||
@@ -54,0 +54,0 @@ "mocha": "5.2.0", |
281103
3823