Bartek
Bartek

Reputation: 15599

Django: Gracefully restart nginx + fastcgi sites to reflect code changes?

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

Upvotes: 1

Views: 1329

Answers (1)

Justin Abrahms
Justin Abrahms

Reputation: 1421

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

Upvotes: 1

Related Questions