Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def experiment_completed(self):
"""Checks the current state of the experiment to see whether it has
completed"""
status_url = 'https://{}.herokuapp.com/summary'.format(
app_name(self.app_id)
)
data = {}
try:
resp = requests.get(status_url)
data = resp.json()
except (ValueError, requests.exceptions.RequestException):
logger.exception('Error fetching experiment status.')
logger.debug('Current application state: {}'.format(data))
return data.get('completed', False)
"--app", app_name(app)
])
log("Removing addons...")
addons = [
"heroku-postgresql",
# "papertrail",
"heroku-redis",
]
for addon in addons:
subprocess.check_call([
"heroku",
"addons:destroy", addon,
"--app", app_name(app),
"--confirm", app_name(app)
])
subprocess.check_call(
["git", "commit", "-m", '"Save URLs for database and notifications"'],
stdout=out
)
time.sleep(0.25)
# Launch the Heroku app.
log("Pushing code to Heroku...")
subprocess.check_call(
["git", "push", "heroku", "HEAD:master"],
stdout=out,
stderr=out
)
log("Scaling up the dynos...")
scale_up_dynos(app_name(id))
time.sleep(8)
# Launch the experiment.
log("Launching the experiment on MTurk...")
launch_data = _handle_launch_data('https://{}.herokuapp.com/launch'.format(app_name(id)))
log("URLs:")
log("App home: https://{}.herokuapp.com/".format(app_name(id)), chevrons=False)
log("Initial recruitment: {}".format(launch_data.get('recruitment_url', None)), chevrons=False)
# Return to the branch whence we came.
os.chdir(cwd)
log("Completed deployment of experiment " + id + ".")
subprocess.check_call(
"git push heroku HEAD:master", stdout=out, stderr=out, shell=True)
log("Scaling up the dynos...")
scale_up_dynos(app_name(id))
time.sleep(8)
# Launch the experiment.
log("Launching the experiment on MTurk...")
launch_request = requests.post('https://{}.herokuapp.com/launch'.format(app_name(id)))
launch_data = launch_request.json()
log("URLs:")
log("App home: https://{}.herokuapp.com/".format(app_name(id)), chevrons=False)
log("Initial recruitment: {}".format(launch_data.get('recruitment_url', None)), chevrons=False)
# Return to the branch whence we came.
os.chdir(cwd)
log("Completed deployment of experiment " + id + ".")
def get_summary(app):
r = requests.get('https://{}.herokuapp.com/summary'.format(app_name(app)))
summary = r.json()['summary']
out = []
out.append("\nstatus \t| count")
out.append("----------------")
for s in summary:
out.append("{}\t| {}".format(s[0], s[1]))
num_101s = sum([s[1] for s in summary if s[0] == 101])
num_10xs = sum([s[1] for s in summary if s[0] >= 100])
if num_10xs > 0:
out.append("\nYield: {:.2%}".format(1.0 * num_101s / num_10xs))
return "\n".join(out)
"https://github.com/stomita/heroku-buildpack-phantomjs",
])
database_size = config.get('database_size')
# Set up postgres database and AWS environment variables.
cmds = [
["heroku", "addons:create", "heroku-postgresql:{}".format(quote(database_size))],
["heroku", "addons:create", "heroku-redis:premium-0"],
["heroku", "addons:create", "papertrail"],
]
for cmd in cmds:
subprocess.check_call(cmd + ["--app", app_name(id)], stdout=out)
heroku_config = {
"HOST": "{}.herokuapp.com".format(app_name(id)),
"aws_access_key_id": config["aws_access_key_id"],
"aws_secret_access_key": config["aws_secret_access_key"],
"aws_region": config["aws_region"],
"auto_recruit": config["auto_recruit"],
"dallinger_email_username": config["dallinger_email_address"],
"dallinger_email_key": config["dallinger_email_password"],
"whimsical": config["whimsical"],
}
for key in heroku_config:
subprocess.check_call([
"heroku",
"config:set",
"{}={}".format(key, quote(str(heroku_config[key]))),
"--app", app_name(id)
], stdout=out)
def experiment_completed(self):
"""Checks the current state of the experiment to see whether it has
completed"""
status_url = 'https://{}.herokuapp.com/summary'.format(
app_name(self.app_id)
)
data = {}
try:
resp = requests.get(status_url)
data = resp.json()
except (ValueError, requests.exceptions.RequestException):
logger.exception('Error fetching experiment status.')
logger.debug('Current application state: {}'.format(data))
return data.get('completed', False)
subprocess.check_call(["heroku", "pg:wait", "--app", app_name(id)])
bucket = data.user_s3_bucket()
key = bucket.lookup('{}.dump'.format(id))
url = key.generate_url(expires_in=300)
subprocess.check_call([
"heroku", "pg:backups", "restore", "'{}'".format(url), "DATABASE_URL",
"--app", app_name(id),
"--confirm", app_name(id),
])
subprocess.check_call([
"heroku", "addons:create", "heroku-redis:premium-0",
"--app", app_name(id)
])
# Scale up the dynos.
log("Scaling up the dynos...")
scale_up_dynos(app_name(id))
def logs(app):
"""Show the logs."""
if app is None:
raise TypeError("Select an experiment using the --app flag.")
else:
subprocess.check_call(
"heroku addons:open papertrail --app " + app_name(app),
shell=True)
ready = False
while not ready:
redis_URL = subprocess.check_output(
"heroku config:get REDIS_URL --app {}".format(app_name(id)),
shell=True
)
r = redis.from_url(redis_URL)
try:
r.set("foo", "bar")
ready = True
except redis.exceptions.ConnectionError:
time.sleep(2)
log("Saving the URL of the postgres database...")
db_url = subprocess.check_output(
"heroku config:get DATABASE_URL --app " + app_name(id), shell=True)
# Set the notification URL and database URL in the config file.
config.extend({
"notification_url": u"http://" + app_name(id) + ".herokuapp.com/notifications",
"database_url": db_url.rstrip().decode('utf8'),
})
config.write_config()
subprocess.check_call("git add config.txt", stdout=out, shell=True),
time.sleep(0.25)
subprocess.check_call(
'git commit -m "Save URLs for database and notifications"',
stdout=out,
shell=True)
time.sleep(0.25)
# Launch the Heroku app.