After some frustrating days of running this, I'm needing to look at debugging a celery worker process in VSCode. This is following the suggested process in the Celery docs for creating a message handler, rather than pub/sub from the same application.
The celery.py file:
from __future__ import absolute_import, unicode_literals
import os
import json
from celery import Celery, bootsteps
from kombu import Consumer, Exchange, Queue
dataFeedQueue = Queue('statistical_forecasting', Exchange('forecasting_event_bus', 'direct', durable=False), 'DataFeedUpdatedIntegrationEvent')
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
app = Celery('statistical_forecasting')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Not required yet as handler is within this file
#app.autodiscover_tasks()
class DataFeedUpdatedHandler(bootsteps.ConsumerStep):
def get_consumers(self, channel):
return [Consumer(channel, queues=[dataFeedQueue], callbacks=[self.handle_message], accept=['json'])]
def handle_message(self, body, message):
event = json.loads(body)
# removed for brevity, but at present echo's message content with print
message.ack()
app.steps['consumer'].add(DataFeedUpdatedHandler)
My abbreviated project structure is:
workspace -
vscode -
- launch.json
config -
__init__.py
settings -
local.py
venv -
celery.exe
statistical_forecasting -
__init__.py
celery.py
farms -
__init__.py
handlers.py # ultimately handler code should live here...
From terminal with venv enable I'm running celery -A statistical_forecasting worker -l info
which does appear to succeed in setting up and running the basic message handler.
What I've tried thus far with VSCode is setting up the follow configuration in launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Celery",
"type": "python",
"request": "launch",
"module": "celery",
"console": "integratedTerminal",
//"program": "${workspaceFolder}\\env\\Scripts\\celery.exe",
"args": [
"worker",
"-A statistical_forecasting",
"-l info",
]
},
]
}
Unfortunately this just results in the following message:
Error:
Unable to load celery application.
The module statistical_forecasting was not found.
Logically I can reason that the debug should be running celery
from the workspace directory and that it should see the statistical_forecasting
directory with a __init__.py
technical making it a module?
I've tried other various ideas, like forcing the program
in lauch.json
setting virtual environment etc. but all with the same basic Error message returned.
The 'init.py' within statistical_forecasting contains the standard Django setup, that I'm not convinced that it's required as celery task is run outside of Django and I don't intend publish/receive from the Django application.