Skip to content

Commit

Permalink
Add delayed shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakobhenningjensen committed Nov 11, 2024
1 parent fe39100 commit ac9ffdc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Options:
--timeout-graceful-shutdown INTEGER
Maximum number of seconds to wait for
graceful shutdown.
--delayed-shutdown FLOAT
Time in seconds to delay shutdown when a shutdown-signal is received
--ssl-keyfile TEXT SSL key file
--ssl-certfile TEXT SSL certificate file
--ssl-keyfile-password TEXT SSL keyfile password
Expand Down
2 changes: 2 additions & 0 deletions uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def __init__(
headers: list[tuple[str, str]] | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
shutdown_delay: float = 0
):
self.app = app
self.host = host
Expand Down Expand Up @@ -268,6 +269,7 @@ def __init__(
self.encoded_headers: list[tuple[bytes, bytes]] = []
self.factory = factory
self.h11_max_incomplete_event_size = h11_max_incomplete_event_size
self.shutdown_delay = shutdown_delay

self.loaded = False
self.configure_logging()
Expand Down
4 changes: 4 additions & 0 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ def main(
app_dir: str,
h11_max_incomplete_event_size: int | None,
factory: bool,
shutdown_delay: float = 0
) -> None:
run(
app,
Expand Down Expand Up @@ -457,6 +458,7 @@ def main(
factory=factory,
app_dir=app_dir,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
shutdown_delay=shutdown_delay
)


Expand Down Expand Up @@ -509,6 +511,7 @@ def run(
app_dir: str | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
shutdown_delay: float = 0
) -> None:
if app_dir is not None:
sys.path.insert(0, app_dir)
Expand Down Expand Up @@ -560,6 +563,7 @@ def run(
use_colors=use_colors,
factory=factory,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
shutdown_delay=shutdown_delay
)
server = Server(config=config)

Expand Down
6 changes: 5 additions & 1 deletion uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,12 @@ async def on_tick(self, counter: int) -> bool:
return False

async def shutdown(self, sockets: list[socket.socket] | None = None) -> None:
logger.info("Shutting down")
if self.config.shutdown_delay:
logger.info(f"Shutting down in {self.config.shutdown_delay} seconds")
self.config.app.uvicorn_shutdown_triggered = True
await asyncio.sleep(self.config.shutdown_delay)

logger.info("Shutting down")
# Stop accepting new connections.
for server in self.servers:
server.close()
Expand Down

0 comments on commit ac9ffdc

Please sign in to comment.