-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapi.sh
More file actions
executable file
·128 lines (108 loc) · 3.6 KB
/
api.sh
File metadata and controls
executable file
·128 lines (108 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
set -euo pipefail
# Default entrypoint for api
echo "Starting api"
# Signal handling for graceful shutdown
shutdown_in_progress=false
shutdown() {
# Prevent duplicate shutdown attempts (in case both shell and process receive signal)
if [[ "$shutdown_in_progress" == "true" ]]; then
return 0
fi
shutdown_in_progress=true
echo "Received SIGTERM, shutting down gracefully..."
if [[ -n "${api_pid:-}" ]]; then
# Send SIGTERM to the API process (gunicorn)
kill -TERM "$api_pid" 2>/dev/null || true
# Wait for graceful shutdown with timeout (default 25s, less than K8s terminationGracePeriodSeconds)
local timeout=${SHUTDOWN_TIMEOUT:-25}
local count=0
while kill -0 "$api_pid" 2>/dev/null && [[ $count -lt $timeout ]]; do
sleep 1
((count++))
done
# If process is still running, force kill
if kill -0 "$api_pid" 2>/dev/null; then
echo "API did not shutdown gracefully after ${timeout}s, forcing shutdown..."
kill -KILL "$api_pid" 2>/dev/null || true
wait "$api_pid" 2>/dev/null || true
else
echo "API shutdown complete"
fi
fi
exit 0
}
# Trap SIGTERM and SIGINT
trap shutdown SIGTERM SIGINT
# Script section to keep in sync with worker.sh
#### Start ####
# Optional prefix and suffix for all python commands
pre="${CODECOV_WRAPPER:-}"
post="${CODECOV_WRAPPER_POST:-}"
# Whether to ignore the prefix and suffix on migration commands
if [[ -n "${CODECOV_WRAPPER_IGNORE_MIGRATE:-}" ]]; then
pre_migrate=""
post_migrate=""
else
pre_migrate="$pre"
post_migrate="$post"
fi
# Berglas is used to manage secrets in GCP.
berglas=""
if [[ -f "/usr/local/bin/berglas" ]]; then
berglas="berglas exec --"
fi
#### End ####
GUNICORN_WORKERS=${GUNICORN_WORKERS:-1}
if [[ "$GUNICORN_WORKERS" -gt 1 ]]; then
export PROMETHEUS_MULTIPROC_DIR="${PROMETHEUS_MULTIPROC_DIR:-$HOME/.prometheus}"
rm -r "${PROMETHEUS_MULTIPROC_DIR:?}"/* 2>/dev/null || true
mkdir -p "$PROMETHEUS_MULTIPROC_DIR"
fi
statsd=""
if [[ -n "${STATSD_HOST:-}" ]]; then
statsd="--statsd-host ${STATSD_HOST:?}:${STATSD_PORT:?}"
fi
if [[ "${CODECOV_SKIP_MIGRATIONS:-}" != "true" && ("${RUN_ENV:-}" = "ENTERPRISE" || "${RUN_ENV:-}" = "DEV") ]]; then
echo "Running migrations"
$pre_migrate $berglas python manage.py migrate $post_migrate
$pre_migrate $berglas python migrate_timeseries.py $post_migrate
$pre_migrate $berglas python manage.py pgpartition --yes --skip-delete $post_migrate
fi
if [[ "${RUN_ENV:-}" = "STAGING" || "${RUN_ENV:-}" = "DEV" ]]; then
export PYTHONWARNINGS=always
fi
if [[ -z "${1:-}" ]]; then
added_args=""
case "${RUN_ENV:-}" in
"PROD")
echo "Starting gunicorn in production mode"
added_args="--disable-redirect-access-to-syslog --config=gunicorn.conf.py --max-requests=50000 --max-requests-jitter=300"
;;
"STAGING")
echo "Starting gunicorn in staging mode"
added_args="--disable-redirect-access-to-syslog --config=gunicorn.conf.py"
;;
*)
echo "Starting gunicorn in default mode"
;;
esac
# Start gunicorn in the background and capture its PID
$pre $berglas gunicorn codecov.wsgi:application \
$added_args \
$statsd \
--workers="$GUNICORN_WORKERS" \
--threads="${GUNICORN_THREADS:-1}" \
--worker-connections="${GUNICORN_WORKER_CONNECTIONS:-1000}" \
--bind "${CODECOV_API_BIND:-0.0.0.0}":"${CODECOV_API_PORT:-8000}" \
--access-logfile '-' \
--timeout "${GUNICORN_TIMEOUT:-600}" \
$post &
api_pid=$!
echo "Gunicorn started with PID $api_pid"
# Wait for the background process
wait "$api_pid"
else
echo "Executing custom command"
exec "$@"
fi