Skip to content

Commit

Permalink
installation_id management never throws
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-pc committed Jan 30, 2024
1 parent c1211b4 commit b56b8d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
52 changes: 28 additions & 24 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from fileinput import FileInput
from pathlib import Path
from types import ModuleType
from typing import Callable
from typing import Callable, Optional

import httpx
import pkg_resources
Expand Down Expand Up @@ -824,34 +824,38 @@ def validate_frontend_dependencies(init=True):
validate_bun()


def ensure_reflex_installation_id() -> int:
def ensure_reflex_installation_id() -> Optional[int]:
"""Ensures that a reflex distinct id has been generated and stored in the reflex directory.
Returns:
Distinct id.
"""
initialize_reflex_user_directory()
installation_id_file = os.path.join(constants.Reflex.DIR, "installation_id")

installation_id = None
if os.path.exists(installation_id_file):
try:
with open(installation_id_file, "r") as f:
installation_id = int(f.read())
except Exception:
# If anything goes wrong at all... just regenerate.
# Like what? Examples:
# - file not exists
# - file not readable
# - content not parseable as an int
pass

if installation_id is None:
installation_id = random.getrandbits(128)
with open(installation_id_file, "w") as f:
f.write(str(installation_id))
# If we get here, installation_id is definitely set
return installation_id
try:
initialize_reflex_user_directory()
installation_id_file = os.path.join(constants.Reflex.DIR, "installation_id")

installation_id = None
if os.path.exists(installation_id_file):
try:
with open(installation_id_file, "r") as f:
installation_id = int(f.read())
except Exception:
# If anything goes wrong at all... just regenerate.
# Like what? Examples:
# - file not exists
# - file not readable
# - content not parseable as an int
pass

if installation_id is None:
installation_id = random.getrandbits(128)
with open(installation_id_file, "w") as f:
f.write(str(installation_id))
# If we get here, installation_id is definitely set
return installation_id
except Exception as e:
console.debug(f"Failed to ensure reflex installation id: {e}")
return None


def initialize_reflex_user_directory():
Expand Down
2 changes: 2 additions & 0 deletions reflex/utils/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def send(event: str, telemetry_enabled: bool | None = None) -> bool:
return False

installation_id = ensure_reflex_installation_id()
if installation_id is None:
return False

try:
with open(constants.Dirs.REFLEX_JSON) as f:
Expand Down

0 comments on commit b56b8d2

Please sign in to comment.