Read-only mirror of https://github.com/SwissFederalArchives/lindas-clear-sparql-cache-endpoint — Schweizerisches Bundesarchiv. Issues & pull requests at the source.
  • JavaScript 99.5%
  • Dockerfile 0.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-30 11:50:14 +01:00
.changeset fix: bound metadata observation to dataset resources (#64) 2026-11-30 11:50:14 +01:00
.github ci: Bump docker/login-action from 3 to 4 2026-11-30 11:27:52 +01:00
lib fix: bound metadata observation to dataset resources (#64) 2026-11-30 11:50:14 +01:00
test feat: track dataset metadata changes for cache diagnostics (#62) 2026-11-30 11:38:00 +01:00
.gitignore fix: ignore installed Node.js dependencies 2026-10-27 10:48:16 +01:00
.nvmrc chore: node to 22.23.4 2026-10-27 10:04:20 +01:00
CHANGELOG.md chore: release 2026-10-27 10:54:35 +01:00
Dockerfile chore: node to 22.23.4 2026-10-27 10:04:20 +01:00
eslint.config.js Initial mirror from rareba/clear-sparql-cache-endpoint 2025-10-17 13:00:43 +02:00
example.env feat: track dataset metadata changes for cache diagnostics (#62) 2026-11-30 11:38:00 +01:00
index.js feat: track dataset metadata changes for cache diagnostics (#62) 2026-11-30 11:38:00 +01:00
LICENSE Initial mirror from rareba/clear-sparql-cache-endpoint 2025-10-17 13:00:43 +02:00
package-lock.json chore: refresh locked ESLint to 10.16.0 (#63) 2026-11-30 11:46:46 +01:00
package.json feat: track dataset metadata changes for cache diagnostics (#62) 2026-11-30 11:38:00 +01:00
README.md fix: bound metadata observation to dataset resources (#64) 2026-11-30 11:50:14 +01:00

Clear SPARQL cache endpoint

This assumes that your cached endpoint is using Varnish and has the xkey module enabled. You can take a look at our custom varnish-post repository that comes with all required configuration for the cached endpoint.

Get started

Use Node.js 22.23.4 or a newer Node.js 22 release. Version 22.23.4 fixes CVE-2026-111497 in the bundled Brotli decoder. With nvm, run nvm install and nvm use to select the version in .nvmrc.

npm install # Install dependencies
cp example.env .env # Copy environment variables file
# Open your editor, and fill the environment variables in the `.env` file
npm run start # Start the script

For container deployments, rebuild the image and redeploy the scheduled job to use the patched runtime. Existing images retain their original Node.js version; retagging an old image does not apply the fix.

Environment variables

Name Description Default Value
CACHE_ENDPOINT The URL of the cache endpoint ""
CACHE_ENDPOINT_USERNAME The username for the cache endpoint ""
CACHE_ENDPOINT_PASSWORD The password for the cache endpoint ""
CACHE_DEFAULT_ENTRY_NAME The default entry name for the cache "default"
CACHE_TAG_HEADER The header name for the cache tag "xkey"
SUPPORT_URL_ENCODED Whether to clear the cache for the URL-encoded version of the dataset URI "true"
SPARQL_ENDPOINT_URL The URL of the SPARQL endpoint ""
SPARQL_USERNAME The username for the SPARQL endpoint ""
SPARQL_PASSWORD The password for the SPARQL endpoint ""
S3_ENABLED Whether to use S3 for caching "false"
S3_LAST_TIMESTAMP_KEY The key for the last timestamp file in S3 "last_timestamp.txt"
S3_SIMPLE_DATE_WORKAROUND_KEY The key for the simple date workaround file in S3 "simple_date_workaround.txt"
S3_BUCKET The S3 bucket name "default"
S3_ACCESS_KEY_ID The S3 access key ID ""
S3_SECRET_ACCESS_KEY The S3 secret access key ""
S3_REGION The S3 region "default"
S3_ENDPOINT The S3 endpoint ""
S3_SSL_ENABLED Whether to use SSL for S3 "false"
S3_FORCE_PATH_STYLE Whether to force path style for S3 "false"

If S3_ENABLED is set to true, the first time you run the script you might see an error message saying that the last timestamp file does not exist. This is expected, and the script will create the file automatically at the end of the first run, and will update that file every time it runs. You will not see this error message again after the first run.

You might also get a similar error about a simple date workaround file. This is also expected, and the script will create the file automatically at the end of the first run, and will update that file every time it runs.

Using S3 allows us to trick a bit for the cases where dateModified returned by the SPARQL query is a date and not a dateTime. The trick makes sure that the cache is invalidated for this entry only the first time, and the day after the dateModified date. Without this trick, the cache would be invalidated every time the script runs until the day after its value.

Diagnostics

LOG_LEVEL=0 (default; aliases detailed, debug) logs every discovery decision. LOG_LEVEL=1 (aliases normal, info) logs summaries, warnings and errors. JSON events use runStartedAt for correlation and Graylog's source for the node. Existing checkpoint, selection, skip and purge wording is retained in each event's message field for searches across old/new logs, without duplicate text lines. Verbosity filtering still applies. S3 state dumps remain counts; Varnish responses retain the status prefix and invalidation count rather than the full HTML body. The familiar "Found … cache entries to clear:" text is now on the completion summary, after purge results, alongside the failure count. Run-start events contain the checkpoint and default marker datatype; decisions contain the dataset IRI, returned marker, reason and any midnight adjustment.

Enable METADATA_OBSERVATION_ENABLED=true with S3 to track metadata changes across datasets. It is disabled by default and enabled only on TEST in GitOps. Selected decisions, purge results and checkpoint writes remain visible in normal mode when observation is enabled. Discovery and invalidation behavior are unchanged.

One extra query per run reads supported dataset types and HTTP/HTTPS schema.org modification markers for cube.link, VOID, schema.org, DCAT and Data Cube datasets. Missing and invalid markers remain visible. Marker-only subjects without a supported dataset type are excluded to avoid scanning vocabulary terms and other unrelated resources. Losing the last supported type appears as a disappearance; resources never carrying a supported type are outside the inventory. Dataset content and unrelated RDF types are not retained.

The separate S3 object ${S3_LAST_TIMESTAMP_KEY}.metadata-v1.json stores the last complete baseline and observation times; use independent buckets/keys per node. The first run logs each dataset once. Later metadata_changed events record only new, changed, excluded or disappearing entries, with previous/current metadata, checkpoint and selection decision. Metadata tuples are [predicate, term type, value, datatype, language]; unstable blank-node labels are omitted. Warnings flag missing/invalid markers, HTTPS-only markers and unselected changes with old markers. metadata_observation_completed reports counts, duration and the previous observation.

Observation has a ten-second deadline, 8 MiB response/state limits and a 50,000-row limit. Failures emit metadata_observation_failed and retain the previous baseline; only S3 NoSuchKey starts a new one. Monitor failures and missing scheduled runs, and retain logs for the investigation period (30 days recommended). Retention and alert routing are configured separately; S3 keeps the latest baseline only.

Observation time is not commit time. Discovery and observation are separate reads; transient changes between runs and content changes with unchanged metadata remain invisible. The endpoint must fail explicitly rather than silently return partial results. Client cancellation does not guarantee immediate server cancellation; measure TEST query duration before wider rollout. This does not fix missed invalidation or checkpoint advancement after failed purges.

License

This project is licensed under the MIT License - see the LICENSE file for details.