sync 100 offline heartbeats at 10 per request

This commit is contained in:
Alan Hamlett 2018-09-20 22:16:48 -07:00
parent 127275dd3b
commit d4e0283c75
4 changed files with 28 additions and 7 deletions

View file

@ -20,7 +20,7 @@ import traceback
from .__about__ import __version__ from .__about__ import __version__
from .compat import basestring from .compat import basestring
from .configs import parseConfigFile from .configs import parseConfigFile
from .constants import AUTH_ERROR from .constants import AUTH_ERROR, DEFAULT_SYNC_OFFLINE_ACTIVITY
from .packages import argparse from .packages import argparse
@ -245,9 +245,9 @@ def parse_arguments():
parser.error('argument --entity is required') parser.error('argument --entity is required')
if not args.sync_offline_activity: 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': if args.sync_offline_activity == 'none':
args.sync_offline_activity = '0' args.sync_offline_activity = 0
try: try:
args.sync_offline_activity = int(args.sync_offline_activity) args.sync_offline_activity = int(args.sync_offline_activity)
if args.sync_offline_activity < 0: if args.sync_offline_activity < 0:

View file

@ -45,3 +45,12 @@ Files larger than this in bytes will not have a line count stat for performance.
Default is 2MB. Default is 2MB.
""" """
MAX_FILE_SIZE_SUPPORTED = 2000000 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

View file

@ -24,7 +24,7 @@ from .__about__ import __version__
from .api import send_heartbeats from .api import send_heartbeats
from .arguments import parse_arguments from .arguments import parse_arguments
from .compat import u, json 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 from .logger import setup_logging
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
@ -63,11 +63,22 @@ def execute(argv=None):
msg=u(ex), 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: if retval == SUCCESS:
queue = Queue(args, configs) queue = Queue(args, configs)
for offline_heartbeats in queue.pop_many(args.sync_offline_activity): for offline_heartbeats in queue.pop_many(args.sync_offline_activity):
retval = send_heartbeats(offline_heartbeats, args, configs) retval = send_heartbeats(offline_heartbeats, args, configs)
if retval != SUCCESS:
break
return retval return retval

View file

@ -15,6 +15,7 @@ import os
from time import sleep from time import sleep
from .compat import json from .compat import json
from .constants import DEFAULT_SYNC_OFFLINE_ACTIVITY, HEARTBEATS_PER_REQUEST
from .heartbeat import Heartbeat from .heartbeat import Heartbeat
@ -104,7 +105,7 @@ class Queue(object):
def pop_many(self, limit=None): def pop_many(self, limit=None):
if limit is None: if limit is None:
limit = 5 limit = DEFAULT_SYNC_OFFLINE_ACTIVITY
heartbeats = [] heartbeats = []
@ -115,7 +116,7 @@ class Queue(object):
break break
heartbeats.append(heartbeat) heartbeats.append(heartbeat)
count += 1 count += 1
if count % 10 == 0: if count % HEARTBEATS_PER_REQUEST == 0:
yield heartbeats yield heartbeats
heartbeats = [] heartbeats = []