如何在 Heroku 免费计划上使用 Celery 任务部署 Django 应用程序
How to deploy a Django application with the Celery task on Heroku Free plan
我需要在 Heroku 上部署使用 Celery 的 Django 应用程序。该网站已经部署并运行良好,但 Celery worker 无法启动。
这里是 heroku logs -t -p celery
命令的输出:
2020-05-20T16:37:23.529208+00:00 heroku[celery.1]: State changed from starting to up
2020-05-20T16:37:26.576179+00:00 heroku[celery.1]: State changed from up to crashed
2020-05-20T16:37:26.580191+00:00 heroku[celery.1]: State changed from crashed to starting
2020-05-20T16:37:26.438750+00:00 app[celery.1]: Error:
2020-05-20T16:37:26.438775+00:00 app[celery.1]: Unable to load celery application.
2020-05-20T16:37:26.438776+00:00 app[celery.1]: Module 'forum' has no attribute 'celery'
我的Procfile
:
web: gunicorn --pythonpath forum forum.wsgi --log-file -
celery: celery worker -A forum -l info -c 4
forum/celery.py
文件:
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'forum.settings')
app = Celery('forum')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
forum/__init__.py
文件:
# 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',)
在 forum/settings.py
文件中,我有以下与 Celery 相关的参数:
# REDIS related settings
REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
CELERY_BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
# for Heroku
CELERY_BROKER_URL = os.environ.get('REDIS_URL', 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0')
CELERY_RESULT_BACKEND = os.environ.get('REDIS_URL', 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0')
我的 Django 项目(forum
)有几个应用程序,registration
应用程序是我的 Celery 任务所在的位置。因此,该项目有 registration/tasks.py
个文件:
import logging
from forum.celery import app
@app.task
def send_verification_email(user_id):
pass
在registration/signals.py
我有发送邮件的功能:
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .tasks import send_verification_email
# send verification email when new user is registered
@receiver(post_save, sender=User)
def user_post_save(sender, instance, created, *args, **kwargs):
if created:
# Send verification email
send_verification_email.delay(instance.pk)
我将使用免费的 Heroku dynos(因为这只是我的学习项目)。但我怀疑原因不在 Heroku 定价计划中,而是在 Procfile
的错误配置中。
Heroku Redis 插件已启动 运行ning。
但是,我尝试通过多种方式更改 Procfile
,但结果是一样的 - Celery worker 无法启动,它崩溃了。在我的本地机器上,Celery 运行良好。我通过以下命令 运行 Celery(假设我之前通过 redis-server
命令启动了 Redis 服务器):
celery worker -A forum --loglevel=debug --concurrency=4
有人可以帮我解决这个问题吗?
问题出在整个 Django 应用程序的错误结构上。
我有类似的东西:
pet
|-- forum
| |-- forum
| | |-- __init__.py
| | |-- celery.py
| | |-- wsgi.py
| | |-- asgi.py
| | |-- settings.py
| | |-- urls.py
| |-- app1
| |-- app2
| |-- appN
| |-- manage.py
| |-- Procfile
| `-- requirements.txt
pet/forum/
文件夹是多余的。包含 settings.py
或 celery.py
文件的 forum
文件夹应直接放在 pet
文件夹下,与所有其他应用程序的文件夹处于同一级别。
此外,在这些更改之后我需要修改 Procfile
:
web: gunicorn forum.wsgi --log-file -
celery: celery worker -A forum -l info -c 4
我需要在 Heroku 上部署使用 Celery 的 Django 应用程序。该网站已经部署并运行良好,但 Celery worker 无法启动。
这里是 heroku logs -t -p celery
命令的输出:
2020-05-20T16:37:23.529208+00:00 heroku[celery.1]: State changed from starting to up
2020-05-20T16:37:26.576179+00:00 heroku[celery.1]: State changed from up to crashed
2020-05-20T16:37:26.580191+00:00 heroku[celery.1]: State changed from crashed to starting
2020-05-20T16:37:26.438750+00:00 app[celery.1]: Error:
2020-05-20T16:37:26.438775+00:00 app[celery.1]: Unable to load celery application.
2020-05-20T16:37:26.438776+00:00 app[celery.1]: Module 'forum' has no attribute 'celery'
我的Procfile
:
web: gunicorn --pythonpath forum forum.wsgi --log-file -
celery: celery worker -A forum -l info -c 4
forum/celery.py
文件:
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'forum.settings')
app = Celery('forum')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
forum/__init__.py
文件:
# 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',)
在 forum/settings.py
文件中,我有以下与 Celery 相关的参数:
# REDIS related settings
REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
CELERY_BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
# for Heroku
CELERY_BROKER_URL = os.environ.get('REDIS_URL', 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0')
CELERY_RESULT_BACKEND = os.environ.get('REDIS_URL', 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0')
我的 Django 项目(forum
)有几个应用程序,registration
应用程序是我的 Celery 任务所在的位置。因此,该项目有 registration/tasks.py
个文件:
import logging
from forum.celery import app
@app.task
def send_verification_email(user_id):
pass
在registration/signals.py
我有发送邮件的功能:
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .tasks import send_verification_email
# send verification email when new user is registered
@receiver(post_save, sender=User)
def user_post_save(sender, instance, created, *args, **kwargs):
if created:
# Send verification email
send_verification_email.delay(instance.pk)
我将使用免费的 Heroku dynos(因为这只是我的学习项目)。但我怀疑原因不在 Heroku 定价计划中,而是在 Procfile
的错误配置中。
Heroku Redis 插件已启动 运行ning。
但是,我尝试通过多种方式更改 Procfile
,但结果是一样的 - Celery worker 无法启动,它崩溃了。在我的本地机器上,Celery 运行良好。我通过以下命令 运行 Celery(假设我之前通过 redis-server
命令启动了 Redis 服务器):
celery worker -A forum --loglevel=debug --concurrency=4
有人可以帮我解决这个问题吗?
问题出在整个 Django 应用程序的错误结构上。 我有类似的东西:
pet
|-- forum
| |-- forum
| | |-- __init__.py
| | |-- celery.py
| | |-- wsgi.py
| | |-- asgi.py
| | |-- settings.py
| | |-- urls.py
| |-- app1
| |-- app2
| |-- appN
| |-- manage.py
| |-- Procfile
| `-- requirements.txt
pet/forum/
文件夹是多余的。包含 settings.py
或 celery.py
文件的 forum
文件夹应直接放在 pet
文件夹下,与所有其他应用程序的文件夹处于同一级别。
此外,在这些更改之后我需要修改 Procfile
:
web: gunicorn forum.wsgi --log-file -
celery: celery worker -A forum -l info -c 4