views:

193

answers:

1

Hi,

Common situation: I have a client on my server who may update some of the code in his python project. He can ssh into his shell and pull from his repository and all is fine -- but the code is stored in memory (as far as I know) so I need to actually kill the fastcgi process and restart it to have the code change.

I know I can gracefully restart fcgi but I don't want to have to manually do this. I want my client to update the code, and within 5 minutes or whatever, to have the new code running under the fcgi process.

Thanks

+1  A: 

First off, if uptime is important to you, I'd suggest making the client do it. It can be as simple as giving him a command called deploy-code. Using your method, if there is an error in their code, your method requires a 10 minute turnaround (read: downtime) for fixing it, assuming he gets it correct.

That said, if you actually want to do this, you should create a daemon which will look for files modified within the last 5 minutes. If it detects one, it will execute the reboot command.

Code might look something like:

import os, time
CODE_DIR = '/tmp/foo'

while True:
    if restarted = True:
        restarted = False
        time.sleep(5*60)

    for root, dirs, files in os.walk(CODE_DIR):
        if restarted=True:
            break
        for filename in files:
            if restared=True:
                break
            updated_on = os.path.getmtime(os.path.join(root, filename))
            current_time = time.time()
            if current_time - updated_on <= 6 * 60: # 6 min
                # 6 min could offer false negatives, but that's better
                # than false positives
                restarted = True
                print "We should execute the restart command here."
Justin Lilly
Each client currently shares their own process (I use svc and a bash script to basically launch manage.py runfcgi from their own dir) .. I don't have many, so it's not really an issue. How would I allow for them to restart their own using a deploy-code script? The daemontool files are in `/etc/service/<appname>/` so they don't have permission to touch those, nor do they have permission to use `svc -d/-u` to restart services. Thanks for the help so far, though!
Bartek
I'm not entirely sure what it takes to restart a fcgi process. If its similar to mod_wsgi, you can just touch the wsgi file (apache monitors the mtime). Alternatively, you could grant access to your bash script and just make it smarter (so it handles user permissions properly). Or you could rethink your deployment to allow 1 process per app, then they could restart their own process as often as they like. Not sure which fits for you, but the above script answers the question you asked.
Justin Lilly