gpt4 book ai didi

django - 将 Django Channels 部署到 Elastic Beanstalk Python3.4 环境

转载 作者:行者123 更新时间:2023-12-02 06:30:15 26 4
gpt4 key购买 nike

过去几天我一直在我的 Django 应用程序中实现 Channels,以便使用 Channel 的 websocket 支持。我的 Django 项目是用 Python 3.4 编写的,我使用 Daphne 和 Channel 的 redis 后端。

我已经能够通过将 Supervisord 包装在 Python2 virtualenv 中并使用它来运行在 Python3 virtualenv 中启动 Daphne/Redis/workers 的脚本来在本地运行所有功能,但没有成功部署到我们的 Elastic Beanstalk (Python 3.4) ) 环境。

有什么方法可以设置我的 EB 部署配置以在 Python2 virtualenv 中运行 Supervisor,就像我在本地一样?如果没有,我该如何让 Daphne、redis 和我的工作人员在 EB 部署上启动并运行?如果有必要,我愿意更换流程管理器,但发现 Supervisor 的语法比 Circus 更容易理解/实现,并且不知道有任何其他可行的替代方案。

通过此配置,我能够成功部署到我的 EB 环境并通过 ssh 进入其中,但 Supervisor 无法启动每个进程,并且如果我尝试手动启动 Supervisor 来检查进程supervisorctl status 给我致命的“退出太快(进程日志可能有详细信息) 对于我尝试初始化的所有内容。日志是空的。

channel 后端配置:

CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"ROUTING": "<app>.routing.channel_routing",
"CONFIG": {
"hosts": [
os.environ.get('REDIS_URL', 'redis://localhost:6379')
],
},
},
}

asgi.py:

import os
from channels.asgi import get_channel_layer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<app>.settings")

channel_layer = get_channel_layer()

supervisor conf(conf 文件的其余部分保留默认值):

[program:Redis]
environment=PATH="/opt/python/run/venv/bin"
command=sh /opt/python/current/app/<app>/start_redis.sh
directory=/opt/python/current/app
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/redis.out.log

[program:Daphne]
environment=PATH="/opt/python/run/venv/bin"
command=sh /opt/python/current/app/<app>/start_daphne.sh
directory=/opt/python/current/app
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/daphne.out.log

[program:Worker]
environment=PATH="/opt/python/run/venv/bin"
command=sh /opt/python/current/app/<app>/start_worker.sh
directory=/opt/python/current/app
process_name=%(program_name)s_%(process_num)02d
numprocs=4
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/workers.out.log

.ebextensions/channels.config:

container_commands:
01_start_supervisord:
command: "sh /supervisord/start_supervisor.sh"

start_supervisor.sh:

#!/usr/bin/env bash
virtualenv -p /usr/bin/python2.7 /tmp/senv
source /tmp/senv/bin/activate
sudo pip install supervisor
sudo /usr/local/bin/supervisord -c
/opt/python/current/app/<app>/supervisord.conf
supervisorctl -c /opt/python/current/app/<app>/supervisord.conf status

启动redis:

#!/usr/bin/env bash
sudo wget http://download.redis.io/releases/redis-3.2.8.tar.gz
sudo tar xzf redis-3.2.8.tar.gz
cd redis-3.2.8
sudo make
source /opt/python/run/venv/bin/activate
sudo src/redis-server

开始达芙妮:

#!/usr/bin/env bash
source /opt/python/run/venv/bin/activate
/opt/python/run/venv/bin/daphne -b 0.0.0.0 -p 5000 <app>.asgi:channel_layer

start_worker:

#!/usr/bin/env bash
source /opt/python/run/venv/bin/activate
python manage.py runworker

我正在松散地关注this guide但由于它是为 python2 EB 环境编写的,因此实际上只适用于 ALB 设置和基本管理程序配置。

感谢你们阅读本文,如果我可以通过代码/输出等方式提供其他任何内容,请告诉我。

最佳答案

感谢 Berlin 的回答提供的日志记录建议,以及 AWS 支持团队建议的虚拟环境调整(我将这个问题转发给他们),我终于能够让它发挥作用。

首先,我最终从 Supervisor 中完全删除了 Redis,而是选择运行 ElastiCache Redis 实例,然后将其连接到我的 EB 实例。我不认为这是处理这个问题的唯一方法,但这是我实现的最佳途径。

然后,我不再使用预先存在的 start_supervisor.sh 脚本,而是向 channels.config ebextension 添加一个命令来创建脚本并将其添加到 EB 的部署后操作。这是必要的,因为 .ebextension 配置文件在部署期间运行,但不会在环境创建之后存在(这可能不完全正确,但为了这个解决方案,这就是我对它们的看法) ,所以即使我的脚本基本上是正确的,一旦部署完成,它启动的 Supervisor 进程就会立即终止。

所以我的 .ebextensions/channels.config 现在是:

container_commands:
01_create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/start_supervisor.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
virtualenv -p /usr/bin/python2.7 /tmp/senv
source /tmp/senv/bin/activate && source /opt/python/current/env
python --version > /tmp/version_check.txt
sudo pip install supervisor
/usr/local/bin/supervisord -c /opt/python/current/app/<app>/supervisord.conf
supervisorctl -c /opt/python/current/app/<app>/supervisord.conf status

仅此一项就足以让 Supervisor 在 EB 部署上运行,但我必须进行更多更改才能让 Daphne 和我的 Django 工作人员保持活力:

start_daphne.sh:

#!/usr/bin/env bash
source /opt/python/run/venv/bin/activate && source /opt/python/current/env
/opt/python/run/venv/bin/daphne -b 0.0.0.0 -p 5000 <app>.asgi:channel_layer

start_worker.sh:

#!/usr/bin/env bash
source /opt/python/run/venv/bin/activate && source /opt/python/current/env
python manage.py runworker
AWS 支持人员建议我将 && source/opt/python/current/env 添加到 virtualenv 激活命令,因为 env 变量不会自动拉入 virtualenvs,这导致 Daphne 和工作人员由于导入错误而在创建时死亡。

我还对我的 supervisord.conf 文件进行了一些更改:

[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)

[supervisord]
logfile=/tmp/supervisord.log ; supervisord log file
loglevel=error ; info, debug, warn, trace
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket

[program:Daphne]
environment=PATH="/opt/python/run/venv/bin"
command=sh /opt/python/current/app/<app>/start_daphne.sh --log-file /tmp/start_daphne.log
directory=/opt/python/current/app
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/daphne.out.log
stderr_logfile=/tmp/daphne.err.log

[program:Worker]
environment=PATH="/opt/python/run/venv/bin"
command=sh /opt/python/current/app/<app>/start_worker.sh --log-file /tmp/start_worker.log
directory=/opt/python/current/app
process_name=%(program_name)s_%(process_num)02d
numprocs=4
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/workers.out.log
stderr_logfile=/tmp/workers.err.log

关于django - 将 Django Channels 部署到 Elastic Beanstalk Python3.4 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43947099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com