current position:Home>Django uses Django celery beat to dynamically add scheduled tasks

Django uses Django celery beat to dynamically add scheduled tasks

2022-01-30 05:09:17 Take the surname of Wang

Version information

 #  Plug in installation  
 Django==2.2.2
 django-celery-beat==2.1.0
 django-redis==4.8.0
 mysqlclient==2.0.0
 django-mysql==3.2.0
 redis==3.2.1
 uWSGI==2.0.17.1
 django-redis-cache==2.1.0
 Copy code 

Installation and configuration

  1. Install the corresponding celery edition
  2. To configure settings.py
 # django Timezone configuration 
 TIME_ZONE = 'Asia/Shanghai'
 #  If USE_TZ Set to True when ,Django Will use the default time zone of the system , At this time TIME_ZONE It doesn't work with or without settings 
 #  If USE_TZ  Set to False,TIME_ZONE = 'Asia/Shanghai',  Use Shanghai's UTC Time .
 USE_TZ = False
 ​
 INSTALLED_APPS = (
     ...,
     'django_celery_beat',
 )
 ​
 # celery beat To configure 
 # CELERY_ENABLE_UTC = False
 CELERY_TIMEZONE = TIME_ZONE
 DJANGO_CELERY_BEAT_TZ_AWARE = False
 CELERY_BEAT_SCHEDULER = 'django-celery-beat.schedulers.DatabaseScheduler'
 # celery  Set the number of startup jobs for 
 CELERY_WORKER_CONCURRENCY = 10
 #  Task prefetching function , I'll take as much as I can  n  individual , To ensure that the communication cost can be reduced .
 CELERYD_PREFETCH_MULTIPLIER = 20
 #  In some cases, deadlock can be prevented 
 CELERYD_FORCE_EXECV = True
 # celery  Of  worker  How many tasks are executed to restart 
 CELERY_WORKER_MAX_TASKS_PER_CHILD = 100
 #  Disable all speed limits , If network resources are limited , It's not recommended to drive full power .
 CELERY_DISABLE_RATE_LIMITS = True
 #  Set up an agent broker
 CELERY_BROKER_URL = 'redis://127.0.0.1:6379/2'
 #  Appoint  Backend
 CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/1'
 ​
 Copy code 
  1. Generate database

    python manage.py migrate

     #  Table structure generated after migration 
     django_celery_beat.models.PeriodicTask  This model defines a single recurring task to run .
     django_celery_beat.models.IntervalSchedule  At specific intervals ( for example , Every time 5 second ) Running plans .
     django_celery_beat.models.CrontabSchedule  And like in cron Timetable for this area   A week of minutes, hours and days  DAY_OF_MONTH month_of_year
     django_celery_beat.models.PeriodicTasks  This model is used only as an index to track when the plan changes 
     Copy code 
  2. Configure... In the working directory celery.py

     # -*- coding: utf-8 -*-
     # @File:    celeryc.py
     # @Content: celery Timing task configuration 
     ​
     ​
     import os
     from celery import Celery, platforms
     from celery.schedules import crontab
     from django.conf import settings
     ​
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_server.settings")
     app = Celery("django_server")
     app.config_from_object("django.conf:settings", namespace="CELERY")
     #  Storage location of scheduled tasks 
     app.autodiscover_tasks(["monitoring.tasks", "wechat.tasks"])
     ​
     Copy code 
  3. establish tasks Mission

     from celery import shared_task
     ​
     @shared_task
     def alarm_monitor_task(**kwargs):
       print(" Timing task !!!")
     ​
     Copy code 
  4. Create a scheduled task

     from django_celery_beat.models import PeriodicTask, IntervalSchedule
     ​
     ----- Periodic tasks 
     #   establish 10 Minutes apart  interval  object 
     schedule, _ = IntervalSchedule.objects.update_or_create(every=10, period=IntervalSchedule.MINUTES)
     #  If the task exists, update , Create if it does not exist 
     PeriodicTask.objects.update_or_create(
       defaults={
         "interval": schedule,  #  Create above 10 Minutes apart  interval  object 
         "task": "monitoring.tasks.alarm_monitor_task",  #  Specify tasks that need to be performed periodically 
         "args"=json.dumps(['arg1', 'arg2']),
         "kwargs": json.dumps({"a": 1, "b": 2}, ensure_ascii=False)  #  Incoming parameter 
       },
       name=" Timing task -task",
     )
     #  Optional parameters for periodic tasks 
     IntervalSchedule.DAYS  Fixed interval days 
     IntervalSchedule.HOURS  Fixed interval hours 
     IntervalSchedule.MINUTES  Fixed interval minutes 
     IntervalSchedule.SECONDS  Fixed interval seconds 
     IntervalSchedule.MICROSECONDS  Fixed interval microseconds 
     ​
     ​
     ----Crontab  Periodic tasks 
     from django_celery_beat.models import CrontabSchedule, PeriodicTask
     #  Create interval 30 Minutes to perform tasks 
     crontab, _ = CrontabSchedule.objects.update_or_create(
       minute="*/30",
       hour="*",
       day_of_week="*",
       day_of_month='*',
       month_of_year='*',
       timezone=pytz.timezone("Asia/Shanghai"),
     )
     #  Update if the task exists , There is no creation 
     PeriodicTask.objects.update_or_create(
       name=task_name,
       defaults={
         "kwargs": json.dumps(kwargs, ensure_ascii=False),
         "task": "wechat.tasks.subscribe_task",
         "crontab": crontab,
       },
     )
     ​
     #  Delete task 
     task = PeriodicTask.objects.filter(name__startswith=sub_id)
     if task:
         task.update(enabled=False)
         task.delete()
         
         
     #  Pause the current task 
     tasks = PeriodicTask.objects.filter(name__startswith=sub_id)
     if tasks:
         tasks.update(enabled=True if status else False)
     Copy code 
  1. Run the task

     #  Start the task  work
     celery -A django_server worker -l INFO --logfile=/var/log/dec_server/worker.log
     ​
     #  Start timer triggers  beat
     celery -A django_server beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler --logfile=/var/log/dec_server/beat.log
     Copy code 

Tips:

Reference link :

github.com/celery/djan…

pypi.org/project/dja…

copyright notice
author[Take the surname of Wang],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300509148291.html

Random recommended