rq / django-rq

A simple app that provides django integration for RQ (Redis Queue)
MIT License
1.82k stars 286 forks source link

How to schedule job with scheduler using crontab? #159

Open sheeshmohsin opened 8 years ago

sheeshmohsin commented 8 years ago

There is no docs link or nothing in the README regarding support of crontab.

ericmillsio commented 8 years ago

You can use the rq-scheduler to do what you need? https://github.com/ui/django-rq#support-for-rq-scheduler https://github.com/ui/rq-scheduler

lechup commented 8 years ago

I think there should be a note how to intergrate it with django?

Best solution so far I found here: http://stackoverflow.com/a/32033454/479931

Just use cron() instead of schedule(), each time You run ./manage.py command (AppConfig.ready() is called).

I've created cronjobs app that only is used for registering jobs for my project.

lechup commented 8 years ago

Hm... Ok. I've ended up with cronjobs management command instead of binding with AppConfig... I had some issues with multiprocess setup (all manage.py commands were touching jobs)

# -*- coding: utf-8 -*-
from optparse import make_option

import django_rq
from django.conf import settings
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option(
            '-i',
            '--install',
            action='store_true',
            dest='install',
            default=False,
            help=u'Install cronjobs defined in settings.RQ_CRONJOBS!'
        ),
    )

    def handle(self, *args, **options):
        scheduler = django_rq.get_scheduler()

        # always remove all previous cron jobs
        for job in scheduler.get_jobs():
            print(u'Removed job: {}.'.encode('utf-8').format(job))
            job.delete()

        # install new cron jobs
        if options['install']:
            for cronjob_entry in settings.RQ_CRONJOBS:
                if type(cronjob_entry) is dict:
                    args = []
                    kwargs = cronjob_entry
                else:
                    args = cronjob_entry
                    kwargs = {}

                job = scheduler.cron(
                    *args, **kwargs
                )

                print(u'Installed job: {}.'.encode('utf-8').format(job))

And in settings.py

RQ_CRONJOBS = [
    ('*/10 * * * *', 'whatever.function'),
    {
        'cron_string': '*/10 6-23,0 * * *',
        'func': 'whatever.function',
    },
]

What is best approach for management of cronjobs using django - @selwin ? Maybe we could incorporate above as a pattern in django_rq, and add rqcronjobs management command? What do You think?

sobolevn commented 8 years ago

This is totally required. I guess the format should be similar to the CELERYBEAT_SCHEDULE.

lucas03 commented 7 years ago

Hi, today I wanted to replace my crons with rqworker and scheduler. I found this pretty nice commit implementing that.

Now, to schedule that job I need to run ./manage.py rqscheduler and to execute that job I need to be running ./manage.py rqworker. So, in my docker compose I need to have two extra services for these 2 commands? Or can I run both as one service?