django-cron icon indicating copy to clipboard operation
django-cron copied to clipboard

Cron skips minutes

Open Gordey-Kachurin opened this issue 1 year ago • 1 comments

Hello. We have multiple crons running every minute. They are used for email notifications. User chooses recipients and the time he wants recipients to be notified (10:25 for example). During testing on out server we've noticed that some minutes were skipped. That was unexpected because we wrapped each of our function in django_q async_task. For example:

class GeneralEmailNotificationCron(CronJobBase):
    RUN_EVERY_MINS = 1
    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = "cron_app.general_email_notification"

    def do(self):
        async_task(general_email_notification, datetime.datetime.now())

The crontab was not the problem either. It was set as follows * * * * * /path_to/venv/bin/python3 /path_to/manage.py runcrons

I tried to reproduce the issue locally and created custom command.

class Command(BaseCommand):
    help = ('Run crons locally.' )

    def handle(self, *args, **options):         
        while True:
            execute_from_command_line(["manage.py", 'runcrons'])
            time.sleep(60)

Result was very similar to that we have seen on our server. I tried time.sleep(59) it didn't help. After some time trying to solve the problem, eventually, I set time.sleep(59) and made some changes to CronJobManager's __enter__ method

def __enter__(self):
    from django_cron.models import CronJobLog
    ct = get_current_time()
    print(ct)
    ct = ct.replace(second=0, microsecond=0)
    print(ct)
    self.cron_log = CronJobLog(start_time=ct)

It worked locally. It seems that you shouldn't track start_time in seconds when possible minimum of crontab and your library is one minute.

I haven't used it on the server yet because every time, I need to make sure we have these changes implemented. We might simply forget to make changes to the library. I'm not sure if adding it to Git is a good option. I really hope you could modify the library so that every team can simply use 'pip install'.

It is also necessary to correct humanize_duration if you will use suggested changes.

Gordey-Kachurin avatar May 20 '23 06:05 Gordey-Kachurin

I encountered the same problem, the problem is that the start_time and end_time columns contain seconds, this needs to be removed

alexeydg avatar Dec 14 '23 07:12 alexeydg