Skip to content

Commit

Permalink
Merge pull request #904 from InfuseAI/bug/sc-32380/jsondecodeerror-ex…
Browse files Browse the repository at this point in the history
…pecting-value-line-1-column

[Bug] Add a handler to unexpected json file format
  • Loading branch information
wcchang1115 authored Oct 18, 2023
2 parents 5efa1ea + c9ee12d commit 2e7f921
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions piperider_cli/event/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import platform
import sys
import time
from contextlib import contextmanager
from datetime import datetime
from json import JSONDecodeError

import portalocker
import requests
Expand Down Expand Up @@ -64,7 +66,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 All @@ -83,45 +84,46 @@ def _is_full(self):
o = json.loads(f.read())
return len(o.get('unsend_events', [])) >= self._upload_threshold

def send_events(self):
@contextmanager
def load_json(self):
with portalocker.Lock(self._unsend_events_file, 'r+', timeout=5) as f:
o = json.loads(f.read())
try:
o = json.loads(f.read())
yield o
except JSONDecodeError:
o = dict(unsend_events=[])
yield o
finally:
f.seek(0)
f.truncate()
f.write(json.dumps(o))

def send_events(self):
with self.load_json() as o:
payload = dict(
api_key=self._api_key,
events=o['unsend_events'],
)
o['unsend_events'] = []
f.seek(0)
f.truncate()
f.write(json.dumps(o))
try:
requests.post(self._api_endpoint, json=payload)
except Exception:
# TODO: handle exception when sending events
pass

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

o['unsend_events'].append(event)
f.seek(0)
f.truncate()
f.write(json.dumps(o))

def _cleanup_unsend_events(self):
with portalocker.Lock(self._unsend_events_file, 'r+', timeout=5) as f:
o = json.loads(f.read())
with self.load_json() as o:
events = o.get('unsend_events', None)
if events is None:
o['unsend_events'] = []

while len(o['unsend_events']) > self._delete_threshold:
o['unsend_events'].pop(0)

f.seek(0)
f.truncate()
f.write(json.dumps(o))

0 comments on commit 2e7f921

Please sign in to comment.