Answer the question
In order to leave comments, you need to log in
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.
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)
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question