Merge pull request #134 from vatesfr/abhamonr-backup-jobs-notifications

Xo event `job:terminated` on job termination.
This commit is contained in:
Julien Fontanet
2015-11-26 15:29:39 +01:00
2 changed files with 26 additions and 4 deletions

View File

@@ -61,7 +61,9 @@ export default class JobExecutor {
try {
if (job.type === 'call') {
await this._execCall(job, runJobId)
const execStatus = await this._execCall(job, runJobId)
this.xo.emit('job:terminated', execStatus)
} else {
throw new UnsupportedJobType(job)
}
@@ -93,6 +95,12 @@ export default class JobExecutor {
connection.set('user_id', job.userId)
const execStatus = {
runJobId,
start: Date.now(),
calls: {}
}
forEach(paramsFlatVector, params => {
const runCallId = this._logger.notice(`Starting ${job.method} call. (${job.id})`, {
event: 'jobCall.start',
@@ -101,6 +109,12 @@ export default class JobExecutor {
params
})
const call = execStatus.calls[runCallId] = {
method: job.method,
params,
start: Date.now()
}
promises.push(
this.xo.api.call(connection, job.method, assign({}, params)).then(
value => {
@@ -110,6 +124,9 @@ export default class JobExecutor {
runCallId,
returnedValue: value
})
call.returnedValue = value
call.end = Date.now()
},
reason => {
this._logger.notice(`Call ${job.method} (${runCallId}) has failed. (${job.id})`, {
@@ -118,13 +135,18 @@ export default class JobExecutor {
runCallId,
error: reason
})
call.error = reason
call.end = Date.now()
}
)
)
})
connection.close()
await Promise.all(promises)
execStatus.end = Date.now()
return execStatus
}
}

View File

@@ -1532,9 +1532,9 @@ export default class Xo extends EventEmitter {
// For security, prevent from accessing `this`.
if (isFunction(value)) {
value = function () {
value = (value => function () {
return value.apply(null, arguments)
}
})(value)
}
Object.defineProperty(this, name, {