Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron skips minutes #232

Open
Gordey-Kachurin opened this issue May 20, 2023 · 1 comment
Open

Cron skips minutes #232

Gordey-Kachurin opened this issue May 20, 2023 · 1 comment

Comments

@Gordey-Kachurin
Copy link

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.

@alexeydg
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants