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.