Skip to content
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

Cache expensive data collection. #57

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion softpack_core/schemas/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ class Environment:
state: Optional[State]
tags: list[str]
hidden: bool
cachedEnvs: list["Environment"] = None
envsUpdates: bool = True
sb10 marked this conversation as resolved.
Show resolved Hide resolved

requested: Optional[datetime.datetime] = None
build_start: Optional[datetime.datetime] = None
Expand All @@ -335,6 +337,9 @@ def iter(cls) -> list["Environment"]:
Returns:
Iterable[Environment]: An iterator of Environment objects.
"""
if not cls.envsUpdates:
return cls.cachedEnvs

statuses = BuildStatus.get_all()
if isinstance(statuses, BuilderError):
statuses = []
Expand Down Expand Up @@ -366,6 +371,9 @@ def iter(cls) -> list["Environment"]:
env.build_start = status.build_start
env.build_done = status.build_done

cls.cachedEnvs = environment_objects
cls.envsUpdates = False

return environment_objects

@classmethod
Expand Down Expand Up @@ -541,6 +549,7 @@ def create_new_env(
],
True,
)
cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "create environment folder")
except RuntimeError as e:
return InvalidInputError(
Expand Down Expand Up @@ -640,7 +649,7 @@ def store_metadata(cls, environment_path: Path, metadata: Box) -> None:
metadata.to_yaml(),
overwrite=True,
)

cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "update metadata")

@classmethod
Expand Down Expand Up @@ -677,6 +686,7 @@ def delete(cls, name: str, path: str) -> DeleteResponse: # type: ignore
"""
if artifacts.get(Path(path), name):
tree_oid = artifacts.delete_environment(name, path)
cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "delete environment")
return DeleteEnvironmentSuccess(
message="Successfully deleted the environment"
Expand Down Expand Up @@ -846,6 +856,7 @@ async def write_artifacts(
tree_oid = artifacts.create_files(
Path(folder_path), new_files, overwrite=True
)
cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "write artifact")
return WriteArtifactSuccess(
message="Successfully written artifact(s)",
Expand Down
8 changes: 6 additions & 2 deletions softpack_core/schemas/package_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ class PackageCollection:
packages: list[PackageMultiVersion]

@classmethod
def iter(cls) -> Iterable["PackageMultiVersion"]:
def iter(cls) -> list["PackageMultiVersion"]:
"""Get an iterator over PackageCollection objects.

Returns:
Iterable[PackageCollection]: An iterator of PackageCollection
objects.
"""
return map(cls.from_package, app.spack.packages())
if app.spack.packagesUpdated:
cls.packages = list(map(cls.from_package, app.spack.packages()))
app.spack.packagesUpdated = False

return cls.packages

@classmethod
def from_package(cls, package: Package) -> PackageMultiVersion:
Expand Down
4 changes: 4 additions & 0 deletions softpack_core/spack.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Package(PackageBase):
class Spack:
"""Spack interface class."""

packages: list[Package]
packagesUpdated: bool = True

def __init__(
self,
spack_exe: str = "spack",
Expand Down Expand Up @@ -97,6 +100,7 @@ def store_packages_from_spack(
json.loads(jsonData),
)
)
self.packagesUpdated = True

def __readPackagesFromCacheOnce(self) -> Tuple[bytes, bool]:
if len(self.stored_packages) > 0 or self.cacheDir == "":
Expand Down
Loading