簡體   English   中英

如何在 Heroku 免費計划上使用 Celery 任務部署 Django 應用程序

[英]How to deploy a Django application with the Celery task on Heroku Free plan

我需要在 Heroku 上部署使用 Celery 的 Django 應用程序。 該網站已經部署並且工作正常,但是 Celery 工作器無法啟動。

這里是heroku logs -t -p celery命令的 output:

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 signals.py 我有function 用於發送電子郵件:

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 測功機(因為這只是我的學習寵物項目)。 但我懷疑原因不在 Heroku 定價計划中,而是在Procfile的錯誤配置中。

Heroku Redis 插件已啟動並正在運行。

但是,我嘗試通過多種方式更改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.pycelery.py文件的forum文件夾應直接放在pet文件夾下,與所有其他應用程序的文件夾處於同一級別。

此外,在這些更改之后,我需要修改Procfile

web: gunicorn forum.wsgi --log-file -
celery: celery worker -A forum -l info -c 4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM