1

I'm looking for a way to check the subscriptions of our users with a cron job daily. I want to use node-cron for this. I'm not sure where I should put the code to start the cronjob. Currently, I have it set up like this:

app.js:

app.listen(config.port, function() {

            // Logging initialization
            console.log('--');
            console.log(chalk.green(config.app.title));
            console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
            console.log(chalk.green('Port:\t\t\t\t' + config.port));
            console.log(chalk.green('Database:\t\t\t\t' + config.db.uri));
            if (process.env.NODE_ENV === 'secure') {
                console.log(chalk.green('HTTPs:\t\t\t\ton'));
            }
            console.log('--');

            if (callback) callback(app, db, config);

            var job1 = new cron.CronJob({
                cronTime: '* * * * *',
                onTick: function(){
                    app.get('/api/users/checkSubscriptions/').success(function() {
                        console.log( 'working!')
                    }).error(function() {
                        console.log( 'error!')
                    });
                },
                start: false,
                timeZone: 'Europe/Paris'});
            job1.start();
        });

users.server.routes.js

'use strict';

module.exports = function(app) {

    app.route('/api/users/checkSubscriptions/')
      .get(subscriptionCheck.checkSubscriptions);
};

cronjob-subscription.server.controller

var mongoose = require('mongoose'),
User = mongoose.model('User');
exports.checkSubscriptions = function() {
  User.findAll(function(err, users) {
    if(err) {
      console.log(err);
    } else {
      for(var i = 0;i< users.length; i++) {
        var user = users[i];
        console.log(user);
      }
    }
  });
}

Currently I just want to print the users to the console to check if this is working. With the current code in app.js I get the following error:

app.get('/api/users/checkSubscriptions/').success(function() {
                            ^

    TypeError: app.get is not a function

Could you please advice how I can do the API call in the cronjob when initialising the app?

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
Thomas L
  • 320
  • 3
  • 14
  • Seems to work fine outside of the `app.listen`. I guess this makes sense if it's trying to initialise `app.get` before `app.listen` has finished. Also, i'm not so sure how viable having node-cron and an express server running at the same time is. – TomFirth Oct 02 '17 at 12:29
  • @TomFirth when placing the code outside of the app.listen I still receive the same error message (app.get is not a function..). How would you suggest I implement this without using the node-cronm but inside express then? – Thomas L Oct 02 '17 at 12:47

1 Answers1

0

I managed to make it work by adding the code in my server.js file instead of the app.js file. In the server.js I have the following (relevant) code now:

var app = require('./config/lib/app'),
cron = require('cron');

var server = app.start(function(req, res) {
var job1 = new cron.CronJob({
    cronTime: '00 30 07 * * 1-7',
    onTick: function() {
      //function execution
    },
    start: false,
    timeZone: 'Europe/Paris'
});
job1.start();
Thomas L
  • 320
  • 3
  • 14