]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Adds simple Python to wait for Redis broker to be ready (with minor Dockerfile improv... 788/head
authorTrenton Holmes <holmes.trenton@gmail.com>
Sat, 23 Apr 2022 21:40:56 +0000 (14:40 -0700)
committerTrenton Holmes <trenton.holmes@psware.com>
Tue, 26 Apr 2022 15:46:03 +0000 (08:46 -0700)
Dockerfile
docker/docker-prepare.sh
docker/wait-for-redis.py [new file with mode: 0755]

index 77417383f0cd71d754ce7d0939015571c1f5399a..2267eab0b75308c7c4da734e2a12e80e3da344bd 100644 (file)
@@ -138,6 +138,8 @@ RUN set -eux \
   && chmod 755 /sbin/docker-entrypoint.sh \
   && cp docker-prepare.sh /sbin/docker-prepare.sh \
   && chmod 755 /sbin/docker-prepare.sh \
+  && cp wait-for-redis.py /sbin/wait-for-redis.py \
+  && chmod 755 /sbin/wait-for-redis.py \
   && chmod +x install_management_commands.sh \
   && ./install_management_commands.sh
 
index 48f0c6b828a740276ec7129c220d03f8cd2e3226..540b1d7ed6cc969f9e2705f6f22cf793afffef8a 100755 (executable)
@@ -27,6 +27,14 @@ wait_for_postgres() {
        done
 }
 
+wait_for_redis() {
+       # We use a Python script to send the Redis ping
+       # instead of installing redis-tools just for 1 thing
+       if ! python3 /sbin/wait-for-redis.py; then
+               exit 1
+       fi
+}
+
 migrations() {
        (
                # flock is in place to prevent multiple containers from doing migrations
@@ -60,6 +68,8 @@ do_work() {
                wait_for_postgres
        fi
 
+       wait_for_redis
+
        migrations
 
        search_index
diff --git a/docker/wait-for-redis.py b/docker/wait-for-redis.py
new file mode 100755 (executable)
index 0000000..2924503
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+"""
+Simple script which attempts to ping the Redis broker as set in the environment for
+a certain number of times, waiting a little bit in between
+
+"""
+import os
+import sys
+import time
+from typing import Final
+
+from redis import Redis
+
+if __name__ == "__main__":
+
+    MAX_RETRY_COUNT: Final[int] = 5
+    RETRY_SLEEP_SECONDS: Final[int] = 5
+
+    REDIS_URL: Final[str] = os.getenv("PAPERLESS_REDIS", "redis://localhost:6379")
+
+    print(f"Waiting for Redis: {REDIS_URL}", flush=True)
+
+    attempt = 0
+    with Redis.from_url(url=REDIS_URL) as client:
+        while attempt < MAX_RETRY_COUNT:
+            try:
+                client.ping()
+                break
+            except Exception:
+                print(
+                    f"Redis ping #{attempt} failed, waiting {RETRY_SLEEP_SECONDS}s",
+                    flush=True,
+                )
+                time.sleep(RETRY_SLEEP_SECONDS)
+                attempt += 1
+
+    if attempt >= MAX_RETRY_COUNT:
+        print(f"Failed to connect to: {REDIS_URL}")
+        sys.exit(os.EX_UNAVAILABLE)
+    else:
+        print(f"Connected to Redis broker: {REDIS_URL}")
+        sys.exit(os.EX_OK)