Skip to content

Commit

Permalink
Merge pull request #56 from lsst-sqre/tickets/DM-40250
Browse files Browse the repository at this point in the history
DM-40250: Delete nbhtml instances from redis when a page updates
  • Loading branch information
jonathansick authored Jul 31, 2023
2 parents 935bbf2 + de4adec commit ff512ce
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Collect fragments into this file with: scriv collect --version X.Y.Z

<!-- scriv-insert-here -->

<a id='changelog-0.9.1'></a>
## 0.9.1 (2023-07-31)

### Bug fixes

- When a page is updated (e.g., from a GitHub pull request merge), the HTML renders for that page are cleared.
- Fixed a bug where updating a page and executing it with defaults would result in two requests to Noteburst.
- Deleting a page now deletes the page's HTML renders.

<a id='changelog-0.9.0'></a>

## 0.9.0 (2023-07-27)
Expand Down
21 changes: 16 additions & 5 deletions src/timessquare/domain/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,17 +648,28 @@ class PageSummaryModel:


@dataclass
class PageInstanceIdModel:
class PageIdModel:
"""A domain model that identifies a page and creates a reproducible key
string for a page.
"""

name: str
"""The name of the page, which is used as a URL path component (slug)."""

@property
def cache_key_prefix(self) -> str:
return f"{self.name}/"


@dataclass
class PageInstanceIdModel(PageIdModel):
"""The domain model that identifies an instance of a page through the
page's name and values of parameters.
The `cache_key` property produces a reproducible key string, useful as
a Redis key.
"""

name: str
"""The name of the page, which is used as a URL path component (slug)."""

values: dict[str, Any]
"""The values of a page instance's parameters.
Expand All @@ -673,7 +684,7 @@ def cache_key(self) -> str:
"utf-8"
)
).decode("utf-8")
return f"{self.name}/{encoded_values_key}"
return f"{super().cache_key_prefix}{encoded_values_key}"


@dataclass
Expand Down
12 changes: 9 additions & 3 deletions src/timessquare/services/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,15 @@ async def get_github_pr_tree(
)

async def update_page_in_store(self, page: PageModel) -> None:
"""Update the page in the database."""
"""Update the page in the database.
Algorithm is:
1. Update the page in Postgres
2. Delete all cached HTML for the page from redis
"""
await self._page_store.update_page(page)
await self.execute_page_with_defaults(page)
await self._html_store.delete_objects_for_page(page.name)

async def update_page_and_execute(
self, page: PageModel, *, enable_retry: bool = True
Expand Down Expand Up @@ -233,7 +239,7 @@ async def execute_page_with_defaults(
async def soft_delete_page(self, page: PageModel) -> None:
"""Soft delete a page by setting its date_deleted field."""
page.date_deleted = datetime.now(UTC)
await self._page_store.update_page(page)
await self.update_page_in_store(page)

async def soft_delete_pages_for_repo(self, owner: str, name: str) -> None:
"""Soft delete all pages backed by a specific GitHub repository."""
Expand Down
6 changes: 6 additions & 0 deletions src/timessquare/storage/nbhtmlcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from redis.asyncio import Redis

from timessquare.domain.nbhtml import NbHtmlModel
from timessquare.domain.page import PageIdModel

from .redisbase import RedisPageInstanceStore

Expand Down Expand Up @@ -37,3 +38,8 @@ async def store_nbhtml(
"""
key = nbhtml.create_key()
await super().store_instance(key, nbhtml, lifetime=lifetime)

async def delete_objects_for_page(self, page_name: str) -> None:
"""Delete all cached renders for a page."""
prefix = PageIdModel(name=page_name).cache_key_prefix
await self.delete_all(f"{prefix}*")

0 comments on commit ff512ce

Please sign in to comment.