Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changed files to reload trigger listener #2805

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions sanic/server/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def trigger_events(
events: Optional[Iterable[Callable[..., Any]]],
loop,
app: Optional[Sanic] = None,
**kwargs,
):
"""Trigger event callbacks (functions or async)

Expand All @@ -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)
2 changes: 1 addition & 1 deletion sanic/worker/reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __call__(self) -> None:
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:
Expand Down
10 changes: 6 additions & 4 deletions tests/worker/test_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ def start(self):
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
Expand Down Expand Up @@ -213,6 +211,7 @@ def check_file(filename, mtimes):
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()
Expand All @@ -222,15 +221,18 @@ async def before_reload_trigger(_):
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
run_reloader(reloader)

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):
Expand Down
Loading