I was trying to use Celery to query an external api at a regular frequency and update my database in my Django project with the new data. Celery schedules the task correctly and sends it to the celery worker but it never executes anything.
Here is my celery.py file which is at the same level as my settings.py:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
from celery.schedules import crontab
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "data_analysis.settings")
app = Celery("data_analysis")
app.conf.enable_utc = False
app.conf.update(timezone= 'Asia/Kolkata')
app.config_from_object(settings, namespace="CELERY")
app.autodiscover_tasks()
app.conf.beat_schedule = {
'update_all_api':{
'task':'operations.tasks.celery_update',
'schedule': 30.0,
}
}
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
This is my tasks.py which is inside an app named 'operations':
from celery import shared_task
import time
from operations.models import Machine, MachineData
from operations.analytics.processing import get_data
@shared_task(bind=True)
def celery_update(self):
print('Running Update')
print('The time is :' str(time.asctime(time.localtime(time.time()))))
print('Getting Data ...')
all_machines = Machine.objects.all()
for machine in all_machines:
data_list = get_data(machine.api_url)
for data in data_list:
# print(data)
if not MachineData.objects.filter(data=data):
print('New Data received for :' str(machine))
MachineData.objects.create(machine=machine, data=data)
else:
print('No new data for : ' str(machine))
return 'done'
The scheduler sends the task to the celery worker, but it never executes the function.
Here is a screenshot of the settings and the directory structure and the terminal. settings
If someone can point out my mistake, it will be very helpful.
CodePudding user response:
In addition to running celery beat to producing tasks, You should run a celery worker like follow:
celery -A data_analysis worker -l info
celery beat produces the task and then, the celery worker will execute the task.