send batches of at most 10 heartbeats to api

This commit is contained in:
Alan Hamlett 2018-09-20 10:14:25 -07:00
parent 8809c55765
commit b488cd537c
3 changed files with 12 additions and 9 deletions

View file

@ -239,12 +239,12 @@ class OfflineQueueTestCase(TestCase):
queue = Queue(None, None) queue = Queue(None, None)
self.assertEquals(queue._get_db_file(), fh.name) self.assertEquals(queue._get_db_file(), fh.name)
saved_heartbeats = queue.pop_many() saved_heartbeats = next(queue.pop_many())
self.assertNothingPrinted() self.assertNothingPrinted()
self.assertNothingLogged(logs) self.assertNothingLogged(logs)
# make sure only heartbeats with error code responses were saved # make sure only heartbeats with error code responses were saved
self.assertEquals(len(saved_heartbeats), 2) self.assertEquals(sum(1 for x in saved_heartbeats), 2)
self.assertEquals(saved_heartbeats[0].entity, os.path.realpath(os.path.join(tempdir, entities[1]))) self.assertEquals(saved_heartbeats[0].entity, os.path.realpath(os.path.join(tempdir, entities[1])))
self.assertEquals(saved_heartbeats[1].entity, os.path.realpath(os.path.join(tempdir, entities[3]))) self.assertEquals(saved_heartbeats[1].entity, os.path.realpath(os.path.join(tempdir, entities[3])))
@ -295,7 +295,7 @@ class OfflineQueueTestCase(TestCase):
queue = Queue(None, None) queue = Queue(None, None)
self.assertEquals(queue._get_db_file(), fh.name) self.assertEquals(queue._get_db_file(), fh.name)
saved_heartbeats = queue.pop_many() saved_heartbeats = next(queue.pop_many())
self.assertNothingPrinted() self.assertNothingPrinted()
# make sure all offline heartbeats were sent, so queue should only have 1 heartbeat left from the second 500 response # make sure all offline heartbeats were sent, so queue should only have 1 heartbeat left from the second 500 response
@ -346,7 +346,7 @@ class OfflineQueueTestCase(TestCase):
queue = Queue(None, None) queue = Queue(None, None)
self.assertEquals(queue._get_db_file(), fh.name) self.assertEquals(queue._get_db_file(), fh.name)
saved_heartbeats = queue.pop_many() saved_heartbeats = next(queue.pop_many())
self.assertNothingPrinted() self.assertNothingPrinted()
expected = "WakaTime WARNING Missing 4 results from api.\nWakaTime WARNING Missing 2 results from api." expected = "WakaTime WARNING Missing 4 results from api.\nWakaTime WARNING Missing 2 results from api."
@ -398,7 +398,7 @@ class OfflineQueueTestCase(TestCase):
# offline queue should still have saved heartbeats # offline queue should still have saved heartbeats
queue = Queue(None, None) queue = Queue(None, None)
saved_heartbeats = queue.pop_many() saved_heartbeats = next(queue.pop_many())
self.assertNothingPrinted() self.assertNothingPrinted()
self.assertEquals(len(saved_heartbeats), 2) self.assertEquals(len(saved_heartbeats), 2)
@ -444,7 +444,7 @@ class OfflineQueueTestCase(TestCase):
# offline queue should still have saved heartbeats # offline queue should still have saved heartbeats
queue = Queue(None, None) queue = Queue(None, None)
saved_heartbeats = queue.pop_many() saved_heartbeats = next(queue.pop_many())
self.assertNothingPrinted() self.assertNothingPrinted()
self.assertEquals(len(saved_heartbeats), 2) self.assertEquals(len(saved_heartbeats), 2)

View file

@ -66,8 +66,7 @@ def execute(argv=None):
retval = send_heartbeats(heartbeats, args, configs) retval = send_heartbeats(heartbeats, args, configs)
if retval == SUCCESS: if retval == SUCCESS:
queue = Queue(args, configs) queue = Queue(args, configs)
offline_heartbeats = queue.pop_many(args.sync_offline_activity) for offline_heartbeats in queue.pop_many(args.sync_offline_activity):
if len(offline_heartbeats) > 0:
retval = send_heartbeats(offline_heartbeats, args, configs) retval = send_heartbeats(offline_heartbeats, args, configs)
return retval return retval

View file

@ -115,8 +115,12 @@ class Queue(object):
break break
heartbeats.append(heartbeat) heartbeats.append(heartbeat)
count += 1 count += 1
if count % 10 == 0:
yield heartbeats
heartbeats = []
return heartbeats if heartbeats:
yield heartbeats
def _get_db_file(self): def _get_db_file(self):
home = '~' home = '~'