情况:我想将我们的Django项目的代码更改部署到舞台或生产服务器。为此我们有一个ansible部署脚本,它从gitlab中提取django项目的代码,迁移数据库,收集静态文件,重新启动不同的服务器等。
问题:当我通过ansible脚本部署时,一些uwsgi进程(平均10个中的1个)不会重新加载我的django应用程序。例如,在代码更新后静态资产发生更改并且静态资产包的文件名哈希发生更改后,我可以重现此问题。然后服务器抛出500错误,因为uwsgi仍然尝试使用旧哈希加载包(通过https://github.com/ezhome/django-webpack-loader)。当我在静态资产发生变化的部署后刷新浏览器中的网站时,服务器在大约10-20%的请求中返回500错误,因为它找不到旧的静态资产包(例如:bundle.9290aAFKASE234。 JS)。
当我在服务器上运行/etc/init.d/supervisor restart
时问题立即消失,所有uwsgi进程似乎都正确重新加载。
相关的nginx配置
location / {
{% if nginx_site_basic_auth %}
auth_basic "Test Server what.digital";
auth_basic_user_file "{{ project_root}}/nginx_passwdfile";
{% endif %}
uwsgi_pass {{ project_name }}_server;
uwsgi_read_timeout 1800s;
include /etc/nginx/uwsgi_params;
}
uwsgi config(app.ini)
# uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = {{ project_root }}
# Django's wsgi file
module = {{ project_django_wsgi }}
plugins = python
# the virtualenv (full path)
home = {{ project_venv }}
# process-related settings
# prevents some pretty crazy erratic behaviour
lazy-apps = true
# master
master = true
# maximum number of worker processes
processes = 10
pidfile = {{ wsgi_pid_file }}
#touch-reload = {{ project_root }}/config/wsgi.py
#daemonize = {{ project_root }}/{{ project_name }}-uwsgi-daemon.log
logto = {{ project_root }}/{{ project_name }}-uwsgi-error.log
log-5xx = true
disable-logging = true
harakiri = 120
no-orphans = true
max-requests = 50
# give appropriate permissions to socket file
chmod-socket = 666
# the socket (use the full path to be safe)
socket = {{ project_root }}/{{ project_name }}.sock
# http-socket = :8000
# clear environment on exit
vacuum = true
buffer-size = 32768
# https://stackoverflow.com/questions/32452529/uwsgi-emperor-unicodeencodeerror-ascii-codec-cant-encode-character
env = LANG=en_US.UTF-8
主管模板(由ansible填充):
[program:uwsgi]
user = {{ user_name }}
command={{ project_venv }}/bin/uwsgi --ini {{ project_root }}/{{ uwsgi_conf_file }}
environment={% for name, value in env_vars.iteritems() -%}
{{ name }}="{{ value|replace('%', '%%') }}";
{%- endfor %}
autostart=true
autorestart=true
stderr_logfile = {{ project_logs }}/supervisor_uwsgi_err.log
stdout_logfile = {{ project_logs }}/supervisor_uwsgi_stdout.log
stopsignal=INT
相关的ansible config
- name: upload supervisor configuration
template: src=supervisor.j2 dest=/etc/supervisor/conf.d/{{ project_name }}.conf
become_user: root
#- name: make sure supervisord runs
# service: name=supervisor state=started
# become_user: root
#- name: reread supervisorctl config files
# command: supervisorctl reread
# become_user: root
- name: restart supervisor
# ansible's supervisorctl module doesnt really restart and some weird caching problems emerge
# shell: supervisorctl restart uwsgi
shell: /etc/init.d/supervisor restart
become_user: root
- name: make sure nginx is restarted
service: name=nginx state=restarted
become_user: root