本職是前端,最近想利用GitHub的webhook Flask 搞個簡單的自動化部署,需求很簡單:
在有提交的時候透過webhook介面自動拉取程式碼,並重啟uwsgi。
python新手,程式碼輕噴。
flask程式碼(目錄結構參考的《FLASK WEB開發》中的目錄,就不貼了);主要的程式碼如下:
# coding=utf-8
import os
from flask import request, json, jsonify
from threading import Thread
from . import main
def restart():
os.system('./reload.sh')
@main.route('/')
def welcome():
user_agent = request.headers.get('User-Agent')
return "Welcome, %s" % user_agent
@main.route('/webhook', methods=['POST'])
def webhook():
print('---------------------begin----------------------')
print(request.get_json())
print('---------------------end----------------------')
push_info = request.get_json()
commit_info = push_info['commits']
if push_info['ref'] == 'refs/heads/master':
print('master分支有提交')
print(push_info['commits'])
os.system('git pull origin master')
if push_info['commits'][0]['committer']['email'] == '**********@qq.com':
print('确认是自己提交的')
data = {
'hello': 'world',
}
js = json.dumps(data)
resp = jsonify(data)
resp.status_code = 200
t = Thread(target=restart, daemon=True)
t.start()
return resp
reload.sh腳本程式碼
#/usr/bin/sh
sleep 10s
killall -s INT /www/webhook/bin/uwsgi
sleep 10s
uwsgi uwsgi.ini
遇到的問題如下:
1.return语句不会执行,因为线程中把uwsgi杀死了
2.脚本关闭uwsgi报 `is taking too much time to die...NO MERCY !!!`
3.上一步时间太长导致uwsgi重启失败
有木有大神幫忙看下有什麼問題,可以怎麼改
重啟伺服器可以用: