This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
| b5918c8f » | brosner | 2008-11-03 | 1 | import os | |
| 2 | import signal | ||||
| 3 | |||||
| 4 | from optparse import OptionParser, make_option | ||||
| 5 | from django.core.management.base import BaseCommand | ||||
| 6 | from django.core.handlers.wsgi import WSGIHandler | ||||
| 7 | from django.conf import settings | ||||
| 8 | |||||
| 9 | try: | ||||
| 10 | from cherrypy.wsgiserver import CherryPyWSGIServer | ||||
| 11 | except ImportError: | ||||
| 12 | from wsgiserver import CherryPyWSGIServer | ||||
| 13 | |||||
| 14 | DEFAULT_HOST = getattr(settings, "WSGI_HOST", "127.0.0.1") | ||||
| 15 | DEFAULT_PORT = getattr(settings, "WSGI_PORT", 8001) | ||||
| 16 | |||||
| 17 | class Command(BaseCommand): | ||||
| 18 | option_list = BaseCommand.option_list + ( | ||||
| 19 | make_option("-h", "--host", dest="host", default="127.0.0.1"), | ||||
| 20 | make_option("-p", "--port", dest="port", default=8001), | ||||
| 21 | make_option("-d", "--daemon", dest="daemonize", action="store_true"), | ||||
| 22 | ) | ||||
| 23 | requires_model_validation = False | ||||
| 24 | |||||
| 25 | def handle(self, *args, **options): | ||||
| 26 | self.server = CherryPyWSGIServer((options["host"], options["port"]), WSGIHandler()) | ||||
| 27 | self.pidfile = os.path.join(settings.PROJECT_ROOT, "logs/wsgi.pid") | ||||
| 28 | try: | ||||
| 29 | action = args[0] | ||||
| 30 | except IndexError: | ||||
| 31 | print "You must provide an action. Possible actions are start, stop and restart." | ||||
| 32 | raise SystemExit | ||||
| 33 | if options["daemonize"]: | ||||
| 34 | daemonize() | ||||
| 35 | if action == "start": | ||||
| 36 | self.start() | ||||
| 37 | elif action == "stop": | ||||
| 38 | pid = open(self.pidfile, "r").read() | ||||
| 39 | self.stop(pid) | ||||
| 40 | elif action == "restart": | ||||
| 41 | pid = open(self.pidfile, "r").read() | ||||
| 42 | self.restart(pid) | ||||
| 43 | |||||
| 44 | def start(self): | ||||
| 45 | writepid(self.pidfile) | ||||
| 46 | try: | ||||
| 47 | self.server.start() | ||||
| 48 | except KeyboardInterrupt: | ||||
| 49 | # likely not a daemon so make sure to shutdown properly. | ||||
| 50 | self.server.stop() | ||||
| 51 | |||||
| 52 | def stop(self, pid): | ||||
| 53 | os.kill(int(pid), signal.SIGHUP) | ||||
| 54 | |||||
| 55 | def restart(self, pid): | ||||
| 56 | self.stop(pid) | ||||
| 57 | self.start() | ||||
| 58 | |||||
| 59 | def create_parser(self, prog_name, subcommand): | ||||
| 60 | """ | ||||
| 61 | Create and return the ``OptionParser`` which will be used to | ||||
| 62 | parse the arguments to this command. | ||||
| 63 | """ | ||||
| 64 | return OptionParser(prog=prog_name, usage=self.usage(subcommand), | ||||
| 65 | version = self.get_version(), | ||||
| 66 | option_list = self.option_list, | ||||
| 67 | conflict_handler = "resolve") | ||||
| 68 | |||||
| 69 | def writepid(pid_file): | ||||
| 70 | """ | ||||
| 71 | Write the process ID to disk. | ||||
| 72 | """ | ||||
| 73 | fp = open(pid_file, "w") | ||||
| 74 | fp.write(str(os.getpid())) | ||||
| 75 | fp.close() | ||||
| 76 | |||||
| 77 | def daemonize(): | ||||
| 78 | """ | ||||
| 79 | Detach from the terminal and continue as a daemon. | ||||
| 80 | """ | ||||
| 81 | # swiped from twisted/scripts/twistd.py | ||||
| 82 | # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16 | ||||
| 83 | if os.fork(): # launch child and... | ||||
| 84 | os._exit(0) # kill off parent | ||||
| 85 | os.setsid() | ||||
| 86 | if os.fork(): # launch child and... | ||||
| 87 | os._exit(0) # kill off parent again. | ||||
| 88 | os.umask(077) | ||||
| 89 | null = os.open("/dev/null", os.O_RDWR) | ||||
| 90 | for i in range(3): | ||||
| 91 | try: | ||||
| 92 | os.dup2(null, i) | ||||
| 93 | except OSError, e: | ||||
| 94 | if e.errno != errno.EBADF: | ||||
| 95 | raise | ||||
| 96 | os.close(null) | ||||
| 97 | |||||







