Skip to content

Commit

Permalink
[ENH] Add hot reloading for development (conda-incubator#907)
Browse files Browse the repository at this point in the history
  • Loading branch information
peytondmurray authored Oct 25, 2024
1 parent 15e0b4a commit 3e4e1af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
29 changes: 24 additions & 5 deletions conda-store-server/conda_store_server/_internal/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ def initialize(self, *args, **kwargs):
f"Running conda-store with store directory: {self.conda_store.store_directory}"
)

if self.conda_store.upgrade_db:
dbutil.upgrade(self.conda_store.database_url)

self.authentication = self.authentication_class(
parent=self,
log=self.log,
Expand Down Expand Up @@ -389,7 +386,9 @@ def _check_worker(self, delay=5):
)

def start(self):
fastapi_app = self.init_fastapi_app()
"""Start the CondaStoreServer application, and run a FastAPI-based webserver."""
if self.conda_store.upgrade_db:
dbutil.upgrade(self.conda_store.database_url)

with self.conda_store.session_factory() as db:
self.conda_store.ensure_settings(db)
Expand Down Expand Up @@ -431,6 +430,10 @@ def start(self):
process = multiprocessing.Process(target=CondaStoreWorker.launch_instance)
process.start()

# If running in standalone mode, also enable automatic reloading of the
# webserver
self.reload = True

try:
# Note: the logger needs to be defined here for the output to show
# up, self.log doesn't work here either
Expand All @@ -440,7 +443,7 @@ def start(self):
logger.info(f"Starting server on {self.address}:{self.port}")

uvicorn.run(
fastapi_app,
"conda_store_server._internal.server.app:CondaStoreServer.create_webserver",
host=self.address,
port=self.port,
workers=1,
Expand All @@ -452,7 +455,9 @@ def start(self):
if self.reload
else []
),
factory=True,
)

except:
import traceback

Expand All @@ -462,3 +467,17 @@ def start(self):
if self.standalone:
process.join()
worker_checker.join()

@classmethod
def create_webserver(cls: type) -> FastAPI:
"""Create a CondaStoreServer instance to load the config, then return a FastAPI app.
Returns
-------
FastAPI
A FastAPI app configured using a fresh CondaStoreServer instance
"""
app = cls()
app.initialize()
return app.init_fastapi_app()
5 changes: 4 additions & 1 deletion docusaurus-docs/community/contribute/contribute-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ subprocess of the web server.
conda-store-server --standalone
```

4. You should now be able to access the `conda-store` server at [localhost:8080](http://localhost:8080/) from your web browser.
4. You should now be able to access the `conda-store` server at
[localhost:8080](http://localhost:8080/) from your web browser. Note that
this will enable hot reloading by default, allowing code changes to be picked
up by the webserver immediately without restart.

## `conda-store-ui`

Expand Down
1 change: 1 addition & 0 deletions tests/assets/conda_store_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
c.CondaStoreServer.enable_ui = True
c.CondaStoreServer.enable_api = True
c.CondaStoreServer.enable_registry = True
c.CondaStoreServer.reload = True
c.CondaStoreServer.enable_metrics = True
c.CondaStoreServer.address = "0.0.0.0"
c.CondaStoreServer.port = 8080
Expand Down

0 comments on commit 3e4e1af

Please sign in to comment.