Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shravanasati committed Feb 17, 2024
1 parent 39c2b1b commit 6386a7b
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions stellapy/reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from logging import exception
from threading import Lock, Thread
from time import sleep
from typing import Callable, Generic, TypeVar
from typing import Any, Callable, Generic, TypeVar

import helium

Expand All @@ -13,17 +13,16 @@
from stellapy.logger import log
from stellapy.walker import get_file_content, walk

ActionFunc = Callable[["Trigger"], None]
ErrorHandlerFunc = Callable[["Trigger", Exception], None]


T = TypeVar("T")
ActionFunc = Callable[["Trigger"], None]
ErrorHandlerFunc = Callable[["Trigger", Exception], None]


@dataclass(frozen=True)
class Trigger(Generic[T]):
"""
Similar to contexts in go, a trigger carries an action to perform at a certain datetime.
Similar to contexts in go, a trigger carries an action to perform at a certain datetime, along with an error handler and a value.
"""

action: ActionFunc
Expand All @@ -34,18 +33,25 @@ class Trigger(Generic[T]):

class TriggerQueue:
"""
A list of triggers offering some useful methods.
A list of triggers offering some useful and thread-safe methods.
The type variable `T` is used for the value of Trigger.
"""

def __init__(self) -> None:
self.triggers: list[Trigger] = []
self.triggers: list[Trigger[Any]] = []
self.__lock = Lock()

def add(self, trigger: Trigger):
def add(self, trigger: Trigger[Any]):
"""
Add a trigger to the queue.
"""
with self.__lock:
self.triggers.append(trigger)

def execute_remaining(self):
"""
Executes all the triggers that need to be executed, i.e., whose deadline has been reached.
"""
now = datetime.now()
with self.__lock:
for i, trigger in enumerate(self.triggers):
Expand All @@ -60,6 +66,9 @@ def execute_remaining(self):
self.triggers.pop(i)

def cancel_all(self):
"""
Cancel all the triggers.
"""
with self.__lock:
self.triggers.clear()

Expand Down Expand Up @@ -94,6 +103,7 @@ def __init__(
self._finished = False # used by trigger thread to look for exits
self.trigger_queue = TriggerQueue()
self.trigger_thread = Thread(target=self._trigger_executor)
self.trigger_thread.start()

# convert to seconds
self.poll_interval = self.config.poll_interval / 1000
Expand All @@ -102,6 +112,10 @@ def __init__(
)

def _trigger_executor(self):
"""
Executes all the remaining triggers in the trigger queue until self._finished
is set to `True`.
"""
while not self._finished:
self.trigger_queue.execute_remaining()
sleep(0.1)
Expand Down Expand Up @@ -188,17 +202,17 @@ def start_browser(self):
)
self.stop()

def _browser_reloader(self, _: Trigger):
def _browser_reloader(self, _: Trigger[timedelta]):
"""
A helper function used in browser reload triggers.
"""
if self.RELOAD_BROWSER:
helium.refresh()

def _browser_reload_error_handler(self, t: Trigger, e: Exception):
def _browser_reload_error_handler(self, t: Trigger[timedelta], e: Exception):
log(
"error",
f"browser reload didnt work, retrying in {2 * t.value} seconds...",
f"browser reload didnt work, retrying in {2 * t.value.microseconds / 10e6} seconds...",
)
self.trigger_queue.add(
Trigger(
Expand All @@ -219,7 +233,7 @@ def _restart(self):
self.trigger_queue.cancel_all()
self.executor.re_execute()
self.trigger_queue.add(
Trigger(
Trigger[timedelta](
action=self._browser_reloader,
when=datetime.now() + self.browser_wait_delta,
error_handler=self._browser_reload_error_handler,
Expand Down Expand Up @@ -302,6 +316,7 @@ def stop(self):
)
exception(e)
finally:
self._finished = True
os._exit(0)

def start(self) -> None:
Expand Down

0 comments on commit 6386a7b

Please sign in to comment.