Skip to content

Commit

Permalink
Add a cache warmer script
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed May 6, 2024
1 parent e4b0c3d commit 1d4d347
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/cache-warmer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ARG TAG=main
ARG DOCKER_REPOSITORY=local
FROM ${DOCKER_REPOSITORY}/scyllaridae:${TAG}

RUN apk update && \
apk add --no-cache jq==1.7.1-r0

COPY cmd.sh /app/
COPY scyllaridae.yml /app/scyllaridae.yml
65 changes: 65 additions & 0 deletions examples/cache-warmer/cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash

set -eou pipefail

mygrep() {
# mac OS needs ggrep for the -P flag
if command -v ggrep &>/dev/null; then
ggrep "$@"
else
grep "$@"
fi
}

# how many cURL commands to run in parallel
PARALLEL_EXECUTIONS=3

# Base URL of the sitemap.xml file
BASE_URL="https://$1/sitemap.xml"
PAGE=1

while true; do
NEXT_PAGE_URL="$BASE_URL?page=$PAGE"
STATUS=$(curl -w '%{http_code}' \
--silent \
-o links.xml \
"${NEXT_PAGE_URL}")

if [ "${STATUS}" -eq 200 ]; then
mapfile -t URLS < <(mygrep -oP '<loc>\K[^<]+' links.xml)
while [ "${#URLS[@]}" -gt 0 ]; do
for ((i = 0; i < PARALLEL_EXECUTIONS; i++)); do
array_length=${#URLS[@]}
if [ "$array_length" -gt 0 ]; then
URL="${URLS[$((array_length-1))]}"
unset "URLS[$((array_length-1))]"
else
break
fi
echo "Crawling: $URL"
curl --silent -o /dev/null "${URL}" &
job_ids+=($!)
done

for job_id in "${job_ids[@]}"; do
wait "$job_id" || echo "One job failed, but continuing anyway"
done
done

PAGE=$((PAGE + 1))
else
break
fi
done

rm -f links.xml

curl -v http://drupal/api/v1/paged-content > pc.json

mapfile -t NIDS < <(jq -r '.[]' pc.json)
for NID in "${NIDS[@]}"; do
echo "Processing: $NID"
curl -s -o /dev/null "http://drupal/node/$NID/book-manifest?cache-warmer=1"
done

rm -f pc.json

0 comments on commit 1d4d347

Please sign in to comment.