forked from joeyespo/hello-redis-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passenger_wsgi.py
69 lines (53 loc) · 1.76 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
import os
import sys
from traceback import format_exc
def log(message):
__directory__ = os.path.dirname(__file__)
logfile = os.path.join(__directory__, 'passenger.log')
with open(logfile, 'a') as log:
print >>log, message
log.flush()
def create_application():
# Adjust path
sys.path.append(os.path.dirname(__file__))
# Import app
try:
from hello_redis_tasks import app
except Exception:
log('Could not load app:\n' + str(format_exc()))
return None
# Run worker
try:
from worker import TaskWorker
worker = TaskWorker(app, debug=app.debug)
worker.reset()
worker.start()
except Exception:
log('Could not load worker:\n' + str(format_exc()))
return None
# Handle request
def application(environ, start_response):
log('Application called')
try:
results = app(environ, start_response)
except Exception:
log('*** ERROR ***\n' + str(format_exc()) + '\n*************')
return results
return application
def create_error_application():
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
return ['500 Internal Error']
return application
# Init logs
log('Running ' + str(sys.executable))
# Check interpreter
INTERP = "/home/joeyespo/local/Python-2.7/bin/python"
if sys.executable != INTERP:
try:
log('Detected wrong interpreter location, swapping to, ' + INTERP)
os.execl(INTERP, INTERP, *sys.argv)
# Should resume execution from the top of the file
except:
log('Could not switch interpreters:\n' + str(format_exc()))
application = create_application() or create_error_application()