0

I'm having this weird situation where my Cron Job is successfully executing a function that returns a promise, however, when it tries to execute a .find() on the Model, it never actually executes. I have this same function used elsewhere in my app and is called via an API call and returns no problem. Is there something I'm missing?

Here is my cron script:

var CronJob = require('node-cron');
var TradeService = require('../services/TradeService');

// Setup the cron job to fire every second
CronJob.schedule('* * * * * *', function() {
  console.log('You will see this message every second');
  TradeService.executePendingTrades();

}, null, true, 'America/Los_Angeles');

Here are the related functions that get called:

exports.executePendingTrades = () => {

  // Get all pending trades
  exports.getPendingTrades().then(results => {
    console.log('results', results); // This never fires
  })
}

exports.getPendingTrades = () => {
  return new Promise((resolve, reject) => {

    Trades.find({})
      .where('is_canceled').equals('false')
      .where('is_completed').equals('false')
      .sort('-created_at')
      .exec( (err, payload) => {
        if (err) {
          return reject(err); // This never fires
        }
        return resolve(payload); // This never fires
      })
  });
}
Stevie Star
  • 2,331
  • 2
  • 28
  • 54

1 Answers1

3

This is a shot in the dark, but make sure you are starting a database connection in your CRON job. Otherwise you won't be able to execute any queries.

mdeang2
  • 231
  • 1
  • 9