Repository: sanic-org/sanic
Pull request #2805 (2023-08-23 06:32:07)

Add changed files to reload trigger listener
This injects the set of changed files after a reload event into the listener.


Patch:
diff --git a/sanic/server/events.py b/sanic/server/events.py
index 9f4be84a..ed1e74d0 100644
--- a/sanic/server/events.py
+++ b/sanic/server/events.py
@@ -12,6 +12,7 @@ def trigger_events(
     events: Optional[Iterable[Callable[..., Any]]],
     loop,
     app: Optional[Sanic] = None,
+    **kwargs,
 ):
     """Trigger event callbacks (functions or async)
 
@@ -23,8 +24,12 @@ def trigger_events(
     if events:
         for event in events:
             try:
-                result = event() if not app else event(app)
+                result = event(**kwargs) if not app else event(app, **kwargs)
             except TypeError:
-                result = event(loop) if not app else event(app, loop)
+                result = (
+                    event(loop, **kwargs)
+                    if not app
+                    else event(app, loop, **kwargs)
+                )
             if isawaitable(result):
                 loop.run_until_complete(result)
diff --git a/sanic/worker/reloader.py b/sanic/worker/reloader.py
index b490c5b3..528c029b 100644
--- a/sanic/worker/reloader.py
+++ b/sanic/worker/reloader.py
@@ -64,7 +64,7 @@ class Reloader:
                     trigger_events(before_trigger, loop, app)
                 self.reload(",".join(changed) if changed else "unknown")
                 if after_trigger:
-                    trigger_events(after_trigger, loop, app)
+                    trigger_events(after_trigger, loop, app, changed=changed)
             sleep(self.interval)
         else:
             if reloader_stop:


Test patch:
diff --git a/tests/worker/test_reloader.py b/tests/worker/test_reloader.py
index 6530c770..a0082014 100644
--- a/tests/worker/test_reloader.py
+++ b/tests/worker/test_reloader.py
@@ -155,9 +155,7 @@ def test_reload_delayed(monkeypatch):
     monkeypatch.setattr(threading.Thread, "start", orig)
 
 
-def test_reloader_triggers_start_stop_listeners(
-    app: Sanic, app_loader: AppLoader
-):
+def test_reloader_triggers_start_stop_listeners(app: Sanic, app_loader: AppLoader):
     results = []
 
     @app.reload_process_start
@@ -213,6 +211,7 @@ def test_triggered(app_loader):
 def test_reloader_triggers_reload_listeners(app: Sanic, app_loader: AppLoader):
     before = Event()
     after = Event()
+    changed_files = set()
 
     def check_file(filename, mtimes):
         return not after.is_set()
@@ -222,8 +221,9 @@ def test_reloader_triggers_reload_listeners(app: Sanic, app_loader: AppLoader):
         before.set()
 
     @app.after_reload_trigger
-    async def after_reload_trigger(_):
+    async def after_reload_trigger(_, changed):
         after.set()
+        changed_files.update(changed)
 
     reloader = Reloader(Mock(), 0.1, set(), app_loader)
     reloader.check_file = check_file  # type: ignore
@@ -231,6 +231,8 @@ def test_reloader_triggers_reload_listeners(app: Sanic, app_loader: AppLoader):
 
     assert before.is_set()
     assert after.is_set()
+    assert len(changed_files) > 0
+    assert changed_files == set(reloader.files())
 
 
 def test_check_file(tmp_path):