设置webhook为django服务器重新启动apache的最佳方式
问题描述:
我首先尝试使用django,然后使用django-webhooks调用重新启动服务器的shell脚本。这是行不通的,因为当重新加载django时,在重新启动服务器时网页会挂起。设置webhook为django服务器重新启动apache的最佳方式
然后,我单独使用fastcgi和python创建一个调用shell脚本的URL。我知道python脚本在服务器上运行时工作,但不知道它是从URL运行的。
Apache是设置为:
<VirtualHost *:80>
ServerName webhooks.myserver.com
DocumentRoot /home/ubuntu/web/common/www
<Directory />
Options FollowSymLinks +ExecCGI
AllowOverride All
</Directory>
<Files post.py>
SetHandler fastcgi-script
</Files>
FastCgiServer /home/ubuntu/web/common/www/post.py -processes 2 -socket /tmp/fcgi.sock
</VirtualHost>
被Apache调用的Python代码是:
#!/usr/bin/python
import fcgi, warnings, os, subprocess
BASE_DIR = os.getcwd()
def app(environ, start_response):
cmd = "sudo %s/../deploy/postwebhook.sh >> /var/log/votizen/webhooks_run.log 2>> /var/log/votizen/webhooks_error.log &" % BASE_DIR
warnings.warn("Running cmd=%s" % cmd)
bufsize = -1
PIPE = subprocess.PIPE
subprocess.Popen(cmd, shell=isinstance(cmd, basestring),
bufsize=bufsize, stdin=PIPE, stdout=PIPE,
stderr=PIPE, close_fds=True)
warnings.warn("Post deployment webhook completed")
start_response('200 OK', [('Content-Type', 'text/html')])
return('Hello World!')
fcgi.WSGIServer(app, bindAddress = '/tmp/fcgi.sock').run()
,而shell脚本是:
#!/bin/bash
# restart the apache server
echo ' '
echo 'post webhooks started'
date '+%H:%M:%S %d-%m-%y'
apache2ctl -t; sudo /etc/init.d/apache2 stop; sudo /etc/init.d/apache2 start
# todo: check if apache failed
# copy media files for apps
echo "moving SC to S3"
python /home/ubuntu/web/corporate/manage.py sync_media_s3 -p sc
date '+%H:%M:%S %d-%m-%y'
echo 'post webhooks completed'
我没有看到任何apache日志和访问日志中的错误表明正在调用触发URL。但是,我只会在重新启动后第一次调用URL时才会看到python警告,并且它从不真正重新启动服务器。
答
我使用webfaction,和下面的脚本为我工作:
import time
import os
BASE_DIR = "/path/to/your/app/"
def stopit(app):
os.popen(os.path.join(BASE_DIR, "apache2", "bin", "stop"))
def startit(app):
os.popen(os.path.join(BASE_DIR, "apache2", "bin", "start"))
def restart(app):
stopit(app)
time.sleep(1)
startit(app)
“停止” 脚本则是这样的:
#!/usr/local/bin/python
import os
lines = os.popen('ps -u username -o pid,command').readlines()
running = False
for line in lines:
if '/path/to/app/apache2/conf/httpd.conf' in line:
running = True
proc_id = line.split()[0]
os.system('kill %s 2> /dev/null' % proc_id)
if not running:
print "Not running"
else:
print "Stopped"
“开始” 脚本:
/path/to/app/apache2/bin/httpd -f /path/to/app/apache2/conf/httpd.conf
答
我没有使用django,但简单的python下面的代码适用于我。
import os
os.system('apachectl -k restart')
Ryan,这对我不起作用。没有通过Apache提供此脚本?我看到的是Apache停止运行时,整个脚本停止运行,因此Apache再也不会重新启动。 – 2010-09-15 20:14:20
运行“开始”脚本不起作用?您可能需要等待1秒以上才能关闭Apache。可靠的方法是检查您的流程列表... – 2010-09-16 07:26:44