11from __future__ import annotations
22
3- from datetime import timedelta
43from typing import TYPE_CHECKING , Any
54
5+ from cachetools import TTLCache , cached
6+ from cachetools .keys import hashkey
67from git .exc import BadName , GitCommandError
78from infrahub_sdk .exceptions import GraphQLError
89from prefect import task
9- from prefect .cache_policies import INPUTS
10- from prefect .locking .memory import MemoryLockManager
11- from prefect .transactions import IsolationLevel
10+ from prefect .cache_policies import NONE
1211from pydantic import Field
1312
1413from infrahub .core .constants import InfrahubKind , RepositoryInternalStatus
@@ -251,14 +250,13 @@ async def sync_from_remote(self, commit: str | None = None) -> None:
251250 await self .update_commit_value (branch_name = self .infrahub_branch_name , commit = commit )
252251
253252
254- @task (
255- name = "Fetch repository commit" ,
256- description = "Retrieve a git repository at a given commit, if it does not already exist locally" ,
257- cache_policy = INPUTS .configure (isolation_level = IsolationLevel .SERIALIZABLE , lock_manager = MemoryLockManager ())
258- - "client" ,
259- cache_expiration = timedelta (seconds = 30 ),
253+ @cached (
254+ TTLCache (maxsize = 100 , ttl = 30 ),
255+ key = lambda * _ , ** kwargs : hashkey (
256+ kwargs .get ("repository_id" ), kwargs .get ("name" ), kwargs .get ("repository_kind" ), kwargs .get ("commit" )
257+ ),
260258)
261- async def get_initialized_repo (
259+ async def _get_initialized_repo (
262260 client : InfrahubClient , repository_id : str , name : str , repository_kind : str , commit : str | None = None
263261) -> InfrahubReadOnlyRepository | InfrahubRepository :
264262 if repository_kind == InfrahubKind .REPOSITORY :
@@ -268,3 +266,16 @@ async def get_initialized_repo(
268266 return await InfrahubReadOnlyRepository .init (id = repository_id , name = name , commit = commit , client = client )
269267
270268 raise NotImplementedError (f"The repository kind { repository_kind } has not been implemented" )
269+
270+
271+ @task (
272+ name = "Fetch repository commit" ,
273+ description = "Retrieve a git repository at a given commit, if it does not already exist locally" ,
274+ cache_policy = NONE ,
275+ )
276+ async def get_initialized_repo (
277+ client : InfrahubClient , repository_id : str , name : str , repository_kind : str , commit : str | None = None
278+ ) -> InfrahubReadOnlyRepository | InfrahubRepository :
279+ return await _get_initialized_repo (
280+ client = client , repository_id = repository_id , name = name , repository_kind = repository_kind , commit = commit
281+ )
0 commit comments