-
Notifications
You must be signed in to change notification settings - Fork 1
/
passenger_wsgi.py
73 lines (64 loc) · 2.05 KB
/
passenger_wsgi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import sys
import logging
from logging.handlers import RotatingFileHandler
DEBUG = False
cwd = os.getcwd()
SUB_DOMAIN = os.path.basename(cwd)
LOG_FILENAME = os.path.join(cwd, 'logs', 'passanger', 'passenger_wsgi.log')
VIRTUALENV = os.path.join(cwd, 'env')
logLevel = logging.INFO
if DEBUG:
logLevel = logging.NOTSET
logger = logging.getLogger(__name__)
logger.setLevel(1)
logHandler = RotatingFileHandler(
filename=LOG_FILENAME,
mode='a',
maxBytes=5000000,
backupCount=5,
encoding=None,
delay=0)
logHandler.setLevel(logLevel)
logHandler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
logger.addHandler(logHandler)
logger.debug('RotatingFileHandler logging started')
# verify we are in the virtual environment
if sys.prefix != VIRTUALENV:
logger.debug('Detected wrong sys.prefix: {path}'.format(path=sys.prefix))
execfile(
os.path.join(VIRTUALENV, 'bin', 'activate_this.py'),
dict(__file__=os.path.join(VIRTUALENV, 'bin', 'activate_this.py')))
out = ['Running python:']
out += [' executable: "{exe}"']
out += [' version: "{ver}"']
out += [' prefix: "{prfx}"']
logger.debug('\n'.join(out).format(
exe=sys.executable,
ver=sys.version,
prfx=sys.prefix))
# import web application
sys.path.append(cwd)
try:
import myAppModule
except ImportError as err:
logger.exception('Error importing module: "{e}"'.format(e=err))
# start the application
def application(environ, start_response):
results = []
logger.debug('Application called')
if DEBUG:
out = ['environ:']
for k in environ:
out.append(' {k!r}: {val!r}'.format(k=k, val=environ[k]))
logger.debug('\n'.join(out))
try:
results = myAppModule.application(environ, start_response)
logger.debug('Application executed successfully')
except Exception, inst:
logger.exception('Error: {0}'.format(type(inst)))
logger.debug('Application call done')
if DEBUG:
logHandler.flush()
return results
logger.debug('Application initilization compleated')