Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overall timeouts #47

Merged
merged 4 commits into from
Sep 22, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions centinel/backend.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import glob
import uuid
import json
import requests
import logging
import os
import requests
import time
import uuid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change ordering of imports?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made alphabetical

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


class User:
Expand Down Expand Up @@ -50,9 +51,11 @@ def submit_result(self, file_name):
files = {'result': result_file}
url = "%s/%s" % (self.config['server']['server_url'], "results")
req = requests.post(url, proxies=self.config['proxy']['proxy'],
files=files, auth=self.auth)
files=files, auth=self.auth,
timeout=self.config['server']['req_timeout'])

req.raise_for_status()
os.remove(file_name)

def download_experiment(self, name):
logging.info("Downloading experiment - %s", name)
Expand Down Expand Up @@ -99,6 +102,7 @@ def create_user(self):
def sync(config):
logging.info("Starting sync with %s", config['server']['server_url'])

start = time.time()
try:
user = User(config)
except Exception, e:
Expand All @@ -113,6 +117,8 @@ def sync(config):
user.submit_result(path)
except Exception, e:
logging.error("Unable to send result file: %s" % str(e))
if time.time() - start > config['server']['total_timeout']:
logging.error("Interaction with server took too long. Preempting")

# get all experiment names
available_experiments = []
Expand All @@ -121,13 +127,17 @@ def sync(config):
file_name, _ = os.path.splitext(os.path.basename(path))
available_experiments.append(file_name)
available_experiments = set(available_experiments)
if time.time() - start > config['server']['total_timeout']:
logging.error("Interaction with server took too long. Preempting")

# download new experiments from server
try:
map(user.download_experiment,
set(user.experiments) - available_experiments)
except Exception, e:
logging.error("Unable to download experiment files %s", str(e))
for experiment in (set(user.experiments) - available_experiments):
try:
user.download_experiment(experiment)
except Exception, e:
logging.error("Unable to download experiment files %s", str(e))
if time.time() - start > config['server']['total_timeout']:
logging.error("Interaction with server took too long. Preempting")

logging.info("Finished sync with %s", config['server']['server_url'])

Expand Down
7 changes: 6 additions & 1 deletion centinel/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ def __init__(self,):

# server
servers = {}
servers['server_url'] = "http://130.245.145.7:8082"
servers['server_url'] = "http://server.iclab.org:8082"
servers['login_file'] = os.path.join(self.params['user']['centinel_home'],
'login')
# the entire transaction should take less than 5 min
servers['total_timeout'] = 60*5
# each request should timeout if we go 15 seconds without
# hearing from the other party
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Each request should timeout in 15 seconds"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a general thought - you should try to keep comments as concise as possible. Make the code clearer instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that in general, my comments should be more concise, but I want to indicate here that this is not a timeout on the complete request: it is for the socket. requests does not offer a way to do complete request timeouts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the comment doesn't say that either? it doesn't say anything about sockets

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update the comment

servers['req_timeout'] = 15
self.params['server'] = servers

# proxy
Expand Down