| 81 | | #______________________________________________________________________________ |
| 82 | | # redirect stderr if logging is on: |
| 83 | | |
| 84 | | if LOGFILE: |
| 85 | | try: |
| 86 | | class StdErrorHandler(object): |
| 87 | | """ |
| 88 | | redirects messages from stderr to stdout. |
| 89 | | Sends a header, if the header were not already sent. |
| 90 | | """ |
| 91 | | def __init__(self, out_method): |
| 92 | | self.out_method = out_method |
| 93 | | |
| 94 | | def write(self, *txt): |
| 95 | | text = "".join([i for i in txt]) |
| 96 | | for line in text.splitlines(): |
| 97 | | self.out_method(line) |
| 98 | | |
| 99 | | sys.stderr = StdErrorHandler(log) |
| 100 | | sys.stderr.write("stderr redirected into the logfile") |
| 101 | | except Exception, e: |
| 102 | | log("StdErrorHandler error: %s" % e) |
| 103 | | |
| 104 | | #______________________________________________________________________________ |
| 105 | | |
| 106 | | |
| 107 | | def tb_catch_app(environ, start_response): |
| 108 | | """ Minimalistic WSGI app for debugging """ |
| 109 | | start_response('200 OK', [('Content-Type', 'text/html')]) |
| 110 | | yield '<h1>FastCGI Traceback catch</h1>' |
| 111 | | |
| 112 | | # Display the overall running time |
| 113 | | yield 'overall time: %.2fsec' % (time.time() - start_overall) |
| 114 | | |
| 115 | | # Display the last traceback |
| 116 | | yield '<h2>last_tb_info:</h2>' |
| 117 | | try: |
| 118 | | yield "<pre>%s</pre>" % last_tb_info |
| 119 | | except Exception, e: |
| 120 | | yield "Traceback error: %s" % e |
| 121 | | |
| 122 | | yield '<hr />' |
| 123 | | |
| 124 | | from cgi import escape |
| 125 | | yield '<h1>FastCGI Environment</h1><table>' |
| 126 | | for k, v in sorted(environ.items()): |
| 127 | | yield '<tr><th>%s</th><td>%s</td></tr>' % ( |
| 128 | | escape(repr(k)), escape(repr(v)) |
| 129 | | ) |
| 130 | | yield '</table>' |
| 131 | | |
| 132 | | #______________________________________________________________________________ |
| 133 | | # If this file does not lie in the project folder, then you must define the |
| 134 | | # path to the project directory here: |
| 135 | | |
| 136 | | if PROJECT_DIR: |
| 137 | | # Switch to the directory of your project. |
| 138 | | log("chdir '%s'" % PROJECT_DIR) |
| 139 | | try: |
| 140 | | os.chdir(PROJECT_DIR) |
| 141 | | except Exception, e: |
| 142 | | log("chdir error:", e) |
| 143 | | |
| 144 | | # Add a custom Python path, you'll want to add the parent folder of |
| 145 | | # your project directory. (Optional.) |
| 146 | | try: |
| 147 | | sys.path.insert(0, PROJECT_DIR) |
| 148 | | except Exception, e: |
| 149 | | log("path.insert error:", e) |
| 150 | | |
| 151 | | #______________________________________________________________________________ |
| 152 | | |
| 153 | | from django.core.handlers.wsgi import WSGIHandler |
| 154 | | from django.core.servers.fastcgi import runfastcgi |
| 155 | | |
| 156 | | try: |
| 157 | | log('runfastcgi()') |
| 158 | | runfastcgi( |
| 159 | | #method="prefork", # prefork or threaded (default prefork) |
| 160 | | #daemonize="false", # Bool, whether to detach from terminal |
| 161 | | #maxrequests=1, # number of requests a child handles before it is |
| 162 | | # killed and a new child is forked (0 = no limit) |
| 163 | | #maxspare=2, # max number of spare processes/threads |
| 164 | | #maxchildren=2 # hard limit number of processes/threads |
| 165 | | #debug=True, # Not Implemented, see: |
| 166 | | # http://code.djangoproject.com/ticket/6610 |
| 167 | | ) |
| 168 | | except SystemExit, e: |
| 169 | | log("sys.exit(%s) appears." % e) |
| 170 | | except Exception, e: |
| 171 | | log("fastCGI error: %s" % e) |
| 172 | | import sys, traceback |
| 173 | | last_tb_info = traceback.format_exc() |
| 174 | | log(last_tb_info) |
| 175 | | from flup.server.fcgi import WSGIServer |
| 176 | | WSGIServer(tb_catch_app).run() |
| 177 | | else: |
| 178 | | log("fastcgi application ended.") |
| | 86 | # Not Implemented, see: http://code.djangoproject.com/ticket/6610 |
| | 87 | #"debug":True, |
| | 88 | } |
| | 89 | ) |