Skip to content

Commit

Permalink
Fixed this={} in processDbResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Solpatium committed Feb 5, 2019
1 parent 159496f commit 46969a2
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions lib/agenda/save-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');

Expand Down

0 comments on commit 46969a2

Please sign in to comment.