Skip to content

Commit 72e7b83

Browse files
committed
fix(backend): use internal schema cache for task workers
Signed-off-by: Fatih Acar <[email protected]>
1 parent 7f3358f commit 72e7b83

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

backend/infrahub/core/schema/schema_branch.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def to_dict_schema_object(self, duplicate: bool = False) -> dict[str, dict[str,
162162
"templates": {name: self.get(name, duplicate=duplicate) for name in self.templates},
163163
}
164164

165+
def to_dict_api_schema_object(self, duplicate: bool = False) -> dict[str, list[dict]]:
166+
return {
167+
"nodes": [self.get(name, duplicate=duplicate).model_dump() for name in self.nodes],
168+
"profiles": [self.get(name, duplicate=duplicate).model_dump() for name in self.profiles],
169+
"generics": [self.get(name, duplicate=duplicate).model_dump() for name in self.generics],
170+
"templates": [self.get(name, duplicate=duplicate).model_dump() for name in self.templates],
171+
}
172+
165173
@classmethod
166174
def from_dict_schema_object(cls, data: dict) -> Self:
167175
type_mapping = {

backend/infrahub/git/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ async def generate_request_artifact_definition(
378378
) -> None:
379379
await add_tags(branches=[model.branch])
380380

381-
client = get_client()
381+
client = get_client(branch=model.branch)
382382

383383
# Needs to be fetched before fetching group members otherwise `object` relationship would override
384384
# existing node in client store without the `name` attribute due to #521

backend/infrahub/groups/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
async def update_graphql_query_group(model: RequestGraphQLQueryGroupUpdate) -> None:
1313
"""Create or Update a GraphQLQueryGroup."""
1414

15-
client = get_client()
15+
client = get_client(branch=model.branch)
1616

1717
# If there is only one subscriber, associate the task to it
1818
# If there are more than one, for now we can't associate all of them

backend/infrahub/workers/dependencies.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from infrahub import config
88
from infrahub.components import ComponentType
99
from infrahub.constants.environment import INSTALLATION_TYPE
10+
from infrahub.core.registry import registry
1011
from infrahub.database import InfrahubDatabase, get_db
1112
from infrahub.services.adapters.cache import InfrahubCache
1213
from infrahub.services.adapters.event import InfrahubEventService
@@ -33,12 +34,21 @@ def get_component_type() -> ComponentType:
3334
raise ValueError("Component type is not set. It needs to be initialized before working with services.") from exc
3435

3536

36-
def build_client() -> InfrahubClient:
37-
return InfrahubClient(config=Config(address=config.SETTINGS.main.internal_address, retry_on_failure=True))
37+
def build_client(branch: str | None = None) -> InfrahubClient:
38+
client = InfrahubClient(config=Config(address=config.SETTINGS.main.internal_address, retry_on_failure=True))
39+
40+
# Populate client schema cache using our internal schema cache
41+
_branch = branch or registry.default_branch
42+
schema_branch = registry.schema.get_schema_branch(_branch)
43+
client.schema.set_cache(schema=schema_branch.to_dict_api_schema_object(), branch=_branch)
44+
return client
3845

3946

4047
@inject
41-
def get_client(client: InfrahubClient = Depends(build_client)) -> InfrahubClient: # noqa: B008
48+
def get_client(
49+
branch: str | None = None, # noqa: ARG001
50+
client: InfrahubClient = Depends(build_client), # noqa: B008
51+
) -> InfrahubClient:
4252
return client
4353

4454

0 commit comments

Comments
 (0)