0

Am using node-cron to run a cron job every 12 Oclock Midnight, the job am running is to increment some data on the user table, the issue now is that the job will run twice.

e.g I wanna increment 15, after 12 o'clock, it will turn to 17 instead of 16.

have tried my possible best to detect the issue, am unable.

When I stop the Nodejs Application, the MySQL database keeps updating, whereas have already off the nodejs application here is the job am running

const CronJob = require('cron').CronJob;
const logger = require("../helpers/logger");
const { updateUser } = require("../helpers/user");
const db = require("../models/db");



const job = new CronJob('* * * * *', async function() {
    
    db.query("UPDATE sponsored_posts SET post_status = 0", (err, data) => {
        if (err) logger.debug(err)
    });

    db.query("DELETE FROM sponsored_posts", (err, data) => {
        if (err) logger.debug(err)
    });


    //EXPIRING USER
    db.query("SELECT * FROM all_users WHERE membership_expired > 0", async (err, data) => {
        if (data.length > 0) {
            //FIlter Through All User
            //Deduct While They Havent Expired
            await data.map(async user => {
                await updateUser({
                        uid: parseInt(user.uid),
                        membership_expired: user.membership_expired - 1
                })
                
            })
        }
    })
    
    db.query("SELECT * FROM all_users WHERE membership_expired = 0", async (err, data) => {
        if (data.length > 0) {
            //FIlter Through All User
            //Set Can Purchase To 1
            await data.map(async user => {
                await updateUser({
                    uid: parseInt(user.uid),
                    can_purchase: 1
                })
                
            })
        }
    });

    
    
    
}, null, false, "Africa/Lagos");

job.start();
The Owner
  • 1
  • 3
  • Can you please [edit] your question to show the code where you invoke node-cron? – O. Jones Jun 17 '21 at 11:16
  • have edited it, – The Owner Jun 17 '21 at 19:55
  • Your cron pattern is `* * * * *`. But [node-cron](https://www.npmjs.com/package/cron#available-cron-patterns) takes six items, not five. And, a six-star cron pattern means "run this job every second". To run the job [daily at midnight](https://github.com/kelektiv/node-cron/blob/master/examples/at_midnight.js) you need `0 0 0 * * *`. And, your code doesn't have any SQL UPDATE statements in it. It seems likely your unwanted UPDATE is somewhere else in your code. – O. Jones Jun 18 '21 at 09:50

1 Answers1

0

If you are using a Linux system you can use the crontab command to set your cronJob

  1. Create a sh file

example, your-cron.sh
// write your cron script execution commend there
node youscript.js
//or
npm run yourscript

make sure you give execute permission
chmod +x your-cron.sh

  1. set your cron in crontab

set your cron in crontab with following command

crontab -e

0 0 * * * /your/absoulute/path/your-cron.sh >> /your/absoulute/path/logs/your-cron-output.log 2>&1

write command and save it use crontab -l to make sure it is saved properly

https://crontab.guru/ is a site that helps you with scheduling cron.

0 0 * * * mean Cron run daily At 00:00.

  • Have already written the code to execute at 00:00. but the issue is that the code will run twice instead of once. and if I turn of nodejs application in Cpanel, the job will still update my database – The Owner Jun 17 '21 at 18:04