Tivix / django-cron

Write cron business logic as a Python class and let this app do the rest! It enables Django projects to schedule cron tasks, tracks their success / failures, manages contention (via a cache) etc. Basically takes care of all the boring work for you :-)
www.tivix.com
MIT License
900 stars 193 forks source link

Run weekly #151

Closed matburnham closed 2 years ago

matburnham commented 5 years ago

I'm trying to run a backup task once a week. The following runs a daily backup, but the weekly doesn't seem to happen. It's rather difficult to debug as I only want it to happen once a week!

class Backup(CronJobBase):
    RUN_AT_TIMES = ['23:00']
    schedule = Schedule(run_at_times=RUN_AT_TIMES)
    code = 'mysite.Backup'

    def do(self):
        management.call_command('dbbackup')

class BackupImages(CronJobBase):
    # Backup once a week
    RUN_EVERY_MINS = 60 * 24 * 7
    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'mysite.BackupImages'

    def do(self):
      management.call_command('mediabackup', '--compress')

Is running less often than daily supported? Is there a better way to do this?

gregschmit commented 5 years ago

I don't know whether this is supported, however, as a workaround, you could give your model a day to run on (0-6, Mon-Sun), and then run your CronJob daily (e.g., RUN_AT_TIMES = ['1:30']) and in your management command check to see if it is the right day using timezone.localtime(timezone.now()).weekday(), and only run the command if it is that day.

matburnham commented 5 years ago

Sounds like a good workaround @gregschmit. Thanks.

eagle-r commented 5 years ago

We have done something similar for running jobs monthly

JedrzejMaluszczak commented 2 years ago

Take a look at Run weekly feature: https://github.com/Tivix/django-cron/blob/master/docs/sample_cron_configurations.rst