0

I am working on a IoT projects with Django.I don't like to do tedious coding.The problem here is I have a model name Period like this:

class Period(models.Model):
      number = models.PositiveIntegerField(primary_key=True)
      start_time = models.TimeField()
      end_time = models.TimeField()

In addition, i want my Celery beat to do something at Period.end_time and I add this code. Code in mysite/app/tasks.py.

@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    all_periods = Period.objects.all()
    for period in all_periods:
        hour = period.end_time.hour
        minute = period.end_time.minute
        sender.add_periodic_task(
            crontab(hour=hour, minute=minute),
            do_some_thing.s()
        )
@task
def do_some_thing():
      #do_some_thing

Here is the other files:

#mysite/mysite/celery.py
from __future__ import absolute_import
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'smartschool.settings')
app = Celery('smartschool')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

#mysite/mysite/__init__.py
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__=['celery_app']

#mysite/mysite/settings.py ##Celery part.
CELERY_BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'rpc://'
CELERY_ACCEPT_COTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Ho_Chi_Minh'
CELERY_IMPORT = ('timetable.tasks')
CELERY_BEAT_SCHEDULE = {
    #'test':
    #{
    #    'task': 'timetable.tasks.hello',
    #    'schedule': 10.0,
    #},
    'system_on':
    {
        'task': 'timetable.tasks.system_on',
        'schedule': crontab(hour=7, minute=0)
    },
    'system_off':
    {
        'task': 'timetable.tasks.system_off',
        'schedule': crontab(hour=17, minute=30)
    },
}

The periodic tasks added handy to CELERY_SHEDULE_BEAT worked fine but the tasks add by add_periodic_task function didn't. English is not my mother tongue; please excuse any errors on my post.

Q.Nguyen
  • 171
  • 1
  • 8

3 Answers3

0

You can use @periodic_task decorator up to your crontab task and after the your project run you should run this code. celery -A YOURPROJETNAME worker -l -b info

jackquin
  • 534
  • 1
  • 7
  • 19
  • Where should i put the @periodic_task decorator if i want to run add_periodic_task in a loop to automatically add periodic tasks base on the Period instances? And by the way, i am using Celery 4.2.1. – Q.Nguyen Sep 12 '18 at 07:36
  • add for your setup_periodic_tasks function – jackquin Sep 12 '18 at 07:39
0

Also another way to run crontab jobs like this.

@task
def my_task():
   //your code 

and your celery.py file

app.conf.beat_schedule = {
    'my_task': {
        'task': 'Path.tasks.my_task',
        'schedule': crontab(minute='*/5'),
        'args': []
    },
}
jackquin
  • 534
  • 1
  • 7
  • 19
0

i think I 've figured out. By changing @task.on_after_configure.connect to @task.on_after_finalize.connect, the add_periodic_task function has work however only one task was added while I have 11 Period instances!

Q.Nguyen
  • 171
  • 1
  • 8