gpt4 book ai didi

python - Virtualenv 未从主管 AWS Elasticbeanstalk 激活

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:42 25 4
gpt4 key购买 nike

我正在尝试将我的 Django Channels 应用程序部署到弹性 beanstalk 并在部署时启动 daphne 和工作人员,我有一个supervisord 脚本,该脚本在部署期间被复制并重新启动。下面详细介绍了一些奇怪的行为。

这是我在部署期间运行的channels.config 文件

container_commands:
01_copy_supervisord_conf:
command: "cp .ebextensions/supervisord/supervisord.conf /opt/python/etc/supervisord.conf"
02_reload_supervisord:
command: "supervisorctl -c /opt/python/etc/supervisord.conf reload"

当我的supervisord.conf 中有这个时

[unix_http_server]
file=/opt/python/run/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner

[supervisord]
logfile=/opt/python/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=10MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/opt/python/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
directory=/opt/python/current/app ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///opt/python/run/supervisor.sock

[program:httpd]
command=/opt/python/bin/httpdlaunch
numprocs=1
directory=/opt/python/current/app
autostart=true
autorestart=unexpected
startsecs=1 ; number of secs prog must stay running (def. 1)
startretries=3 ; max # of serial start failures (default 3)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
killasgroup=false ; SIGKILL the UNIX process group (def false)
redirect_stderr=false

EB 部署正常(尽管 daphne 和工作线程尚未启动和运行)

如果我将其添加到我的supervisord.conf 文件中:

[program:Daphne]
environment=PATH="/opt/python/run/venv/bin"
command=/opt/python/run/venv/bin/daphne -b 0.0.0.0 -p 5000 chat.asgi:channel_layer
directory=/opt/python/current/app
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/daphne.out.log

然后部署失败并出现以下错误:

error: , : file: /usr/lib64/python2.7/xmlrpclib.py line: 800
Traceback (most recent call last):
File "/opt/elasticbeanstalk/hooks/appdeploy/enact/01flip.py", line 42, in
main()
File "/opt/elasticbeanstalk/hooks/appdeploy/enact/01flip.py", line 36, in main
config.restart_apache()
File "/opt/elasticbeanstalk/hooks/config.py", line 250, in restart_apache
apache_cmd('restart', should_be_running=True)
File "/opt/elasticbeanstalk/hooks/config.py", line 254, in apache_cmd
check_call('/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf %s httpd' % command, shell=True)
File "/usr/lib64/python2.7/subprocess.py", line 541, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf restart httpd' returned non-zero exit status 2.

但是如果我通过 ssh 进入实例而不是 ps -aux |我看不到达芙妮实际上正在运行。

然后如果我将其添加到supervisord.conf中:

[program:Worker]
environment=PATH="/opt/python/run/venv/bin"
command=python manage.py runworker
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

它再次无法部署并出现相同的错误,但是当我检查workers.out.log日志时我看到

Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

这意味着它没有使用 Worker 程序的环境部分?

不知道哪里出了问题。我尝试环顾四周,了解如何通过supervisord 激活virtualenv,但最终得出了我已经在做的事情。

编辑 1因此,我能够通过在我的项目中包含 shell 脚本来解决无法部署的部署问题:

#!/bin/bash
VENV=$1
if [ -z $VENV ]; then
echo "usage: runinenv [virtualenv_path] CMDS"
exit 1
fi
. ${VENV}/bin/activate

这在我的supervisord.conf文件中:

[program:runvenv]
command=sh virtualenv-sh /opt/python/run/venv
directory=/opt/python/current/app
stdout_logfile=/tmp/venv.out.log

它成功部署,daphne 仍在运行,但是当我检查worker.out.log 文件时,它仍然给出关于 django 未安装的相同错误,并确保我已激活 virtualenv。我刚刚做了什么?除非在一次通话后这种情况不再持续?

最佳答案

我也遇到了 worker 无法启动的同样问题,但我可以通过替换来修复它:

command=python manage.py runworker

command=/opt/python/run/venv/bin/python manage.py runworker

我猜如果没有路径,它在启动工作程序时不会使用 virtualenv。

关于python - Virtualenv 未从主管 AWS Elasticbeanstalk 激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43398396/

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