Skip to content

Commit

Permalink
Add thread lock to load user profile
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 f61032b commit 325567c
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions piperider_cli/event/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import threading
import uuid
from typing import Union

Expand All @@ -15,6 +16,7 @@

_collector = Collector()
_yml = yaml.YAML()
user_profile_lock = threading.Lock()


def init():
Expand All @@ -27,15 +29,17 @@ def init():


def load_user_profile():
if not os.path.exists(PIPERIDER_USER_PROFILE):
user_profile = _generate_user_profile()
else:
with open(PIPERIDER_USER_PROFILE, 'r') as f:
user_profile = yaml.YAML().load(f)
if user_profile.get('user_id') is None:
user_profile = _generate_user_profile()

return user_profile
# can be called by multiple threads from capture_exception
with user_profile_lock:
if not os.path.exists(PIPERIDER_USER_PROFILE):
user_profile = _generate_user_profile()
else:
with open(PIPERIDER_USER_PROFILE, 'r') as f:
user_profile = _yml.load(f)
if user_profile.get('user_id') is None:
user_profile = _generate_user_profile()

Check warning on line 40 in piperider_cli/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

piperider_cli/event/__init__.py#L40

Added line #L40 was not covered by tests

return user_profile


def update_user_profile(update_values):
Expand Down

0 comments on commit 325567c

Please sign in to comment.