From f51015e8b866129866bbcc64b3e6f0c03b32928f Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 2 Nov 2026 22:38:23 +0100 Subject: [PATCH] optional redis queue processing setup Allow QUEUE_CONNECTION=redis to start an s6-managed background worker, while keeping synchronous execution as the default. Enable PCNTL for worker timeouts and use the effective application queue configuration. Persist Redis data with append-only logging in the Compose examples and document queue retries, graceful shutdown, and failed-job recovery. --- Dockerfile | 1 + README.md | 21 ++++++++++++++++++++ examples/compose/compose.yml | 3 +++ examples/nginx/compose.yml | 3 +++ examples/rspamd/compose.yml | 3 +++ examples/traefik/compose.yml | 3 +++ rootfs/etc/cont-init.d/00-env | 1 + rootfs/etc/cont-init.d/13-config-anonaddy.sh | 2 +- rootfs/etc/cont-init.d/50-svc-main.sh | 19 ++++++++++++++++++ 9 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index f98ed320..2773edce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,6 +33,7 @@ RUN apk --no-cache add \ php84-mbstring \ php84-opcache \ php84-openssl \ + php84-pcntl \ php84-pdo \ php84-pdo_mysql \ php84-phar \ diff --git a/README.md b/README.md index bd5fc75c..bec66b6d 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ ___ * [Generate DKIM private/public keypair](#generate-dkim-privatepublic-keypair) * [Generate GPG key](#generate-gpg-key) * [Define additional env vars](#define-additional-env-vars) + * [Background jobs](#background-jobs) * [Override Postfix main configuration](#override-postfix-main-configuration) * [Spamhaus DQS configuration](#spamhaus-dqs-configuration) * [Contributing](#contributing) @@ -102,6 +103,7 @@ linux/arm64 * `APP_KEY`: Application key for encrypter service. You can generate one through `anonaddy key:generate --show` or `echo "base64:$(openssl rand -base64 32)"`. **required** * `APP_DEBUG`: Enables or disables debug mode, used to troubleshoot issues (default `false`) * `APP_URL`: The URL of your installation +* `QUEUE_CONNECTION`: Job execution mode, `sync` or `redis` (default `sync`). See [Background jobs](#background-jobs) for Redis queue setup. * `ANONADDY_RETURN_PATH`: Return-path header for outbound emails * `ANONADDY_ADMIN_USERNAME`: If set this value will be used and allow you to receive forwarded emails at the root domain * `ANONADDY_ENABLE_REGISTRATION`: If set to false this will prevent new users from registering on the site (default `true`) @@ -306,6 +308,25 @@ Keys will be stored in `/data/.gnupg` folder. You can define additional environment variables that will be used by the app by creating a file named `.env` in `/data`. +### Background jobs + +With `QUEUE_CONNECTION=redis`, all queued application jobs, including email +delivery and notifications, run in the background. The worker uses the configured +Redis server and queue. Other queue connections require an externally managed +worker. + +The worker allows three attempts with a five-second retry delay and a 60-second +timeout. Job-specific retry and timeout settings take precedence. The timeout is +shorter than the application's 90-second Redis retry interval. + +The Compose examples enable Redis append-only persistence in `./redis`. When +enabling queues, set `S6_SERVICES_GRACETIME=65000` and `stop_grace_period: 90s` +on the `addy` service so the worker can finish its current job during shutdown. + +Drain pending jobs before switching back to `sync`, since that mode does +not start a worker. Failed jobs can be inspected with `anonaddy queue:failed` and +retried with `anonaddy queue:retry `. + ### Override Postfix main configuration In some cases you may want to override the default Postfix main configuration diff --git a/examples/compose/compose.yml b/examples/compose/compose.yml index b7cf3eaa..727089be 100644 --- a/examples/compose/compose.yml +++ b/examples/compose/compose.yml @@ -20,6 +20,9 @@ services: redis: image: redis:8-alpine container_name: addy_redis + command: ["redis-server", "--appendonly", "yes"] + volumes: + - "./redis:/data" restart: always addy: diff --git a/examples/nginx/compose.yml b/examples/nginx/compose.yml index 552878aa..7c2f5597 100644 --- a/examples/nginx/compose.yml +++ b/examples/nginx/compose.yml @@ -20,6 +20,9 @@ services: redis: image: redis:8-alpine container_name: addy_redis + command: ["redis-server", "--appendonly", "yes"] + volumes: + - "./redis:/data" restart: always addy: diff --git a/examples/rspamd/compose.yml b/examples/rspamd/compose.yml index 2a073af3..b1d5789c 100644 --- a/examples/rspamd/compose.yml +++ b/examples/rspamd/compose.yml @@ -20,6 +20,9 @@ services: redis: image: redis:8-alpine container_name: addy_redis + command: ["redis-server", "--appendonly", "yes"] + volumes: + - "./redis:/data" restart: always addy: diff --git a/examples/traefik/compose.yml b/examples/traefik/compose.yml index 02f34c35..444a293f 100644 --- a/examples/traefik/compose.yml +++ b/examples/traefik/compose.yml @@ -50,6 +50,9 @@ services: redis: image: redis:8-alpine container_name: addy_redis + command: ["redis-server", "--appendonly", "yes"] + volumes: + - "./redis:/data" restart: always addy: diff --git a/rootfs/etc/cont-init.d/00-env b/rootfs/etc/cont-init.d/00-env index 2a0650af..0077c8cd 100755 --- a/rootfs/etc/cont-init.d/00-env +++ b/rootfs/etc/cont-init.d/00-env @@ -44,6 +44,7 @@ APP_NAME=${APP_NAME:-addy.io} #APP_KEY=${APP_KEY:-base64:Gh8/RWtNfXTmB09pj6iEflt/L6oqDf9ZxXIh4I9MS7A=} APP_DEBUG=${APP_DEBUG:-false} APP_URL=${APP_URL:-http://localhost} +QUEUE_CONNECTION=${QUEUE_CONNECTION:-sync} #DB_HOST=${DB_HOST:-localhost} DB_PORT=${DB_PORT:-3306} diff --git a/rootfs/etc/cont-init.d/13-config-anonaddy.sh b/rootfs/etc/cont-init.d/13-config-anonaddy.sh index da4faf50..78b8d82b 100755 --- a/rootfs/etc/cont-init.d/13-config-anonaddy.sh +++ b/rootfs/etc/cont-init.d/13-config-anonaddy.sh @@ -53,7 +53,7 @@ echo "Creating env file" printf '\n' dotenv_entry "BROADCAST_DRIVER" "log" dotenv_entry "CACHE_DRIVER" "file" - dotenv_entry "QUEUE_CONNECTION" "sync" + dotenv_entry "QUEUE_CONNECTION" "$QUEUE_CONNECTION" dotenv_entry "SESSION_DRIVER" "file" dotenv_entry "SESSION_LIFETIME" "120" diff --git a/rootfs/etc/cont-init.d/50-svc-main.sh b/rootfs/etc/cont-init.d/50-svc-main.sh index 98abf06d..43c1dd17 100755 --- a/rootfs/etc/cont-init.d/50-svc-main.sh +++ b/rootfs/etc/cont-init.d/50-svc-main.sh @@ -29,3 +29,22 @@ s6-setuidgid ${PUID}:${PGID} php-fpm84 -F EOL chmod +x /etc/services.d/php-fpm/run + +# Read the cached configuration after /data/.env overrides have been applied. +queue_connection=$(php -r '$config = require "/var/www/anonaddy/bootstrap/cache/config.php"; echo $config["queue"]["default"];') + +# Remove a previously generated service when restarting with another connection. +rm -rf /etc/services.d/queue +if [ "$queue_connection" = "redis" ]; then + echo "Creating Redis queue worker" + mkdir -p /etc/services.d/queue + cat > /etc/services.d/queue/run <