init
This commit is contained in:
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
||||
# Dockerfile
|
||||
FROM alpine:latest
|
||||
|
||||
RUN apk add --no-cache curl bash dcron
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY warm.sh /app/warm.sh
|
||||
RUN chmod +x /app/warm.sh
|
||||
|
||||
CMD ["sh", "/app/warm.sh"]
|
||||
12
README.md
Normal file
12
README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
To use you must generate file list: example for astro.build
|
||||
|
||||
```
|
||||
cd dist && \
|
||||
( \
|
||||
find . -name "*.html" \
|
||||
| sed 's|^\./||' \
|
||||
| sed 's|index.html$||' \
|
||||
&& \
|
||||
find _astro -type f \
|
||||
) | sort -u > files-list.txt
|
||||
```
|
||||
14
docker-compose.yml
Normal file
14
docker-compose.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
version: "3.9"
|
||||
|
||||
services:
|
||||
cache-warmer:
|
||||
build:
|
||||
context: ./
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
BASE_URL: "https://example.net"
|
||||
FILE_LIST: "/files-list.txt"
|
||||
CRON_EXPR: "0 */12 * * *"
|
||||
USER_AGENT: "Mozilla/5.0"
|
||||
ACCEPT_ENCODING: "br,gzip"
|
||||
restart: always
|
||||
52
warm.sh
Executable file
52
warm.sh
Executable file
@@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
BASE_URL="${BASE_URL:?BASE_URL env not set}"
|
||||
FILE_LIST="${FILE_LIST:?FILE_LIST env not set}"
|
||||
CRON_EXPR="${CRON_EXPR:-0 */12 * * *}"
|
||||
USER_AGENT="${USER_AGENT:-Mozilla/5.0}"
|
||||
ACCEPT_ENCODING="${ACCEPT_ENCODING:-br,gzip}"
|
||||
|
||||
echo "$(date) - Starting cache warmer for $BASE_URL$FILE_LIST"
|
||||
echo "Cron schedule: $CRON_EXPR"
|
||||
|
||||
cat << EOF > /app/warm_files.sh
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
BASE_URL="$BASE_URL"
|
||||
FILE_LIST="$FILE_LIST"
|
||||
USER_AGENT="$USER_AGENT"
|
||||
ACCEPT_ENCODING="$ACCEPT_ENCODING"
|
||||
|
||||
echo "\$(date) - Fetching file list from \$BASE_URL\$FILE_LIST"
|
||||
|
||||
# WAŻNE: --compressed automatycznie dekompresuje odpowiedź
|
||||
FILES=\$(curl -s --compressed -A "\$USER_AGENT" "\$BASE_URL\$FILE_LIST")
|
||||
|
||||
if [ -z "\$FILES" ]; then
|
||||
echo "\$(date) - File list empty"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "\$FILES" | while read -r url; do
|
||||
if [ -n "\$url" ]; then
|
||||
echo "\$(date) - Warming \$BASE_URL/\$url"
|
||||
curl -s -o /dev/null -A "\$USER_AGENT" -H "Accept-Encoding: \$ACCEPT_ENCODING" "\$BASE_URL/\$url"
|
||||
fi
|
||||
done
|
||||
|
||||
echo "\$(date) - Warming completed"
|
||||
EOF
|
||||
|
||||
chmod +x /app/warm_files.sh
|
||||
touch /var/log/cron.log
|
||||
|
||||
echo "$(date) - Running initial warming..."
|
||||
/app/warm_files.sh
|
||||
|
||||
echo "$CRON_EXPR /app/warm_files.sh >> /var/log/cron.log 2>&1" > /etc/crontabs/root
|
||||
|
||||
echo "$(date) - Starting crond..."
|
||||
crond -f -l 2 &
|
||||
tail -f /var/log/cron.log
|
||||
Reference in New Issue
Block a user