bull icon indicating copy to clipboard operation
bull copied to clipboard

Job cron run wrong time

Open tuannp1307 opened this issue 3 years ago • 6 comments

Description

I setup a cronjob to run send sms every day at 8:00 A.M

  • File server.js global._cronJobSms = require('./controllers/Backend/sms').queueSms;
  • File ./controllers/Backend/sms.js
const Queue = require('bull');
const smsService = require('../../services/sms');
const common = require('../../utils/common');

const queueSms = new Queue(`SMS_JOB`, {
	redis: _redis['__redisClient'].options,
});

queueSms.clean(0, 'delayed');
queueSms.clean(0, 'active');
queueSms.clean(0, 'completed');
queueSms.clean(0, 'failed');
queueSms.clean(0, 'paused');
queueSms.clean(0, 'wait');

queueSms.process(`SEND_SMS_JOB`, async (job, done) => {
    console.log('[SEND_SMS_JOB] Start send sms job - Time: ', common.convertDatetoString(Date.now(), 'yyyy-mm-dd hh:MM:ss.l'))

    await Promise.all([
		smsService.sendSmsDpdMinus5(),
		smsService.sendSmsDpd(1),
		smsService.sendSmsDpd(8),
		smsService.sendSmsDpd(20),
		smsService.sendSmsDpd(61)
	]).catch(function (e) {
		console.log(`[SEND_SMS_JOB] Job exception: ${e?.message} - Time: `, common.convertDatetoString(Date.now(), 'yyyy-mm-dd hh:MM:ss.l'))
	})

	console.log('[SEND_SMS_JOB] Finish send sms job - Time: ', common.convertDatetoString(Date.now(), 'yyyy-mm-dd hh:MM:ss.l'))
	done();
});

queueSms.add( 'SEND_SMS_JOB', null, { repeat: { cron: '0 8 * * *' } });
queueSms.on('completed', (job, result) => {
    job.remove();
});

module.exports = {
    queueSms,
};

But the job only run at 8.0 a.m on the first time. From the 2nd time, it run on 4.10pm. Here is the log.

1
2022-08-21T16:13:33.697+07:00
[SEND_SMS_JOB] Finish send sms job - Time: 2022-08-21 04:13:33.697
2
2022-08-21T16:10:00.051+07:00
[SEND_SMS_JOB] Start send sms job - Time: 2022-08-21 04:10:00.051
3
2022-08-20T16:23:42.226+07:00
[SEND_SMS_JOB] Finish send sms job - Time: 2022-08-20 04:23:42.226
4
2022-08-20T16:10:00.046+07:00
[SEND_SMS_JOB] Start send sms job - Time: 2022-08-20 04:10:00.046
5
2022-08-20T08:13:37.065+07:00
[SEND_SMS_JOB] Finish send sms job - Time: 2022-08-20 08:13:37.065
6
2022-08-20T08:00:00.069+07:00
[SEND_SMS_JOB] Start send sms job - Time: 2022-08-20 08:00:00.069

Can you please help to check this problem. Thank you

Minimal, Working Test code to reproduce the issue.

(An easy to reproduce test case will dramatically decrease the resolution time.)

Bull version

^4.8.4

Additional information

I live in GMT+7 timezone

tuannp1307 avatar Aug 22 '22 09:08 tuannp1307

Please provide a code that can be run standalone without internal dependencies.

manast avatar Aug 23 '22 10:08 manast

@manast Thank for your reply, but I do not understand what do you mean. As you can see in the log file, the "send email/sms" code run without any error. It finish after only a few minutes. The problem is that, from the second time, the cron job start at the wrong time

tuannp1307 avatar Aug 23 '22 10:08 tuannp1307

Sure, but how can I reproduce the error in order to be able to fix it?

manast avatar Aug 23 '22 10:08 manast

@manast Oh, I got it, so I will make a sample project and test again. If the error still happen, I will send the sample project to you. Is it ok?

tuannp1307 avatar Aug 23 '22 10:08 tuannp1307

Hi @manast, how are you. I have make a test project and run it for a week. I set up the cron '30 13 * * *', but it only run on the right time for a few first days. after that it run random, here is the log

[SEND_EMAIL_JOB] Start send email job - Time:  2022-08-29 01:43:53.779
[SEND_EMAIL_JOB] Start send email job - Time:  2022-08-30 02:16:32.811

Here is the source code, you can run it locally. Ps1: Please use the Dockerfile to run it in a docker container Ps2: Please change the redis config in config/uat.js

tuannp1307 avatar Aug 31 '22 02:08 tuannp1307

@tuannp1307

Your repo is probably private and cannot access it.

The simpler your test the better, maybe a plain old js is better than docker.

Cheers mate.

pauldeng avatar Sep 02 '22 01:09 pauldeng

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 01 '22 10:11 stale[bot]