From 46969a2ca21e6a568e3134eb704823953cf3acca Mon Sep 17 00:00:00 2001 From: Jakub Ptak Date: Tue, 5 Feb 2019 14:40:50 +0100 Subject: [PATCH] Fixed this={} in processDbResult --- lib/agenda/save-job.js | 70 +++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lib/agenda/save-job.js b/lib/agenda/save-job.js index defd7e911..0414b14ce 100644 --- a/lib/agenda/save-job.js +++ b/lib/agenda/save-job.js @@ -2,41 +2,6 @@ const debug = require('debug')('agenda:saveJob'); const {processJobs} = require('../utils'); -/** - * Given a result for findOneAndUpdate() or insert() above, determine whether to process - * the job immediately or to let the processJobs() interval pick it up later - * @param {Error} err error passed in via MongoDB call as to whether modify call failed or passed - * @param {*} result the data returned from the findOneAndUpdate() call or insertOne() call - * @access private - * @returns {undefined} - */ -const processDbResult = (job, result) => { - debug('processDbResult() called with success, checking whether to process job immediately or not'); - - // We have a result from the above calls - // findOneAndUpdate() returns different results than insertOne() so check for that - let res = result.ops ? result.ops : result.value; - if (res) { - // If it is an array, grab the first job - if (Array.isArray(res)) { - res = res[0]; - } - - // Grab ID and nextRunAt from MongoDB and store it as an attribute on Job - job.attrs._id = res._id; - job.attrs.nextRunAt = res.nextRunAt; - - // If the current job would have been processed in an older scan, process the job immediately - if (job.attrs.nextRunAt && job.attrs.nextRunAt < this._nextScanAt) { - debug('[%s:%s] job would have ran by nextScanAt, processing the job immediately', job.attrs.name, res._id); - processJobs.call(this, job); - } - } - - // Return the Job instance - return job; -}; - /** * Save the properties on a job to MongoDB * @name Agenda#saveJob @@ -45,6 +10,41 @@ const processDbResult = (job, result) => { * @returns {Promise} resolves when job is saved or errors */ module.exports = async function(job) { + /** + * Given a result for findOneAndUpdate() or insert() above, determine whether to process + * the job immediately or to let the processJobs() interval pick it up later + * @param {Error} err error passed in via MongoDB call as to whether modify call failed or passed + * @param {*} result the data returned from the findOneAndUpdate() call or insertOne() call + * @access private + * @returns {undefined} + */ + const processDbResult = (job, result) => { + debug('processDbResult() called with success, checking whether to process job immediately or not'); + + // We have a result from the above calls + // findOneAndUpdate() returns different results than insertOne() so check for that + let res = result.ops ? result.ops : result.value; + if (res) { + // If it is an array, grab the first job + if (Array.isArray(res)) { + res = res[0]; + } + + // Grab ID and nextRunAt from MongoDB and store it as an attribute on Job + job.attrs._id = res._id; + job.attrs.nextRunAt = res.nextRunAt; + + // If the current job would have been processed in an older scan, process the job immediately + if (job.attrs.nextRunAt && this._jobsToLock < this._lockedJobs.length && job.attrs.nextRunAt < this._nextScanAt) { + debug('[%s:%s] job would have ran by nextScanAt, processing the job immediately', job.attrs.name, res._id); + processJobs.call(this, job); + } + } + + // Return the Job instance + return job; + }; + try { debug('attempting to save a job into Agenda instance');