setup-opds.sh: replace fixed sleep watchdog with retry loop that restarts dir2opds until books are visible inside container

This commit is contained in:
Justin Oros
2026-04-20 22:26:28 -07:00
parent 85def22fca
commit c7c5d2bf8d

View File

@@ -311,21 +311,38 @@ header "Setting Up Mount Watchdog"
WATCHDOG_SCRIPT="/usr/local/bin/opds-watchdog.sh" WATCHDOG_SCRIPT="/usr/local/bin/opds-watchdog.sh"
cat > "$WATCHDOG_SCRIPT" << EOF cat > "$WATCHDOG_SCRIPT" << EOF
#!/usr/bin/env bash #!/usr/bin/env bash
BOOKS_PATH="${BOOKS_PATH}"
SERVER_NAME="${OPDS_SERVER}" SERVER_NAME="${OPDS_SERVER}"
if [ -d "\$BOOKS_PATH" ] && [ -z "\$(ls -A "\$BOOKS_PATH" 2>/dev/null)" ]; then MAX_WAIT=300
docker restart "\$SERVER_NAME" 2>/dev/null || true ELAPSED=0
while [ \$ELAPSED -lt \$MAX_WAIT ]; do
COUNT=\$(docker exec "\$SERVER_NAME" ls /books 2>/dev/null | wc -l)
if [ "\$COUNT" -gt 0 ]; then
exit 0
fi fi
docker restart "\$SERVER_NAME" 2>/dev/null || true
sleep 15
ELAPSED=\$((ELAPSED + 15))
done
EOF EOF
chmod +x "$WATCHDOG_SCRIPT" chmod +x "$WATCHDOG_SCRIPT"
CRON_LINE="@reboot sleep 120 && $WATCHDOG_SCRIPT" MOUNT_SCRIPT="/home/${HUB_USER}/mount-$(basename ${MOUNT_POINT:-grace}).sh"
cat > "$MOUNT_SCRIPT" << EOF
#!/usr/bin/env bash
sleep 90
/usr/bin/rclone mount ${OPDS_SERVER}-remote: ${MOUNT_POINT:-/home/${HUB_USER}/mnt/grace} --config /home/${HUB_USER}/.config/rclone/rclone.conf --vfs-cache-mode writes --allow-other --allow-non-empty --daemon
EOF
chmod +x "$MOUNT_SCRIPT"
chown "$HUB_USER":"$HUB_USER" "$MOUNT_SCRIPT"
CRON_LINE="@reboot $WATCHDOG_SCRIPT"
EXISTING_ROOT_CRON=$(crontab -l 2>/dev/null || true) EXISTING_ROOT_CRON=$(crontab -l 2>/dev/null || true)
if ! echo "$EXISTING_ROOT_CRON" | grep -qF "opds-watchdog"; then if ! echo "$EXISTING_ROOT_CRON" | grep -qF "opds-watchdog"; then
{ echo "$EXISTING_ROOT_CRON"; echo "$CRON_LINE"; } | crontab - { echo "$EXISTING_ROOT_CRON"; echo "$CRON_LINE"; } | crontab -
info "Watchdog crontab entry added — restarts $OPDS_SERVER if mount is empty on boot." info "Watchdog crontab entry added — restarts $OPDS_SERVER until books are visible."
else else
info "Watchdog crontab entry already exists." crontab -l 2>/dev/null | grep -v "opds-watchdog" | { cat; echo "$CRON_LINE"; } | crontab -
info "Watchdog crontab entry updated."
fi fi
header "OPDS Setup Complete" header "OPDS Setup Complete"