Skip to content

Commit

Permalink
Merge pull request #712 from rajulkumar/listings_cache_flush
Browse files Browse the repository at this point in the history
"deploy-config" includes listings for CDN cache flush
  • Loading branch information
rohanpm committed May 19, 2024
2 parents 15cf38a + fa94b4b commit d39bcad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions exodus_gw/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ class Settings(BaseSettings):
have been configured.
"""

cdn_listing_flush: bool = True
"""Whether listing paths in the config should be flushed while deploying the
config."""

cdn_cookie_ttl: int = 60 * 720
"""Time (in seconds) cookies generated by ``cdn-redirect`` remain valid."""

Expand Down
26 changes: 26 additions & 0 deletions exodus_gw/worker/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ def complete_deploy_config_task(
)


def _listing_paths_for_flush(config: dict[str, Any]) -> set[str]:
# extract listing paths from config that might have
# updated values influencing the response of /listing
# endpoint
listing_paths: set[str] = set()

for path in config.get("listing", {}).keys():
lpath = path + "/listing"
LOG.info(
"Listing %s will flush cache for %s",
path,
lpath,
extra={"event": "deploy"},
)
listing_paths.add(lpath)

return listing_paths


@dramatiq.actor(
time_limit=Settings().actor_time_limit,
max_backoff=Settings().actor_max_backoff,
Expand Down Expand Up @@ -135,6 +154,13 @@ def deploy_config(
)
flush_paths.add(published_path.web_uri)

# Include all the listing paths for flush when enabled in settings
flush_paths = (
flush_paths.union(_listing_paths_for_flush(config))
if settings.cdn_listing_flush
else flush_paths
)

# TTL must be sent in milliseconds but the setting is in minutes for
# convenience and consistency with other components.
ttl = settings.config_cache_ttl * 60000
Expand Down
12 changes: 10 additions & 2 deletions tests/worker/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import mock

from exodus_gw import models, worker
from exodus_gw import models, settings, worker
from exodus_gw.models.path import PublishedPath

NOW_UTC = datetime.now(timezone.utc)
Expand Down Expand Up @@ -36,7 +36,13 @@ def test_deploy_config(
db.add(t)
db.commit()

worker.deploy_config(fake_config, "test", NOW_UTC)
# disable cache flush for listings
updated_settings = settings.Settings()
updated_settings.cdn_listing_flush = False

worker.deploy_config(
fake_config, "test", NOW_UTC, settings=updated_settings
)

# It should've created an appropriate put request.
request = {
Expand Down Expand Up @@ -181,6 +187,8 @@ def test_deploy_config_with_flush(
assert body["kwargs"]["env"] == "test"
assert body["kwargs"]["flush_paths"] == [
# It figured out that cache will need to be flushed for these.
"/content/dist/rhel/server/8/listing",
"/content/dist/rhel/server/listing",
"/content/testproduct/1/file1",
"/content/testproduct/1/file2",
]
Expand Down

0 comments on commit d39bcad

Please sign in to comment.