G
G
gitinit2019-03-28 15:41:58
Django
gitinit, 2019-03-28 15:41:58

How to use django-rq and rq_scheduler to run recurring tasks?

If you have used django-rq and rq_scheduler to run recurring tasks, please give me an example of your code. I will be very grateful.

apps.py
import sys

from django.apps import AppConfig
from django_rq import get_scheduler


class PrinterAppConfig(AppConfig):
    name = 'printer_app'

    def ready(self):
        from printer_app.async_tasks import streams_tasks

        if "rqscheduler" not in sys.argv:
            return

        scheduler = get_scheduler('print_check')

        for job in scheduler.get_jobs():
            job.delete()

        streams_tasks(scheduler)

async_tasks.py
import django_rq
import requests
from django_rq import job

from datetime import datetime, timedelta

from checks.models import Printer, Check


def new_checks():
    url = 'http://127.0.0.1:8000/new_checks/'
    headers = {'Api-Token': '0796859f206682d5fb185bcda09f0fa5',
               'Api-Secret-Key': 'P2jg8WASSok8'}

    response = requests.get(url, headers=headers).json()  # словарь
    return response


def streams_tasks(scheduler):
    scheduler.schedule(
        scheduled_time=datetime.utcnow(),
        func=new_checks,
        interval=1,  # в секундах
    )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-04-06
@gitinit

I used python-rq for background processing, but cron handles periodic jobs with this more than perfectly. I don't see any reason to bother. Write your own manage.py command and that's it, nothing else is required

E
elyasa, 2020-04-28
@elyasa

Try python daemon, it's much more convenient than bothering with radishes, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question