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 reloading on addtional directories #2167

Merged
merged 9 commits into from
Jun 18, 2021
Merged

Add reloading on addtional directories #2167

merged 9 commits into from
Jun 18, 2021

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Jun 16, 2021

Resolves #2143

This allows the auto-reloader to watch additional directories beyond just the Python modules for changes. Implemented in two ways:

Via app.run

app.run(..., reload_dir="./some/path")
# or
app.run(..., reload_dir=["./some/path", "./some/other/path"])

Via cli

sanic --reload-dir=./some/path
sanic -D ./some/path -D ./some/other/path

@ahopkins ahopkins requested a review from a team as a code owner June 16, 2021 21:17
@Tronic
Copy link
Member

Tronic commented Jun 17, 2021

I would use name other than include_dir, which connotes header paths and doesn't implicate that they are used with auto-reloader only. Maybe reloader_paths or something alike? (can these be files too, not just folders?)

I suppose the use case for this is to handle changes in templates, config files etc?

@Tronic
Copy link
Member

Tronic commented Jun 17, 2021

Unrelated to this PR, but worth of notice is the use of livereload on client side. Dunno if there are any Sanic extensions that could already do that, and also connect with the server reloads.

@ahopkins
Copy link
Member Author

I would use name other than include_dir

This is a good call out. I was having trouble coming up with a good name. I looked at uvicorn, and they use --reload-dir, which makes sense because their reload is --reload. And then... I just wrote it off. But, looking back on it again during the daytime hours with some more caffeine in my system, and --reload-dir still works for our use case too.

can these be files too, not just folders?

Not right now. I really just wanted to sneak the functionality in a fairly low touch way. The reloader_helper.py probably needs some more work. Under the hood it is just looping on the dir + glob.

I suppose the use case for this is to handle changes in templates, config files etc?

Yeah. The other place that I have personally run into it is with maintaining openapi spec files.

Unrelated to this PR, but worth of notice is the use of livereload on client side. Dunno if there are any Sanic extensions that could already do that, and also connect with the server reloads.

Agreed. It is probably the realm for extension. I built this that I use when working on slide presentations:

https://github.com/ahopkins/pyconil2021-liberate-your-api/blob/main/presentation/server.py

@ahopkins
Copy link
Member Author

ahopkins commented Jun 17, 2021

Also added --reload as an alias to --auto-reload

$ sanic -h
usage: sanic [-h] [-v] [-H HOST] [-p PORT] [-u UNIX] [--cert CERT] [--key KEY] [--access-logs | --no-access-logs] [--factory] [-w WORKERS] [-d] [-r] [-D RELOAD_DIR] module

                 Sanic
         Build Fast. Run Fast.

positional arguments:
  module                         path to your Sanic app. Example: path.to.server:app

optional arguments:
  -h, --help                     show this help message and exit
  -v, --version                  show program's version number and exit
  -H HOST, --host HOST           host address [default 127.0.0.1]
  -p PORT, --port PORT           port to serve on [default 8000]
  -u UNIX, --unix UNIX           location of unix socket
                                  
  --cert CERT                    location of certificate for SSL
  --key KEY                      location of keyfile for SSL
                                  
  --access-logs                  display access logs
  --no-access-logs               no display access logs
                                  
  --factory                      Treat app as an application factory, i.e. a () -> <Sanic app> callable
                                  
  -w WORKERS, --workers WORKERS  number of worker processes [default 1]
                                  
  -d, --debug
  -r, --reload, --auto-reload    Watch source directory for file changes and reload on changes
  -D RELOAD_DIR, --reload-dir RELOAD_DIR
                                 Extra directories to watch and reload on changes

@Tronic
Copy link
Member

Tronic commented Jun 17, 2021

Great! Could you also change the short argument to -R to go with -r for reload, and perhaps change the argument name in help to DIR or PATH so this would fit in one line:

  -D RELOAD_DIR, --reload-dir RELOAD_DIR
                                 Extra directories to watch and reload on changes
  -r, --reload, --auto-reload    Watch source directory for file changes and reload on changes
  -R PATH, --reload-dir PATH     Extra directories to watch and reload on changes

@ahopkins
Copy link
Member Author

  -d, --debug
  -r, --reload, --auto-reload    Watch source directory for file changes and reload on changes
  -R PATH, --reload-dir PATH     Extra directories to watch and reload on changes

@sanic-org sanic-org deleted a comment from Tronic Jun 17, 2021
sanic/reloader_helpers.py Outdated Show resolved Hide resolved
@ahopkins ahopkins merged commit 5bb9aa0 into main Jun 18, 2021
@ahopkins ahopkins deleted the include-dir branch June 18, 2021 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto reload resource files in the a Sanic app
2 participants