Skip to content

Commit 8aa2f69

Browse files
committed
Purge using Surrogate-Key headers
1 parent a97ad68 commit 8aa2f69

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

build_docs.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import logging
3232
import logging.handlers
3333
from functools import total_ordering
34-
from os import readlink
34+
from os import getenv, readlink
3535
import re
3636
import shlex
3737
import shutil
@@ -892,13 +892,8 @@ def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None:
892892

893893
logging.info("%s files changed", len(changed))
894894
if changed and not self.skip_cache_invalidation:
895-
targets_dir = str(self.www_root)
896-
prefixes = run(["find", "-L", targets_dir, "-samefile", target]).stdout
897-
prefixes = prefixes.replace(targets_dir + "/", "")
898-
prefixes = [prefix + "/" for prefix in prefixes.split("\n") if prefix]
899-
purge(http, *prefixes)
900-
for prefix in prefixes:
901-
purge(http, *[prefix + p for p in changed])
895+
surrogate_key = f"{self.language.tag}/{self.version.name}"
896+
purge_surrogate_key(http, surrogate_key)
902897
logging.info(
903898
"Publishing done (%s).", format_seconds(perf_counter() - start_time)
904899
)
@@ -1004,7 +999,8 @@ def symlink(
1004999
link.symlink_to(directory)
10051000
run(["chown", "-h", ":" + group, str(link)])
10061001
if not skip_cache_invalidation:
1007-
purge_path(http, www_root, link)
1002+
surrogate_key = f"{language.tag}/{name}"
1003+
purge_surrogate_key(http, surrogate_key)
10081004

10091005

10101006
def major_symlinks(
@@ -1076,14 +1072,25 @@ def purge(http: urllib3.PoolManager, *paths: Path | str) -> None:
10761072
http.request("PURGE", url, timeout=30)
10771073

10781074

1079-
def purge_path(http: urllib3.PoolManager, www_root: Path, path: Path) -> None:
1080-
"""Recursively remove a path from docs.python.org's CDN.
1075+
def purge_surrogate_key(http: urllib3.PoolManager, surrogate_key: str) -> None:
1076+
"""Remove paths from docs.python.org's CDN.
10811077
1078+
All paths matching the given 'Surrogate-Key' will be removed.
1079+
This is set by the Nginx server for every language-version pair.
10821080
To be used when a directory changes, so the CDN fetches the new one.
1081+
1082+
https://www.fastly.com/documentation/reference/api/purging/#purge-tag
10831083
"""
1084-
purge(http, *[file.relative_to(www_root) for file in path.glob("**/*")])
1085-
purge(http, path.relative_to(www_root))
1086-
purge(http, str(path.relative_to(www_root)) + "/")
1084+
service_id = getenv("FASTLY_SERVICE_ID", "__UNSET__")
1085+
fastly_key = getenv("FASTLY_TOKEN", "__UNSET__")
1086+
1087+
logging.info("Purging Surrogate-Key '%s' from CDN", surrogate_key)
1088+
http.request(
1089+
"POST",
1090+
f"https://api.fastly.com/service/{service_id}/purge/{surrogate_key}",
1091+
headers={"Fastly-Key": fastly_key},
1092+
timeout=30,
1093+
)
10871094

10881095

10891096
def proofread_canonicals(

0 commit comments

Comments
 (0)