From d86fda2af8869c68cbde2a5a41d13620b6bfc4d7 Mon Sep 17 00:00:00 2001 From: Schneems Date: Mon, 9 Sep 2024 16:03:26 -0500 Subject: [PATCH] Remove InAppDirCacheLayer in favor of struct api --- commons/src/cache.rs | 1 - commons/src/cache/app_cache.rs | 34 ++++++++- commons/src/cache/in_app_dir_cache_layer.rs | 81 --------------------- 3 files changed, 31 insertions(+), 85 deletions(-) delete mode 100644 commons/src/cache/in_app_dir_cache_layer.rs diff --git a/commons/src/cache.rs b/commons/src/cache.rs index f7b2ce2..917e476 100644 --- a/commons/src/cache.rs +++ b/commons/src/cache.rs @@ -3,7 +3,6 @@ mod app_cache_collection; mod clean; mod config; mod error; -mod in_app_dir_cache_layer; pub use self::app_cache::{build, PathState}; pub use self::app_cache::{AppCache, CacheState}; diff --git a/commons/src/cache/app_cache.rs b/commons/src/cache/app_cache.rs index 8fa64d5..6f0224e 100644 --- a/commons/src/cache/app_cache.rs +++ b/commons/src/cache/app_cache.rs @@ -1,10 +1,11 @@ use crate::cache::clean::{lru_clean, FilesWithSize}; -use crate::cache::in_app_dir_cache_layer::InAppDirCacheLayer; use crate::cache::{CacheConfig, CacheError, KeepPath}; use byte_unit::{AdjustedByte, Byte}; use fs_extra::dir::CopyOptions; use libcnb::build::BuildContext; use libcnb::data::layer::LayerName; +use libcnb::layer::{CachedLayerDefinition, InvalidMetadataAction, RestoredLayerAction}; +use serde::{Deserialize, Serialize}; use std::path::Path; use std::path::PathBuf; @@ -249,11 +250,33 @@ pub fn build( let layer_name = create_layer_name(&context.app_dir, &path)?; let create_state = layer_name_cache_state(&context.layers_dir, &layer_name); + let metadata = InAppDirCacheLayerMetadata { + app_dir_path: path.clone(), + }; + let layer = context - .handle_layer(layer_name, InAppDirCacheLayer::new(path.clone())) + .cached_layer( + layer_name, + CachedLayerDefinition { + launch: false, + build: true, + invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer, + restored_layer_action: &|old: &InAppDirCacheLayerMetadata, _| { + if old.app_dir_path == metadata.app_dir_path { + Ok(RestoredLayerAction::KeepLayer) + } else { + Ok(RestoredLayerAction::DeleteLayer) + } + }, + }, + ) .map_err(|error| CacheError::InternalLayerError(format!("{error:?}")))?; - let cache = layer.path; + layer + .write_metadata(metadata) + .map_err(|error| CacheError::InternalLayerError(format!("{error:?}")))?; + + let cache = layer.path(); Ok(AppCache { path, @@ -264,6 +287,11 @@ pub fn build( }) } +#[derive(Deserialize, Serialize, Debug, Clone)] +pub(crate) struct InAppDirCacheLayerMetadata { + app_dir_path: PathBuf, +} + /// Copy contents of application path into the cache /// /// This action preserves the contents in the application path. diff --git a/commons/src/cache/in_app_dir_cache_layer.rs b/commons/src/cache/in_app_dir_cache_layer.rs deleted file mode 100644 index 7a60b14..0000000 --- a/commons/src/cache/in_app_dir_cache_layer.rs +++ /dev/null @@ -1,81 +0,0 @@ -use libcnb::build::BuildContext; -use libcnb::data::layer_content_metadata::LayerTypes; -use libcnb::layer::{ExistingLayerStrategy, Layer, LayerData, LayerResult, LayerResultBuilder}; -use libcnb::Buildpack; -use serde::{Deserialize, Serialize}; -use std::marker::PhantomData; -use std::path::Path; -use std::path::PathBuf; - -/// # Caches a folder in the application directory -/// -/// Layers are used for caching, however layers cannot be inside of the app directory. -/// This layer can be used to hold a directory's contents so they are preserved -/// between deploys. -/// -/// The primary usecase of this is for caching assets. After `rake assets:precompile` runs -/// file in `/public/assets` need to be preserved between deploys. This allows -/// for faster deploys, and also allows for prior generated assets to remain on the system -/// until "cleaned." -/// -/// Historically, sprockets will keep 3 versions of old files on disk. This -/// allows for emails, that might live a long time, to reference a specific SHA of an -/// asset. -#[derive(Deserialize, Serialize, Debug, Clone)] -pub(crate) struct InAppDirCacheLayer { - pub(crate) app_dir_path: PathBuf, - buildpack: PhantomData, -} - -#[derive(Deserialize, Serialize, Debug, Clone)] -pub(crate) struct InAppDirCacheLayerMetadata { - app_dir_path: PathBuf, -} - -impl InAppDirCacheLayer { - pub(crate) fn new(app_dir_path: PathBuf) -> Self { - Self { - app_dir_path, - buildpack: PhantomData, - } - } -} - -impl Layer for InAppDirCacheLayer -where - B: Buildpack, -{ - type Buildpack = B; - type Metadata = InAppDirCacheLayerMetadata; - - fn types(&self) -> LayerTypes { - LayerTypes { - build: true, - launch: true, - cache: true, - } - } - - fn create( - &mut self, - _context: &BuildContext, - _layer_path: &Path, - ) -> Result, B::Error> { - LayerResultBuilder::new(InAppDirCacheLayerMetadata { - app_dir_path: self.app_dir_path.clone(), - }) - .build() - } - - fn existing_layer_strategy( - &mut self, - _context: &BuildContext, - layer_data: &LayerData, - ) -> Result { - if self.app_dir_path == layer_data.content_metadata.metadata.app_dir_path { - Ok(ExistingLayerStrategy::Keep) - } else { - Ok(ExistingLayerStrategy::Recreate) - } - } -}