Skip to content

Commit 8fd12b5

Browse files
committed
Initial commit
1 parent 3e37335 commit 8fd12b5

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

.github/workflows/main.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: build
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
7+
build:
8+
runs-on: ubuntu-latest
9+
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
10+
steps:
11+
12+
- uses: actions/checkout@master
13+
14+
- name: Get version tag
15+
id: get_tag
16+
run: |
17+
if [ ${{ endsWith(github.ref, '-lts') }} = true ]; then
18+
echo "tag=latest-lts,latest-${GITHUB_REF:11:4}-lts,${GITHUB_REF:10}" >>$GITHUB_OUTPUT
19+
else
20+
echo "tag=latest,${GITHUB_REF:10}" >>$GITHUB_OUTPUT
21+
fi
22+
23+
- name: Publish to Registry
24+
uses: elgohr/Publish-Docker-Github-Action@v5
25+
if: github.event_name != 'pull_request'
26+
with:
27+
name: sourcepole/${{ github.event.repository.name }}
28+
username: ${{ secrets.DOCKER_HUB_USER }}
29+
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
30+
tags: "${{ steps.get_tag.outputs.tag }}"

Dockerfile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM alpine:3.21
2+
3+
RUN apk add --no-cache curl cronie
4+
5+
WORKDIR /app
6+
COPY qgs_cache_preseed.sh /app/qgs_cache_preseed.sh
7+
RUN chmod +x /app/qgs_cache_preseed.sh
8+
9+
# Default cron schedule: every day at 03:00
10+
ENV CRON_SCHEDULE="0 3 * * *"
11+
# Whether to execute the script directly on startup before running cron
12+
ENV EXECUTE_ON_STARTUP=0
13+
# The QGIS project file extension to look for
14+
ENV QGS_EXT=.qgs
15+
# The number of FCGI instances (i.e. the number if simultaneous requests to send)
16+
ENV FCGI_INSTANCES=10
17+
# The sleep interval in seconds between sending requests
18+
ENV SLEEP_INTERVAL=1
19+
20+
21+
COPY entrypoint.sh /entrypoint.sh
22+
RUN chmod +x /entrypoint.sh
23+
24+
ENTRYPOINT ["/entrypoint.sh"]

README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![](https://github.com/qwc-services/qwc-qgs-cache-preseed/workflows/build/badge.svg)](https://github.com/qwc-services/qwc-qgs-cache-preseed/actions)
2+
[![docker](https://img.shields.io/docker/v/sourcepole/qwc-qgs-cache-preseed?label=Docker%20image&sort=semver)](https://hub.docker.com/r/sourcepole/qwc-qgs-cache-preseed)
3+
4+
QWC QGS Cache Pre-Seed
5+
======================
6+
7+
Docker image for pre-seeding the QGIS Server QGS project cache.
8+
9+
This image will periodically query the capabilities for all QGS projects below
10+
the projects directory to ensure that the projects are cached in the QGIS Server
11+
project cache, helping to avoid slow server responses which occur when a project
12+
is not in cache.
13+
14+
Setup
15+
-----
16+
17+
Add the `qwc-qgs-cache-preseed` container configuration to your QWC `docker-compose.yml`:
18+
```yml
19+
qwc-qgis-server:
20+
image: docker.io/sourcepole/qwc-qgis-server:<tag>
21+
environment:
22+
FCGI_MIN_PROCESSES: 10
23+
FCGI_MAX_PROCESSES: 10
24+
...
25+
volumes:
26+
- ./volumes/qgs-resources:/data:ro
27+
...
28+
29+
qwc-qgs-cache-preseed:
30+
image: docker.io/sourcepole/qwc-qgs-cache-preseed:<tag>
31+
environment:
32+
EXECUTE_ON_STARTUP: 1
33+
CRON_SCHEDULE: "0 3 * * *"
34+
QGS_EXT: ".qgs"
35+
FCGI_INSTANCES: 10
36+
volumes:
37+
- ./volumes/qgs-resources:/data:ro
38+
```
39+
40+
Configuration
41+
-------------
42+
43+
Make sure to mount the `qgs-resources` dir (or whichever directory is mounted to `/data` for `qwc-qgis-server`) to `/data`.
44+
45+
The following environment variables can be set:
46+
47+
| Name | Default | Description |
48+
|----------------------|-------------|----------------------------------------------------------------------------------|
49+
| `CRON_SCHEDULE` | `0 3 * * *` | Interval at which the pre-seeding script is run. Default: every day at 03:00. |
50+
| `EXECUTE_ON_STARTUP` | `0` | Whether to run the script when the container starts. |
51+
| `QGS_EXT` | `.qgs` | The QGS project extension to look for (`.qgs` or `.qgz`). |
52+
| `FCGI_INSTANCES` | `10` | The number of FCGI instances (i.e. the number if simultaneous requests to send). |
53+
| `SLEEP_INTERVAL` | `1` | The sleep interval in seconds between sending requests. |
54+
55+
*Note*: You should set `FCGI_MIN_PROCESSES` equals to `FCGI_MAX_PROCESSES` in the `qwc-qgis-server` container configuration
56+
and `FCGI_INSTANCES` to the same number in the `qwc-qgs-cache-preseed` container configuration.

entrypoint.sh

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
mkdir -p $HOME/.cache
3+
4+
# Wait for qwc-qgis-server
5+
while true; do
6+
echo -n "Waiting for qwc-qgis-server"
7+
status_code=$(curl -o /dev/null -s -w "%{http_code}" http://qwc-qgis-server/ows)
8+
if [ "$status_code" -eq 000 ]; then
9+
echo -n "."
10+
sleep 1
11+
else
12+
echo " Online!"
13+
break
14+
fi
15+
done
16+
17+
if [ ${EXECUTE_ON_STARTUP:-0} -eq 1 ]; then
18+
/app/qgs_cache_preseed.sh
19+
fi
20+
21+
# Setup cron job
22+
cat > $HOME/.cron_env <<EOF
23+
export QGS_EXT=.qgs
24+
export FCGI_INSTANCES=10
25+
export SLEEP_INTERVAL=1
26+
EOF
27+
echo "$CRON_SCHEDULE . $HOME/.cron_env; /app/qgs_cache_preseed.sh >/proc/1/fd/1 2>/proc/1/fd/2" | crontab -
28+
29+
echo "Starting cron..."
30+
exec crond -f

qgs_cache_preseed.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
projects=$(find /data -name '*'${QGS_EXT})
4+
5+
preseed() {
6+
service_name=$1
7+
nr=$2
8+
echo "- Sending request $nr"
9+
code=$(curl -o /dev/null -s -w "%{http_code}" "http://qwc-qgis-server/ows/${service_name}?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0")
10+
echo "- Request $nr completed with status code $code"
11+
}
12+
13+
14+
find /data -name 'qwc_demo'${QGS_EXT} -print0 | while IFS= read -r -d $'\0' file; do
15+
echo "Processing project $file"
16+
service_name=$(echo $file | sed 's|^\/data/||' | sed "s|$QGS_EXT||")
17+
echo "- Service name is ${service_name}"
18+
19+
pids=""
20+
for i in $(seq 1 $FCGI_INSTANCES); do
21+
preseed "${service_name}" $i &
22+
pid=$!
23+
pids="$pids $pid"
24+
done
25+
26+
for pid in $pids; do
27+
wait $pid
28+
done
29+
30+
echo ""
31+
echo "Sleeping for $SLEEP_INTERVAL seconds..."
32+
sleep $SLEEP_INTERVAL
33+
echo ""
34+
done
35+
echo "Done!"

0 commit comments

Comments
 (0)