Implement store-unload hooks

This commit is contained in:
Matthias Beyer 2016-05-26 18:40:58 +02:00
parent 2e80c29f47
commit 2ad5cb48aa
4 changed files with 31 additions and 2 deletions

View file

@ -117,7 +117,10 @@ pub fn config_is_valid(config: &Option<Value>) -> bool {
} }
match *config { match *config {
Some(Value::Table(ref t)) => { has_key_with_string_ary(t, "pre-create-hook-aspects") && Some(Value::Table(ref t)) => {
has_key_with_string_ary(t, "store-unload-hook-aspects") &&
has_key_with_string_ary(t, "pre-create-hook-aspects") &&
has_key_with_string_ary(t, "post-create-hook-aspects") && has_key_with_string_ary(t, "post-create-hook-aspects") &&
has_key_with_string_ary(t, "pre-retrieve-hook-aspects") && has_key_with_string_ary(t, "pre-retrieve-hook-aspects") &&
has_key_with_string_ary(t, "post-retrieve-hook-aspects") && has_key_with_string_ary(t, "post-retrieve-hook-aspects") &&
@ -143,6 +146,10 @@ pub fn config_is_valid(config: &Option<Value>) -> bool {
} }
} }
pub fn get_store_unload_aspect_names(value: &Option<Value>) -> Vec<String> {
get_aspect_names_for_aspect_position("store-unload-hook-aspects", value)
}
pub fn get_pre_create_aspect_names(value: &Option<Value>) -> Vec<String> { pub fn get_pre_create_aspect_names(value: &Option<Value>) -> Vec<String> {
get_aspect_names_for_aspect_position("pre-create-hook-aspects", value) get_aspect_names_for_aspect_position("pre-create-hook-aspects", value)
} }

View file

@ -1,5 +1,7 @@
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum HookPosition { pub enum HookPosition {
StoreUnload,
PreCreate, PreCreate,
PostCreate, PostCreate,
PreRetrieve, PreRetrieve,

View file

@ -171,6 +171,8 @@ pub struct Store {
* Registered hooks * Registered hooks
*/ */
store_unload_aspects : Arc<Mutex<Vec<Aspect>>>,
pre_create_aspects : Arc<Mutex<Vec<Aspect>>>, pre_create_aspects : Arc<Mutex<Vec<Aspect>>>,
post_create_aspects : Arc<Mutex<Vec<Aspect>>>, post_create_aspects : Arc<Mutex<Vec<Aspect>>>,
pre_retrieve_aspects : Arc<Mutex<Vec<Aspect>>>, pre_retrieve_aspects : Arc<Mutex<Vec<Aspect>>>,
@ -216,6 +218,12 @@ impl Store {
return Err(SE::new(SEK::StorePathExists, None)); return Err(SE::new(SEK::StorePathExists, None));
} }
let store_unload_aspects = get_store_unload_aspect_names(&store_config)
.into_iter().map(|n| {
let cfg = AspectConfig::get_for(&store_config, n.clone());
Aspect::new(n, cfg)
}).collect();
let pre_create_aspects = get_pre_create_aspect_names(&store_config) let pre_create_aspects = get_pre_create_aspect_names(&store_config)
.into_iter().map(|n| { .into_iter().map(|n| {
let cfg = AspectConfig::get_for(&store_config, n.clone()); let cfg = AspectConfig::get_for(&store_config, n.clone());
@ -265,8 +273,11 @@ impl Store {
}).collect(); }).collect();
let store = Store { let store = Store {
location: location, location: location.clone(),
configuration: store_config, configuration: store_config,
store_unload_aspects : Arc::new(Mutex::new(store_unload_aspects)),
pre_create_aspects : Arc::new(Mutex::new(pre_create_aspects)), pre_create_aspects : Arc::new(Mutex::new(pre_create_aspects)),
post_create_aspects : Arc::new(Mutex::new(post_create_aspects)), post_create_aspects : Arc::new(Mutex::new(post_create_aspects)),
pre_retrieve_aspects : Arc::new(Mutex::new(pre_retrieve_aspects)), pre_retrieve_aspects : Arc::new(Mutex::new(pre_retrieve_aspects)),
@ -544,6 +555,8 @@ impl Store {
debug!(" with aspect: {:?}", aspect_name); debug!(" with aspect: {:?}", aspect_name);
let guard = match position { let guard = match position {
HookPosition::StoreUnload => self.store_unload_aspects.clone(),
HookPosition::PreCreate => self.pre_create_aspects.clone(), HookPosition::PreCreate => self.pre_create_aspects.clone(),
HookPosition::PostCreate => self.post_create_aspects.clone(), HookPosition::PostCreate => self.post_create_aspects.clone(),
HookPosition::PreRetrieve => self.pre_retrieve_aspects.clone(), HookPosition::PreRetrieve => self.pre_retrieve_aspects.clone(),
@ -649,6 +662,12 @@ impl Drop for Store {
* TODO: Unlock them * TODO: Unlock them
*/ */
fn drop(&mut self) { fn drop(&mut self) {
let store_id = StoreId::from(self.location.clone());
if let Err(e) = self.execute_hooks_for_id(self.store_unload_aspects.clone(), &store_id) {
debug!("Store-load hooks execution failed. Cannot create store object.");
warn!("Store Unload Hook error: {:?}", e);
}
debug!("Dropping store"); debug!("Dropping store");
} }

View file

@ -43,6 +43,7 @@ impl HookDataAccessorProvider for DebugHook {
use libimagstore::hook::accessor::HookDataAccessor as HDA; use libimagstore::hook::accessor::HookDataAccessor as HDA;
match self.position { match self.position {
HP::StoreUnload |
HP::PreCreate | HP::PreCreate |
HP::PreRetrieve | HP::PreRetrieve |
HP::PreDelete | HP::PreDelete |