Skip to content

Instantly share code, notes, and snippets.

@jhamrick
Last active December 30, 2019 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jhamrick/044c3607979eea2123ae to your computer and use it in GitHub Desktop.
Save jhamrick/044c3607979eea2123ae to your computer and use it in GitHub Desktop.
Passenger configuration for psiTurk on DreamHost
import sys
import os
import logging
from logging.handlers import RotatingFileHandler
# This script assumes you have created a subdomain on DreamHost
# that is configured for Passenger. It also assumes you have created
# a Python virtual environment in the root of that subdomain and
# installed psiTurk inside it.
# Check whether the currently running python process is the same
# as the python executable in your virtualenv. If not, exec that
# process so that we use the correct version of python.
base_path = "/path/to/your/subdomain/folder"
INTERP = os.path.join(base_path, 'bin', 'python')
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
# Get the path to the experiment, and change to that directory
# so that psiTurk can find out experiment files.
os.chdir(os.path.join(base_path, 'experiment'))
cwd = os.getcwd()
sys.path.append(cwd)
# Set up a few variables for our logger -- we'll log to the file
# called 'passenger_wsgi.log' in the directory for our experiment,
# and also use a somewhat more informative log format.
logfilename = os.path.join(cwd, 'passenger_wsgi.log')
logformat = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"
loglevel = logging.INFO
# Create a rotating file handler for our passenger_wsgi.log, so
# that we don't end up with crazy gigantic log files.
handler = RotatingFileHandler(
logfilename, maxBytes=1048576, backupCount=5)
handler.setLevel(loglevel)
handler.setFormatter(logging.Formatter(logformat))
# Configure the global logger with our defaults
logging.basicConfig(filename=logfilename, format=logformat, level=loglevel)
# Configure passenger's logger with our defaults
logger = logging.getLogger("passenger_wsgi")
logger.addHandler(handler)
logger.setLevel(loglevel)
# Start the Flask application
logger.debug("Starting application")
from psiturk.experiment import app
# Configure the psiTurk and Werkzeug loggers as well
app.logger.setLevel(loglevel)
app.logger.addHandler(handler)
werkzeug_logger = logging.getLogger('werkzeug')
werkzeug_logger.setLevel(loglevel)
werkzeug_logger.addHandler(handler)
def application(environ, start_response):
"""Expose the application to passenger. This function gets called when
passenger redirects requests to the Flask app."""
ip = environ.get('REMOTE_ADDR', '-')
method = environ.get('REQUEST_METHOD', '-')
uri = environ.get('REQUEST_URI', '-')
referer = environ.get('HTTP_REFERER', '-')
user_agent = environ.get('HTTP_USER_AGENT', '-')
logger.debug('%s "%s %s" "%s" "%s"', ip, method, uri, referer, user_agent)
try:
result = app(environ, start_response)
except Exception, e:
logger.error("%s", str(e))
raise
return result
# Uncomment next two lines to enable debugging
#from werkzeug.debug import DebuggedApplication
#application = DebuggedApplication(application, evalex=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment