Skip to content

Commit

Permalink
Truncate event file with valid json file
Browse files Browse the repository at this point in the history
Signed-off-by: Wei-Chun, Chang <[email protected]>
  • Loading branch information
wcchang1115 committed Oct 17, 2023
1 parent 106b730 commit 1337176
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions piperider_cli/event/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import time
from datetime import datetime
from json import JSONDecodeError

import portalocker
import requests
Expand Down Expand Up @@ -64,7 +65,6 @@ def log_event(self, prop, event_type):
app_version=__version__,
)

# TODO: handle exception when writing to file
self._store_to_file(event)
if self._is_full():
self.send_events()
Expand Down Expand Up @@ -102,7 +102,10 @@ def send_events(self):

def _store_to_file(self, event):
with portalocker.Lock(self._unsend_events_file, 'r+', timeout=5) as f:
o = json.loads(f.read())
try:
o = json.loads(f.read())
except JSONDecodeError:
o = dict(unsend_events=[])
events = o.get('unsend_events', None)
if events is None:
o['unsend_events'] = []
Expand Down

0 comments on commit 1337176

Please sign in to comment.