Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-turner-1 committed Jan 10, 2025
1 parent 38a7ea0 commit a6b9d54
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
61 changes: 44 additions & 17 deletions src/access_py_telemetry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,7 @@ def send_api_request(

print(f"{Fore.RED}Sending telemetry data to {endpoint}{Style.RESET_ALL}")

# Check if there's an existing event loop, otherwise create a new one
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

if loop.is_running():
loop.create_task(send_telemetry(endpoint, telemetry_data))
else:
# breakpoint()
# loop.create_task(send_telemetry(telemetry_data))
loop.run_until_complete(send_telemetry(endpoint, telemetry_data))
warnings.warn(
"Event loop not running, telemetry will block execution",
category=RuntimeWarning,
)
send_in_loop(endpoint, telemetry_data)
return None

def _create_telemetry_record(
Expand Down Expand Up @@ -264,3 +248,46 @@ async def send_telemetry(endpoint: str, data: dict[str, Any]) -> None:
except (httpx.RequestError, httpx.HTTPStatusError) as e:
warnings.warn(f"Request failed: {e}", category=RuntimeWarning, stacklevel=2)
return None


def send_in_loop(endpoint: str, telemetry_data: dict[str, Any]) -> None:
"""
Wraps the send_telemetry function in an event loop. This function will:
- Check if an event loop is already running
- Create a new event loop if one is not running
- Send the telemetry data
Parameters
----------
endpoint : str
The URL to send the telemetry data to.
telemetry_data : dict
The telemetry data to send.
Returns
-------
None
Warnings
--------
RuntimeWarning
If the event loop is not running, telemetry will block execution.
"""

# Check if there's an existing event loop, otherwise create a new one
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

if loop.is_running():
loop.create_task(send_telemetry(endpoint, telemetry_data))
else:
# breakpoint()
# loop.create_task(send_telemetry(telemetry_data))
loop.run_until_complete(send_telemetry(endpoint, telemetry_data))
warnings.warn(
"Event loop not running, telemetry will block execution",
category=RuntimeWarning,
)
22 changes: 2 additions & 20 deletions src/access_py_telemetry/decorators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Callable, Any, Iterable
from .registry import TelemetryRegister
from functools import wraps
import warnings
import asyncio

from .api import ApiHandler, send_telemetry
from .api import ApiHandler, send_in_loop


def ipy_register_func(
Expand Down Expand Up @@ -98,23 +96,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:

print(f"Sending telemetry data to {endpoint}")

# Check if there's an existing event loop, otherwise create a new one
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

if loop.is_running():
loop.create_task(send_telemetry(endpoint, telemetry_data))
else:
# breakpoint()
# loop.create_task(send_telemetry(telemetry_data))
loop.run_until_complete(send_telemetry(endpoint, telemetry_data))
warnings.warn(
"Event loop not running, telemetry will block execution",
category=RuntimeWarning,
)
send_in_loop(endpoint, telemetry_data)

# Call the original function
return func(*args, **kwargs)
Expand Down

0 comments on commit a6b9d54

Please sign in to comment.