feat(hotreload): first pass at debounce support
This commit is contained in:
parent
346963fd4f
commit
5d92abb079
1 changed files with 11 additions and 1 deletions
|
@ -122,11 +122,18 @@ class HotReloadHandler(RegexMatchingEventHandler):
|
|||
self.cog: HotReload = cog
|
||||
self.path: Path = path
|
||||
self.logger: RedTraceLogger = getLogger(name="red.SeaCogs.HotReload.Observer")
|
||||
self.debounce: bool = False
|
||||
|
||||
def on_any_event(self, event: FileSystemEvent) -> None:
|
||||
"""Handle filesystem events."""
|
||||
if event.is_directory:
|
||||
return
|
||||
if self.debounce:
|
||||
self.logger.trace("Debouncing enabled, ignoring event '%s'", event.event_type)
|
||||
return
|
||||
|
||||
self.debounce = True
|
||||
self.logger.verbose("Starting cog reload process, enabling debouncing during event '%s'", event.event_type)
|
||||
|
||||
allowed_events = ("moved", "deleted", "created", "modified")
|
||||
if event.event_type not in allowed_events:
|
||||
|
@ -147,13 +154,16 @@ class HotReloadHandler(RegexMatchingEventHandler):
|
|||
|
||||
self.logger.info("File %s has been %s%s.", event.src_path, event.event_type, dest)
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
future = run_coroutine_threadsafe(
|
||||
coro=self.reload_cogs(
|
||||
cog_names=cogs_to_reload,
|
||||
paths=[Path(str(p)) for p in (event.src_path, getattr(event, "dest_path", None)) if p],
|
||||
),
|
||||
loop=self.cog.bot.loop,
|
||||
)
|
||||
if future.done():
|
||||
self.debounce = False
|
||||
self.logger.verbose("Debouncing disabled, event '%s' has been handled", event.event_type)
|
||||
|
||||
async def reload_cogs(self, cog_names: Sequence[str], paths: Sequence[Path]) -> None:
|
||||
"""Reload modified cogs."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue