Compare commits

...
3 changed files with 73 additions and 13 deletions

No files matched your search

+4 -4
View File
@@ -129,10 +129,10 @@ ARG RUNTIME_PACKAGES="\
ghostscript \
gnupg \
icc-profiles-free \
# PostgreSQL
postgresql-client \
# MySQL / MariaDB
mariadb-client \
# PostgreSQL client library, used by psycopg
libpq5 \
# MariaDB client library, used by mysqlclient
libmariadb3 \
# OCRmyPDF dependencies
tesseract-ocr \
tesseract-ocr-eng \
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
"""
Single-shot database readiness probe used by the init-wait-for-db s6 service.
Exits 0 if the database server is accepting connections, 1 otherwise. The
retry and backoff loop lives in the calling shell script.
This replaces pg_isready / mariadb-admin ping, so the image does not need the
PostgreSQL or MariaDB command line clients. Like those tools, this only checks
that the server answers. A server which responds with an error, such as bad
credentials or an unknown database, counts as ready, so the real error is
reported by the migrations step instead of this one timing out.
"""
import os
import sys
CONNECT_TIMEOUT = 5
def postgres_ready() -> bool:
from psycopg import pq
from psycopg.conninfo import make_conninfo
# This is libpq's PQping, which is what pg_isready uses. It reports OK when
# the server answers, even if it would refuse these credentials, and REJECT
# when the server is up but still starting or shutting down.
conninfo = make_conninfo(
host=os.environ.get("PAPERLESS_DBHOST", "localhost"),
port=int(os.environ.get("PAPERLESS_DBPORT", "5432")),
user=os.environ.get("PAPERLESS_DBUSER", "paperless"),
connect_timeout=CONNECT_TIMEOUT,
)
return pq.PGconn.ping(conninfo.encode()) == pq.Ping.OK
def mariadb_ready() -> bool:
import MySQLdb
try:
MySQLdb.connect(
host=os.environ.get("PAPERLESS_DBHOST", "localhost"),
port=int(os.environ.get("PAPERLESS_DBPORT", "3306")),
user=os.environ.get("PAPERLESS_DBUSER", "paperless"),
passwd=os.environ.get("PAPERLESS_DBPASS", "paperless"),
connect_timeout=CONNECT_TIMEOUT,
).close()
return True
except MySQLdb.OperationalError as e:
# Codes below 2000 are errors sent by the server (access denied, too
# many connections, ...). 2000+ are client side (can't connect, lost
# connection), meaning the server was not reached.
code = e.args[0] if e.args and isinstance(e.args[0], int) else 0
return 0 < code < 2000
def main() -> int:
if os.environ.get("PAPERLESS_DBENGINE") == "mariadb":
ready = mariadb_ready()
else:
ready = postgres_ready()
return 0 if ready else 1
if __name__ == "__main__":
sys.exit(main())
@@ -7,6 +7,7 @@ set -euo pipefail
declare -r LOG_PREFIX="[init-db-wait]"
declare -ri TIMEOUT=60
declare -r PROBE="/etc/s6-overlay/s6-rc.d/init-wait-for-db/db_ready.py"
declare -i ATTEMPT=0
declare -i DELAY=0
declare -i STARTED_AT=${EPOCHSECONDS:?EPOCHSECONDS var unset}
@@ -35,11 +36,7 @@ delay_next_attempt() {
wait_for_postgres() {
echo "${LOG_PREFIX} Waiting for PostgreSQL to start..."
local -r host="${PAPERLESS_DBHOST:-localhost}"
local -r port="${PAPERLESS_DBPORT:-5432}"
local -r user="${PAPERLESS_DBUSER:-paperless}"
while ! pg_isready -h "${host}" -p "${port}" --username "${user}"; do
while ! python3 "${PROBE}"; do
delay_next_attempt
done
echo "${LOG_PREFIX} Connected to PostgreSQL"
@@ -48,10 +45,7 @@ wait_for_postgres() {
wait_for_mariadb() {
echo "${LOG_PREFIX} Waiting for MariaDB to start..."
local -r host="${PAPERLESS_DBHOST:-localhost}"
local -r port="${PAPERLESS_DBPORT:-3306}"
while ! mariadb-admin --host="${host}" --port="${port}" --skip-ssl ping --silent >/dev/null 2>&1; do
while ! python3 "${PROBE}"; do
delay_next_attempt
done
echo "${LOG_PREFIX} Connected to MariaDB"