English 中文(简体)
best way to setup a webhook to restart apache for a django server
原标题:

I first tried to use django and then django-webhooks to call a shell script that restarts the server. This didn t work, because the webpage hangs when the server restart is called, as django is reloaded.

Then I used fastcgi and python alone to create a URL that calls the shell script. I know the python script works when I run it on the server, but not when it is run from the URL.

Apache is setup as:

<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>

The python code called by apache is:

#!/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()

And the shell script is:

#!/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 

I m not seeing any errors in the apache logs and the access log shows that the triggering URL is being called. However, I only see the python warnings the first time the URL is called after a restart and it never actually restarts the server.

问题回答

I m using webfaction, and the following script works for me:

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)

The "stop" script then looks like this:

#!/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"

The "start" script:

/path/to/app/apache2/bin/httpd -f /path/to/app/apache2/conf/httpd.conf

I didn t use django, but with simple python following code works for me.

import os
os.system( apachectl -k restart )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签