new --timeout argument to configure request timeout in seconds

This commit is contained in:
Alan Hamlett 2015-09-15 09:01:17 -07:00
parent 032bd99d97
commit a773c88921

View file

@ -146,6 +146,8 @@ def parseArguments():
help='defaults to ~/.wakatime.log') help='defaults to ~/.wakatime.log')
parser.add_argument('--apiurl', dest='api_url', parser.add_argument('--apiurl', dest='api_url',
help='heartbeats api url; for debugging with a local server') help='heartbeats api url; for debugging with a local server')
parser.add_argument('--timeout', dest='timeout', type=int,
help='number of seconds to wait when sending heartbeats to api')
parser.add_argument('--config', dest='config', parser.add_argument('--config', dest='config',
help='defaults to ~/.wakatime.conf') help='defaults to ~/.wakatime.conf')
parser.add_argument('--verbose', dest='verbose', action='store_true', parser.add_argument('--verbose', dest='verbose', action='store_true',
@ -221,6 +223,11 @@ def parseArguments():
args.logfile = configs.get('settings', 'logfile') args.logfile = configs.get('settings', 'logfile')
if not args.api_url and configs.has_option('settings', 'api_url'): if not args.api_url and configs.has_option('settings', 'api_url'):
args.api_url = configs.get('settings', 'api_url') args.api_url = configs.get('settings', 'api_url')
if not args.timeout and configs.has_option('settings', 'timeout'):
try:
args.timeout = int(configs.get('settings', 'timeout'))
except ValueError:
print(traceback.format_exc())
return args, configs return args, configs
@ -278,12 +285,14 @@ def get_user_agent(plugin):
def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None, entity=None, def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None, entity=None,
timestamp=None, isWrite=None, plugin=None, offline=None, entity_type='file', timestamp=None, isWrite=None, plugin=None, offline=None, entity_type='file',
hidefilenames=None, proxy=None, api_url=None, **kwargs): hidefilenames=None, proxy=None, api_url=None, timeout=None, **kwargs):
"""Sends heartbeat as POST request to WakaTime api server. """Sends heartbeat as POST request to WakaTime api server.
""" """
if not api_url: if not api_url:
api_url = 'https://wakatime.com/api/v1/heartbeats' api_url = 'https://wakatime.com/api/v1/heartbeats'
if not timeout:
timeout = 30
log.debug('Sending heartbeat to api at %s' % api_url) log.debug('Sending heartbeat to api at %s' % api_url)
data = { data = {
'time': timestamp, 'time': timestamp,
@ -342,7 +351,7 @@ def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None,
response = None response = None
try: try:
response = session.post(api_url, data=request_body, headers=headers, response = session.post(api_url, data=request_body, headers=headers,
proxies=proxies) proxies=proxies, timeout=timeout)
except RequestException: except RequestException:
exception_data = { exception_data = {
sys.exc_info()[0].__name__: u(sys.exc_info()[1]), sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
@ -425,6 +434,7 @@ def execute(argv=None):
kwargs['branch'] = branch kwargs['branch'] = branch
kwargs['stats'] = stats kwargs['stats'] = stats
kwargs['hostname'] = args.hostname or socket.gethostname() kwargs['hostname'] = args.hostname or socket.gethostname()
kwargs['timeout'] = args.timeout
if send_heartbeat(**kwargs): if send_heartbeat(**kwargs):
queue = Queue() queue = Queue()
@ -447,6 +457,7 @@ def execute(argv=None):
entity_type=heartbeat['type'], entity_type=heartbeat['type'],
proxy=args.proxy, proxy=args.proxy,
api_url=args.api_url, api_url=args.api_url,
timeout=args.timeout,
) )
if not sent: if not sent:
break break