From d4e0283c7543e3567533ed1e3566544c1b2efc0b Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 20 Sep 2018 22:16:48 -0700 Subject: [PATCH] sync 100 offline heartbeats at 10 per request --- wakatime/arguments.py | 6 +++--- wakatime/constants.py | 9 +++++++++ wakatime/main.py | 15 +++++++++++++-- wakatime/offlinequeue.py | 5 +++-- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/wakatime/arguments.py b/wakatime/arguments.py index 93ca291..34bd999 100644 --- a/wakatime/arguments.py +++ b/wakatime/arguments.py @@ -20,7 +20,7 @@ import traceback from .__about__ import __version__ from .compat import basestring from .configs import parseConfigFile -from .constants import AUTH_ERROR +from .constants import AUTH_ERROR, DEFAULT_SYNC_OFFLINE_ACTIVITY from .packages import argparse @@ -245,9 +245,9 @@ def parse_arguments(): parser.error('argument --entity is required') if not args.sync_offline_activity: - args.sync_offline_activity = '5' + args.sync_offline_activity = DEFAULT_SYNC_OFFLINE_ACTIVITY if args.sync_offline_activity == 'none': - args.sync_offline_activity = '0' + args.sync_offline_activity = 0 try: args.sync_offline_activity = int(args.sync_offline_activity) if args.sync_offline_activity < 0: diff --git a/wakatime/constants.py b/wakatime/constants.py index 0ee51be..938d673 100644 --- a/wakatime/constants.py +++ b/wakatime/constants.py @@ -45,3 +45,12 @@ Files larger than this in bytes will not have a line count stat for performance. Default is 2MB. """ MAX_FILE_SIZE_SUPPORTED = 2000000 + +""" Default number of offline heartbeats to sync before exiting.""" +DEFAULT_SYNC_OFFLINE_ACTIVITY = 100 + +""" Number of heartbeats per api request. +Even when sending more heartbeats, this is the number of heartbeats sent per +individual https request to the WakaTime API. +""" +HEARTBEATS_PER_REQUEST = 10 diff --git a/wakatime/main.py b/wakatime/main.py index dfd2b61..42c227e 100644 --- a/wakatime/main.py +++ b/wakatime/main.py @@ -24,7 +24,7 @@ from .__about__ import __version__ from .api import send_heartbeats from .arguments import parse_arguments from .compat import u, json -from .constants import SUCCESS, UNKNOWN_ERROR +from .constants import SUCCESS, UNKNOWN_ERROR, HEARTBEATS_PER_REQUEST from .logger import setup_logging log = logging.getLogger('WakaTime') @@ -63,11 +63,22 @@ def execute(argv=None): msg=u(ex), )) - retval = send_heartbeats(heartbeats, args, configs) + retval = SUCCESS + while heartbeats: + retval = send_heartbeats(heartbeats[:HEARTBEATS_PER_REQUEST], args, configs) + heartbeats = heartbeats[HEARTBEATS_PER_REQUEST:] + if retval != SUCCESS: + break + + if heartbeats: + Queue(args, configs).push_many(heartbeats) + if retval == SUCCESS: queue = Queue(args, configs) for offline_heartbeats in queue.pop_many(args.sync_offline_activity): retval = send_heartbeats(offline_heartbeats, args, configs) + if retval != SUCCESS: + break return retval diff --git a/wakatime/offlinequeue.py b/wakatime/offlinequeue.py index 94318e1..319f0d4 100644 --- a/wakatime/offlinequeue.py +++ b/wakatime/offlinequeue.py @@ -15,6 +15,7 @@ import os from time import sleep from .compat import json +from .constants import DEFAULT_SYNC_OFFLINE_ACTIVITY, HEARTBEATS_PER_REQUEST from .heartbeat import Heartbeat @@ -104,7 +105,7 @@ class Queue(object): def pop_many(self, limit=None): if limit is None: - limit = 5 + limit = DEFAULT_SYNC_OFFLINE_ACTIVITY heartbeats = [] @@ -115,7 +116,7 @@ class Queue(object): break heartbeats.append(heartbeat) count += 1 - if count % 10 == 0: + if count % HEARTBEATS_PER_REQUEST == 0: yield heartbeats heartbeats = []