#!/usr/bin/env bash

# Generic queue worker entrypoint for Docker containers.
# Usage: queue-worker.sh [queue-name(s)]
# Example: queue-worker.sh default
#          queue-worker.sh default,media-storage

set -euo pipefail

cd "${APP_PATH:-/var/www/html}"

CONNECTION="${QUEUE_CONNECTION:-redis}"
QUEUES="${1:-default}"
SLEEP="${QUEUE_SLEEP:-3}"
TRIES="${QUEUE_TRIES:-3}"
TIMEOUT="${QUEUE_TIMEOUT:-60}"
MAX_TIME="${QUEUE_MAX_TIME:-0}"

ARGS=("$CONNECTION" "--queue=${QUEUES}" "--sleep=${SLEEP}" "--tries=${TRIES}" "--timeout=${TIMEOUT}")

if [[ "${MAX_TIME}" != "0" ]]; then
    ARGS+=("--max-time=${MAX_TIME}")
fi

echo "[queue-worker] Starting queue:work ${ARGS[*]}"
exec php artisan queue:work "${ARGS[@]}"

