Sam*_*Sam 5 django python-3.x supervisord amazon-elastic-beanstalk django-channels
过去几天我花了很多时间将频道实现到我的Django应用程序中,以便使用Channel的websocket支持.我的Django项目是用Python 3.4编写的,我使用的是DaphneChannel的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进去,但主管无法启动每一个过程,如果我尝试手动启动监督员检查的过程中supervisorctl status给了我FATAL "Exited too quickly (process log may have details)的一切,我尝试初始化.日志是空的.
频道后端配置:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"ROUTING": "<app>.routing.channel_routing",
"CONFIG": {
"hosts": [
os.environ.get('REDIS_URL', 'redis://localhost:6379')
],
},
},
}
Run Code Online (Sandbox Code Playgroud)
asgi.py:
import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<app>.settings")
channel_layer = get_channel_layer()
Run Code Online (Sandbox Code Playgroud)
主管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
Run Code Online (Sandbox Code Playgroud)
.ebextensions/channels.config:
container_commands:
01_start_supervisord:
command: "sh /supervisord/start_supervisor.sh"
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
start_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
Run Code Online (Sandbox Code Playgroud)
start_daphne:
#!/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
Run Code Online (Sandbox Code Playgroud)
start_worker:
#!/usr/bin/env bash
source /opt/python/run/venv/bin/activate
python manage.py runworker
Run Code Online (Sandbox Code Playgroud)
我松散地遵循这个指南但是因为它是为python2 EB环境编写的,所以它实际上只对ALB设置和基本管理程序配置有用.
感谢大家阅读本文,如果我能通过代码/输出等方式提供其他任何内容,请告诉我.
因此,感谢 Berlin 的回答提供的日志记录建议,以及 AWS 支持团队建议的虚拟环境调整(我将这个问题转发给他们),我终于能够让它发挥作用。
首先,我最终完全Redis从 Supervisor 中删除,而是选择运行 ElastiCache Redis 实例,然后将其连接到我的 EB 实例。我不认为这是处理这个问题的唯一方法,但这是我实施的最佳途径。
然后,我不再使用预先存在的start_supervisor.sh脚本,而是向channels.configebextension 添加了一个命令来创建脚本并将其添加到 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
Run Code Online (Sandbox Code Playgroud)
仅此一项就足以让 Supervisor 在 EB 部署上运行,但我必须进行更多更改才能让 Daphne 和我的 Django 工作人员保持活力:
启动_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
Run Code Online (Sandbox Code Playgroud)
start_worker.sh:
#!/usr/bin/env bash
source /opt/python/run/venv/bin/activate && source /opt/python/current/env
python manage.py runworker
Run Code Online (Sandbox Code Playgroud)
AWS 支持人员建议我添加&& source /opt/python/current/envvirtualenv 激活命令,因为 env 变量不会自动拉入 virtualenv,这会导致 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
855 次 |
| 最近记录: |