-
Notifications
You must be signed in to change notification settings - Fork 47
IFC-2334: Webhook cache invalidation on KeyValue changes #8602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
polmichel
merged 15 commits into
pmi-20260227-webhooks-ifc-2272
from
pmi-20260312-webhooks-cache-invalidation-mngmt
Mar 17, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
aa40239
Encapsulate raw strings into small detectable objects
polmichel ddd6aa0
new Query to match WebHooks related to headers given in input
polmichel 5d11dbb
new cache invalidation Prefect task
polmichel a78ae61
new trigger to manage Webhook headers invalidation and related impacts
polmichel 60fdd1e
integration test against the cache invalidation system
polmichel 51d8a3e
feedback session with Claude
polmichel 88ea8dc
Make the integration test less dependent on implementation details
polmichel 93c91da
fix KeyValue instantiation in tests
polmichel c9fde3a
adapted EnvironementVariableKeyValue renaming
polmichel ca1a1f3
Query should take in account active status of edges
polmichel 5875ffa
update related to rebase issue
polmichel 4223ebe
add new test and fix the query
polmichel 7c7e148
replaced the Headers -> Webhooks Cypher query by using the abstractio…
polmichel 148d8f4
replaced hard-coded strings by dynamic references
polmichel a7fe5e6
use assert_never structure while handling HeaderKind values
polmichel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from prefect import task | ||
| from prefect.cache_policies import NONE | ||
| from prefect.logging import get_run_logger | ||
|
|
||
| from infrahub.workers.dependencies import get_cache | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Set as AbstractSet | ||
|
|
||
|
|
||
| @task(name="webhook-invalidate-cache", task_run_name="Invalidate webhook cache", cache_policy=NONE) | ||
| async def invalidate_webhook_cache(webhook_ids: AbstractSet[str]) -> None: | ||
| """Delete cached webhook data for the given webhook IDs.""" | ||
| cache = await get_cache() | ||
| log = get_run_logger() | ||
| for wid in webhook_ids: | ||
| await cache.delete(key=f"webhook:{wid}") | ||
| log.info(f"Invalidated cache for {len(webhook_ids)} webhook(s)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from prefect import flow | ||
| from prefect.logging import get_run_logger | ||
| from prefect.runtime import flow_run | ||
|
|
||
| from infrahub.core.constants import InfrahubKind | ||
| from infrahub.core.manager import NodeManager | ||
| from infrahub.workers.dependencies import get_database | ||
|
|
||
| from .cache import invalidate_webhook_cache | ||
|
|
||
| if TYPE_CHECKING: | ||
| from prefect import Flow, State | ||
| from prefect.client.schemas.objects import FlowRun | ||
|
|
||
|
|
||
| def _invalidate_webhook_headers_run_name() -> str: | ||
| params = flow_run.parameters | ||
| event_data = params.get("event_data") | ||
| keyvalue_id = event_data["node_id"] if event_data else "unknown" | ||
| return f"Invalidate webhook headers (KeyValue {keyvalue_id})" | ||
|
|
||
|
|
||
| async def _invalidate_webhook_headers_on_failure(flow: Flow, flow_run: FlowRun, state: State) -> None: # noqa: ARG001 | ||
| log = get_run_logger() | ||
| event_data = flow_run.parameters.get("event_data") | ||
| keyvalue_id = event_data.get("node_id") if event_data else None | ||
| log.error( | ||
| "Webhook header invalidation failed: keyvalue_id=%s state_message=%s", | ||
| keyvalue_id, | ||
| state.message, | ||
| ) | ||
|
|
||
|
|
||
| @flow( | ||
| name="webhook-invalidate-headers", | ||
| flow_run_name=_invalidate_webhook_headers_run_name, | ||
| on_failure=[_invalidate_webhook_headers_on_failure], | ||
| ) | ||
| async def invalidate_webhook_headers( | ||
| event_type: str | None = None, # noqa: ARG001 | ||
| event_data: dict | None = None, | ||
| ) -> None: | ||
| """Resolve webhooks referencing a KeyValue node and invalidate their cache.""" | ||
| log = get_run_logger() | ||
|
|
||
| keyvalue_id = event_data["node_id"] if event_data else None | ||
| if not keyvalue_id: | ||
| log.warning("No KeyValue ID provided, skipping") | ||
| return | ||
|
|
||
| database = await get_database() | ||
|
|
||
| async with database.start_session(read_only=True) as db: | ||
| webhooks = await NodeManager.query( | ||
| db=db, | ||
| schema=InfrahubKind.WEBHOOK, | ||
| filters={"headers__ids": [keyvalue_id]}, | ||
| branch_agnostic=True, | ||
| ) | ||
| webhook_uuids = frozenset(w.id for w in webhooks) | ||
|
|
||
| if webhook_uuids: | ||
| await invalidate_webhook_cache(webhook_ids=webhook_uuids) | ||
| else: | ||
| log.info(f"No webhooks reference KeyValue {keyvalue_id}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.