|
31 | 31 | import logging |
32 | 32 | import logging.handlers |
33 | 33 | from functools import total_ordering |
34 | | -from os import readlink |
| 34 | +from os import getenv, readlink |
35 | 35 | import re |
36 | 36 | import shlex |
37 | 37 | import shutil |
@@ -895,13 +895,8 @@ def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None: |
895 | 895 |
|
896 | 896 | logging.info("%s files changed", len(changed)) |
897 | 897 | if changed and not self.skip_cache_invalidation: |
898 | | - targets_dir = str(self.www_root) |
899 | | - prefixes = run(["find", "-L", targets_dir, "-samefile", target]).stdout |
900 | | - prefixes = prefixes.replace(targets_dir + "/", "") |
901 | | - prefixes = [prefix + "/" for prefix in prefixes.split("\n") if prefix] |
902 | | - purge(http, *prefixes) |
903 | | - for prefix in prefixes: |
904 | | - purge(http, *[prefix + p for p in changed]) |
| 898 | + surrogate_key = f"{self.language.tag}/{self.version.name}" |
| 899 | + purge_surrogate_key(http, surrogate_key) |
905 | 900 | logging.info( |
906 | 901 | "Publishing done (%s).", format_seconds(perf_counter() - start_time) |
907 | 902 | ) |
@@ -1007,7 +1002,8 @@ def symlink( |
1007 | 1002 | link.symlink_to(directory) |
1008 | 1003 | run(["chown", "-h", ":" + group, str(link)]) |
1009 | 1004 | if not skip_cache_invalidation: |
1010 | | - purge_path(http, www_root, link) |
| 1005 | + surrogate_key = f"{language.tag}/{name}" |
| 1006 | + purge_surrogate_key(http, surrogate_key) |
1011 | 1007 |
|
1012 | 1008 |
|
1013 | 1009 | def major_symlinks( |
@@ -1081,14 +1077,25 @@ def purge(http: urllib3.PoolManager, *paths: Path | str) -> None: |
1081 | 1077 | http.request("PURGE", url, timeout=30) |
1082 | 1078 |
|
1083 | 1079 |
|
1084 | | -def purge_path(http: urllib3.PoolManager, www_root: Path, path: Path) -> None: |
1085 | | - """Recursively remove a path from docs.python.org's CDN. |
| 1080 | +def purge_surrogate_key(http: urllib3.PoolManager, surrogate_key: str) -> None: |
| 1081 | + """Remove paths from docs.python.org's CDN. |
1086 | 1082 |
|
| 1083 | + All paths matching the given 'Surrogate-Key' will be removed. |
| 1084 | + This is set by the Nginx server for every language-version pair. |
1087 | 1085 | To be used when a directory changes, so the CDN fetches the new one. |
| 1086 | +
|
| 1087 | + https://www.fastly.com/documentation/reference/api/purging/#purge-tag |
1088 | 1088 | """ |
1089 | | - purge(http, *[file.relative_to(www_root) for file in path.glob("**/*")]) |
1090 | | - purge(http, path.relative_to(www_root)) |
1091 | | - purge(http, str(path.relative_to(www_root)) + "/") |
| 1089 | + service_id = getenv("FASTLY_SERVICE_ID", "__UNSET__") |
| 1090 | + fastly_key = getenv("FASTLY_TOKEN", "__UNSET__") |
| 1091 | + |
| 1092 | + logging.info("Purging Surrogate-Key '%s' from CDN", surrogate_key) |
| 1093 | + http.request( |
| 1094 | + "POST", |
| 1095 | + f"https://api.fastly.com/service/{service_id}/purge/{surrogate_key}", |
| 1096 | + headers={"Fastly-Key": fastly_key}, |
| 1097 | + timeout=30, |
| 1098 | + ) |
1092 | 1099 |
|
1093 | 1100 |
|
1094 | 1101 | def proofread_canonicals( |
|
0 commit comments