伸缩自如的Flask [day 27] Supervisor

像gunicorn 及docker 有着执行时timeout的防止错误发生的机制,
但是要是超过了 timeout的设定,程式就不再运行的情况发生时,该怎么办呢?

也许可以试一下Supervisor这个套件,官网的文件:

http://supervisord.org/introduction.html

安装:

sudo apt-get install supervisor

再来,可以看一下套件设定档的範例:

echo_supervisord_conf

http://img2.58codes.com/2024/20122678VRaTeQssaH.png

可以将範例档存于目前的路径:

echo_supervisord_conf > supervisord.conf

未来使用该路径conf来运行:

sudo supervisord --configuration=/current/directory/path/supervisord.conf

如果使用预设在/etc/supervisor/supervisord.conf的档案来运行:

sudo supervisorctl rereadsudo service supervisor restart

那我们可以修改conf来进行设定:

[program:<app名称>]directory=/home/username/<app名称>command=gunicorn app:app -b 127.0.0.1:8000 --reload autostart=trueautorestart=truestderr_logfile=/var/log/error.logstdout_logfile=/var/log/output.log
directory: app所在位置command:监控及重起时指令autostart:supervisord启动时,执行程式autorestart:程式没执行时,重新启动

可以参考的conf设定:

[unix_http_server]file=/tmp/supervisor.sock [supervisord]logfile=/tmp/supervisord.log logfile_maxbytes=50MB        logfile_backups=10           loglevel=info                pidfile=/tmp/supervisord.pid nodaemon=false               silent=false                 minfds=1024                  minprocs=200 [rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface[supervisorctl]serverurl=unix:///tmp/supervisor.sock[program:hello_api]command=bash -c "source /home/wilson/miniconda3/bin/activate &&  gunicorn -w 4 main:app  -b 0.0.0.0:5000 --reload"directory=/home/wilson/health/health_factory_apiautostart=true                ; start at supervisord start (default: true)autorestart=true

设定完成,可以重新启动supervisor来监控服务。
藉由supervisorctl来查看是否监控成功:

sudo supervisorctl -c xxx.conf
[day 1] 开场白[day 2] blue_print[day 3] Factory mode[day 4] JWT token[day 5] session[day 6] Jinja[day 7] Post data with Form tag[day 8] ajax with jquery[day 9] request[day 10] 将资料写进DB(pymongo)[day 11] log with mongoDB[day 12] Sql Database[day 13] 档案上传[day 14] 档案下载 及 其他传值方法[day 15] boostrap 4.6[day 16] API[day 17] Docker image化--安装篇[day 18] Docker image化--运行篇[day 19] Nginx[day 20] Docker compose[day 21] Nginx with https[day 22] pythonanywhere 部署[day 23] GCP app engine (介绍)[day 24] GCP app engine (deploy)[day 25] Flask with web cam[day 26] Flask with ML[day 27] Supervisor[day 28] Flask-Mail[day 29] Line Messaging API[day 30] 结语

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章