Skip to content

Commit 0ebc6c9

Browse files
ds-filipknefelFilip Knefel
andauthored
feat: add env-variable based uvicorn app timeout (Unstructured-IO#401)
Modify app-start.sh so that if MAX_LIFETIME_SECONDS environmental variable is set, app will be gracefully (and later forcefully if it fails) shutdown after set amount of seconds. Co-authored-by: Filip Knefel <[email protected]>
1 parent 0cadf93 commit 0ebc6c9

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## 0.0.66-dev0
1+
## 0.0.66-dev1
22

33
* Add support for `unique_element_ids` parameter.
4+
* Add max lifetime, via MAX_LIFETIME_SECONDS env-var, to API containers
45

56
## 0.0.65
67

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ docker-start-api:
6969
docker run -p 8000:8000 \
7070
-it --rm \
7171
--mount type=bind,source=$(realpath .),target=/home/notebook-user/local \
72+
$(if $(MAX_LIFETIME_SECONDS),-e MAX_LIFETIME_SECONDS=$(MAX_LIFETIME_SECONDS)) \
7273
pipeline-family-${PIPELINE_FAMILY}-dev:latest scripts/app-start.sh
7374

7475
.PHONY: docker-start-bash

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ You may also set the optional `UNSTRUCTURED_API_KEY` env variable to enable requ
397397
#### Controlling Server Load
398398
Some documents will use a lot of memory as they're being processed. To mitigate OOM errors, the server will return a 503 if the host's available memory drops below 2GB. This is configured with the environment variable `UNSTRUCTURED_MEMORY_FREE_MINIMUM_MB`, which defaults to 2048. You can lower this value to reduce these messages, that is, allow the server to use more memory. Otherwise, you can set to 0 to fully remove this check.
399399

400+
#### Controlling server life time
401+
By default server will run for indefinitely. To change that the `MAX_LIFETIME_SECONDS` environmental variable can be set. If server is run with this variable set, it will enter a graceful shutdown period after `MAX_LIFETIME_SECONDS` from its initialization. Graceful shutdown period lasts for up to 3600 seconds and during it:
402+
- server denies any new requests - they're met with an empty response,
403+
- server continues processing active requests and shuts down (ending graceful period) if all of them are processed.
404+
405+
After the graceful period is over if server is still running, it is shutdown forcefully, cancelling all active requests and sending empty responses to each of them.
406+
407+
*Max lifetime requires gnu [timeout](https://www.gnu.org/software/coreutils/manual/html_node/timeout-invocation.html#timeout-invocation) to be installed, available by default on most linux systems. Downloadable on MacOS as gtimeout with gnu coreutils.*
408+
400409
## :dizzy: Instructions for using the Docker image
401410

402411
The following instructions are intended to help you get up and running using Docker to interact with `unstructured-api`.

scripts/app-start.sh

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
#!/usr/bin/env bash
22

3-
uvicorn prepline_general.api.app:app \
4-
--log-config logger_config.yaml \
3+
NUMREGEX="^[0-9]+$"
4+
GRACEFUL_SHUTDOWN_PERIOD_SECONDS=3600
5+
TIMEOUT_COMMAND='timeout'
6+
OPTIONAL_TIMEOUT=''
7+
8+
if [[ -n $MAX_LIFETIME_SECONDS ]]; then
9+
if ! command -v $TIMEOUT_COMMAND &> /dev/null; then
10+
TIMEOUT_COMMAND='gtimeout'
11+
echo "Warning! 'timeout' command is required but not available. Checking for gtimeout."
12+
elif ! command -v $TIMEOUT_COMMAND &> /dev/null; then
13+
echo "Warning! 'gtimeout' command is required but not available. Running without max lifetime."
14+
elif [[ $MAX_LIFETIME_SECONDS =~ $NUMREGEX ]]; then
15+
OPTIONAL_TIMEOUT="timeout --preserve-status --foreground --kill-after ${GRACEFUL_SHUTDOWN_PERIOD_SECONDS} ${MAX_LIFETIME_SECONDS}"
16+
echo "Server's lifetime set to ${MAX_LIFETIME_SECONDS} seconds."
17+
else
18+
echo "Warning! MAX_LIFETIME_SECONDS was not properly set, an integer was expected, got ${MAX_LIFETIME_SECONDS}. Running without max lifetime."
19+
fi
20+
fi
21+
22+
${OPTIONAL_TIMEOUT} \
23+
uvicorn prepline_general.api.app:app \
24+
--log-config logger_config.yaml \
525
--host 0.0.0.0
26+
27+
echo "Server was shutdown"
28+
[ -n "$MAX_LIFETIME_SECONDS" ] && echo "Reached timeout of $MAX_LIFETIME_SECONDS seconds"

0 commit comments

Comments
 (0)