Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gureckis committed Jul 23, 2014
2 parents 1cf9ae9 + a07132e commit 6c7e2fb
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 130 deletions.
2 changes: 1 addition & 1 deletion doc/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Installation of **psiTurk** requires:
4. **A web browser.** A WebKit compatible browser such as FireFox,
Safari, or Chrome is recommended.

An addition requirement for actually using **psiTurk** to run experiments
An additional requirement for actually using **psiTurk** to run experiments
is a Internet connected computer capable of receiving incoming requests.

Installation steps
Expand Down
20 changes: 10 additions & 10 deletions psiturk/amt_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def verify_aws_login(self):
print exception.error_message
return False
except AttributeError:
print "*** Unable to establish connection to AWS region %s \
using your access key/secret key", self.region
print "*** Unable to establish connection to AWS region %s "\
"using your access key/secret key", self.region
return False
except boto.exception.BotoServerError:
print "***********************************************************"
Expand All @@ -151,8 +151,8 @@ def verify_aws_login(self):
def connect_to_aws_rds(self):
''' Connec to aws rds '''
if not self.valid_login:
print 'Sorry, unable to connect to Amazon\'s RDS database server. \
AWS credentials invalid.'
print 'Sorry, unable to connect to Amazon\'s RDS database server. "\
"AWS credentials invalid.'
return False
# rdsparams = dict(
# aws_access_key_id = self.aws_access_key_id,
Expand Down Expand Up @@ -307,8 +307,8 @@ def __init__(self, aws_access_key_id, aws_secret_access_key, is_sandbox):
if not self.valid_login:
print 'WARNING *****************************'
print 'Sorry, AWS Credentials invalid.\nYou will only be able to '\
+ 'test experiments locally until you enter\nvalid '\
+ 'credentials in the AWS Access section of ~/.psiturkconfig\n'
'test experiments locally until you enter\nvalid '\
'credentials in the AWS Access section of ~/.psiturkconfig\n'

def update_credentials(self, aws_access_key_id, aws_secret_access_key):
''' Update credentials '''
Expand Down Expand Up @@ -482,8 +482,8 @@ def verify_aws_login(self):
def connect_to_turk(self):
''' Connect to turk '''
if not self.valid_login:
print 'Sorry, unable to connect to Amazon Mechanical Turk. AWS \
credentials invalid.'
print 'Sorry, unable to connect to Amazon Mechanical Turk. AWS '\
'credentials invalid.'
return False
if self.is_sandbox:
host = 'mechanicalturk.sandbox.amazonaws.com'
Expand Down Expand Up @@ -571,8 +571,8 @@ def dispose_hit(self, hitid):
try:
self.mtc.dispose_hit(hitid)
except Exception, e:
print 'Failed to dispose of HIT %s. Make sure there are no \
assignments remaining to be reviewed' % hitid
print 'Failed to dispose of HIT %s. Make sure there are no "\
"assignments remaining to be reviewed' % hitid

def extend_hit(self, hitid, assignments_increment=None,
expiration_increment=None):
Expand Down
7 changes: 6 additions & 1 deletion psiturk/experiment_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from psiturk_config import PsiturkConfig
import sys
import setproctitle
import os

config = PsiturkConfig()
config.load_config()
Expand All @@ -24,7 +25,11 @@ def __init__(self):
self.options = self.user_options
self.prog = None
self.do_load_config()
print "Now serving on", "http://" + self.options["bind"]
if 'OPENSHIFT_SECRET_TOKEN' in os.environ:
my_ip = os.environ['OPENSHIFT_APP_DNS']
print "Now serving on " + os.environ['OPENSHIFT_APP_DNS']
else:
print "Now serving on", "http://" + self.options["bind"]

def init(self, *args):
'''init method
Expand Down
28 changes: 22 additions & 6 deletions psiturk/experiment_server_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import urllib2
import socket
import psutil
import time


#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Expand Down Expand Up @@ -118,21 +119,35 @@ def shutdown(self, ppid=None):
else:
self.server_running = False

def kill_child_processes(self, parent_pid, sig=signal.SIGTERM):
if os.uname()[0] is 'Linux':
ps_command = subprocess.Popen('pstree -p %d | perl -ne \'print "$1 "\
while /\((\d+)\)/g\'' % parent.pid,
shell=True, stdout=subprocess.PIPE)
ps_output = ps_command.stdout.read()
retcode = ps_command.wait()
assert retcode == 0, "ps command returned %d" % retcode
for pid_str in ps_output.split("\n")[:-1]:
os.kill(int(pid_str), sig)
if os.uname()[0] is 'Darwin':
child_pid = parent.get_children(recursive=True)
for pid in child_pid:
pid.send_signal(signal.SIGTERM)

def is_server_running(self):
PROCNAME = "psiturk_experiment_server"
cmd = "ps -eo pid,command | grep '"+ PROCNAME + "' | grep -v grep | awk '{print $1}'"
psiturk_exp_processes = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output = psiturk_exp_processes.stdout.readlines()
psiturk_exp_ports = [process[0].laddr[1] for process in [psutil.Process(int(pid)).get_connections() for pid in output]]
p = psutil.Process(psiturk_exp_processes.pid)
child_pid = p.get_children(recursive=True)
for pid in child_pid:
pid.send_signal(signal.SIGTERM)
psiturk_exp_ports = []
if output:
psiturk_exp_ports = [process[0].laddr[1] for process in [psutil.Process(int(pid)).get_connections() for pid in output]]
parent = psutil.Process(psiturk_exp_processes.pid)
self.kill_child_processes(parent.pid)
if psiturk_exp_ports:
is_psiturk_using_port = True
else:
is_psiturk_using_port = False

is_port_open = self.is_port_available()
#print self.server_running, " ", portopen
if is_port_open and is_psiturk_using_port: # This should never occur
Expand Down Expand Up @@ -164,4 +179,5 @@ def startup(self):
print "Experiment server may be already running..."
elif server_status == 'blocked':
print "Another process is running on the desired port. Try using a different port number."
time.sleep(1.2) # Allow CLI to catch up.

39 changes: 22 additions & 17 deletions psiturk/psiturk_org_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,24 @@ def __init__(self):
self.access_key = config.get('psiTurk Access', 'psiturk_access_key_id')
self.secret_key = config.get('psiTurk Access', 'psiturk_secret_access_id')
self.local_port = config.getint('Server Parameters', 'port')
self.check_os()
self.is_open = False
self.tunnel_port = 8000 # Set by tunnel server
self.tunnel_host = 'tunnel.psiturk.org'
self.tunnel_server = os.path.join(os.path.dirname(__file__),
"tunnel/ngrok")
self.tunnel_config = os.path.join(os.path.dirname(__file__),
"tunnel/ngrok-config")
self.is_open = False

@classmethod
def check_os(cls):
def is_compatible(cls):
''' Check OS '''
is_64bit = struct.calcsize('P')*8 == 64
if (_platform == "linux" or _platform == "linux2" or "win32" or not
is_64bit):
Exception('Your OS is currently unsupported.')
if (_platform == "darwin" and is_64bit):
return True
else:
print("Linux tunnels are currenlty unsupported. Please notify "\
"authors@psiturk.org\nif you'd like to see this feature.")
return False

def get_tunnel_ad_url(self):
''' Get tunnel hostname from psiturk.org '''
Expand All @@ -290,17 +292,20 @@ def change_tunnel_ad_url(self):

def open(self):
''' Open tunnel '''
tunnel_ad_url = self.get_tunnel_ad_url()
if not tunnel_ad_url:
return("Tunnel server appears to be down.")
cmd = '%s -subdomain=%s -config=%s -log=stdout %s 2>&1 > server.log' \
%(self.tunnel_server, tunnel_ad_url, self.tunnel_config,
self.local_port)
self.tunnel = subprocess.Popen(cmd, shell=True)
self.url = '%s.%s' %(tunnel_ad_url, self.tunnel_host)
self.full_url = 'http://%s.%s:%s' %(tunnel_ad_url, self.tunnel_host,
self.tunnel_port)
self.is_open = True
if self.is_compatible():
tunnel_ad_url = self.get_tunnel_ad_url()
if not tunnel_ad_url:
return("Tunnel server appears to be down.")
cmd = '%s -subdomain=%s -config=%s -log=stdout %s 2>&1 > server.log' \
%(self.tunnel_server, tunnel_ad_url, self.tunnel_config,
self.local_port)
self.tunnel = subprocess.Popen(cmd, shell=True)
self.url = '%s.%s' %(tunnel_ad_url, self.tunnel_host)
self.full_url = 'http://%s.%s:%s' %(tunnel_ad_url, self.tunnel_host,
self.tunnel_port)
self.is_open = True
print "Tunnel URL: %s" % self.full_url
print "Hint: In OSX, you can open a terminal link using cmd + click"

def close(self):
''' Close tunnel '''
Expand Down

0 comments on commit 6c7e2fb

Please sign in to comment.