Skip to content

Commit 0bc11b1

Browse files
Add delayed shutdown
1 parent fe39100 commit 0bc11b1

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

docs/deployment.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Options:
114114
--timeout-graceful-shutdown INTEGER
115115
Maximum number of seconds to wait for
116116
graceful shutdown.
117+
--delayed-shutdown FLOAT
118+
Time in seconds to delay shutdown when a shutdown-signal is received
117119
--ssl-keyfile TEXT SSL key file
118120
--ssl-certfile TEXT SSL certificate file
119121
--ssl-keyfile-password TEXT SSL keyfile password

uvicorn/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def __init__(
223223
headers: list[tuple[str, str]] | None = None,
224224
factory: bool = False,
225225
h11_max_incomplete_event_size: int | None = None,
226+
shutdown_delay: float = 0,
226227
):
227228
self.app = app
228229
self.host = host
@@ -268,6 +269,7 @@ def __init__(
268269
self.encoded_headers: list[tuple[bytes, bytes]] = []
269270
self.factory = factory
270271
self.h11_max_incomplete_event_size = h11_max_incomplete_event_size
272+
self.shutdown_delay = shutdown_delay
271273

272274
self.loaded = False
273275
self.configure_logging()

uvicorn/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ def main(
408408
app_dir: str,
409409
h11_max_incomplete_event_size: int | None,
410410
factory: bool,
411+
shutdown_delay: float = 0,
411412
) -> None:
412413
run(
413414
app,
@@ -457,6 +458,7 @@ def main(
457458
factory=factory,
458459
app_dir=app_dir,
459460
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
461+
shutdown_delay=shutdown_delay
460462
)
461463

462464

@@ -509,6 +511,7 @@ def run(
509511
app_dir: str | None = None,
510512
factory: bool = False,
511513
h11_max_incomplete_event_size: int | None = None,
514+
shutdown_delay: float = 0,
512515
) -> None:
513516
if app_dir is not None:
514517
sys.path.insert(0, app_dir)
@@ -560,6 +563,7 @@ def run(
560563
use_colors=use_colors,
561564
factory=factory,
562565
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
566+
shutdown_delay=shutdown_delay
563567
)
564568
server = Server(config=config)
565569

uvicorn/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,12 @@ async def on_tick(self, counter: int) -> bool:
259259
return False
260260

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

267+
logger.info("Shutting down")
264268
# Stop accepting new connections.
265269
for server in self.servers:
266270
server.close()

0 commit comments

Comments
 (0)