From a773c889218194961c0dcb23b03445b898f5d3bf Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Tue, 15 Sep 2015 09:01:17 -0700 Subject: [PATCH] new --timeout argument to configure request timeout in seconds --- wakatime/main.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/wakatime/main.py b/wakatime/main.py index 2af2fcb..aa0d87d 100644 --- a/wakatime/main.py +++ b/wakatime/main.py @@ -146,6 +146,8 @@ def parseArguments(): help='defaults to ~/.wakatime.log') parser.add_argument('--apiurl', dest='api_url', 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', help='defaults to ~/.wakatime.conf') parser.add_argument('--verbose', dest='verbose', action='store_true', @@ -221,6 +223,11 @@ def parseArguments(): args.logfile = configs.get('settings', 'logfile') if not args.api_url and configs.has_option('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 @@ -278,12 +285,14 @@ def get_user_agent(plugin): 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', - 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. """ if not api_url: api_url = 'https://wakatime.com/api/v1/heartbeats' + if not timeout: + timeout = 30 log.debug('Sending heartbeat to api at %s' % api_url) data = { 'time': timestamp, @@ -342,7 +351,7 @@ def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None, response = None try: response = session.post(api_url, data=request_body, headers=headers, - proxies=proxies) + proxies=proxies, timeout=timeout) except RequestException: exception_data = { sys.exc_info()[0].__name__: u(sys.exc_info()[1]), @@ -425,6 +434,7 @@ def execute(argv=None): kwargs['branch'] = branch kwargs['stats'] = stats kwargs['hostname'] = args.hostname or socket.gethostname() + kwargs['timeout'] = args.timeout if send_heartbeat(**kwargs): queue = Queue() @@ -447,6 +457,7 @@ def execute(argv=None): entity_type=heartbeat['type'], proxy=args.proxy, api_url=args.api_url, + timeout=args.timeout, ) if not sent: break