diff --git a/include/MarketPlaceAppPool.h b/include/MarketPlaceAppPool.h index bb39389057..d66f674a09 100644 --- a/include/MarketPlaceAppPool.h +++ b/include/MarketPlaceAppPool.h @@ -73,13 +73,14 @@ public: * @param template to generate app with the from_template64 function * @param mp_id of the MarketPlace to store de App * @param mp_name of the MarketPlace + * @param app_id of the imported app * @param error_str Returns the error reason, if any * * @return the oid assigned to the object, -1 in case of failure, -2 * already imported */ int import(const std::string& t64, int mp_id, const std::string& mp_name, - std::string& error_str); + int& app_id, std::string& error_str); /** * Function to get a MarketPlaceApp from the pool @@ -145,6 +146,31 @@ public: { return new MarketPlaceApp(-1,-1,"","", 0, 0); }; + + /** + * Check an element into map + * @param map_id of the app + * @return true if the app has to be deleted + */ + bool test_map_check(int map_id); + + /** + * Resets the counter of missing monitors of an app + * @param app_id of the app + */ + void reset_map_check(int app_id); + +private: + + /** + * Hash to store the number of times an app was missing from monitor data + */ + map map_check; + + /** + * Max number of monitor that an app may be missing before deleting it + */ + static const int MAX_MISSING_MONITORS; }; #endif /*MARKETPLACE_POOL_H_*/ diff --git a/src/market/MarketPlaceAppPool.cc b/src/market/MarketPlaceAppPool.cc index 9a40db1183..e80944b4ca 100644 --- a/src/market/MarketPlaceAppPool.cc +++ b/src/market/MarketPlaceAppPool.cc @@ -190,7 +190,7 @@ int MarketPlaceAppPool::drop(PoolObjectSQL * objsql, std::string& error_msg) /* -------------------------------------------------------------------------- */ int MarketPlaceAppPool::import(const std::string& t64, int mp_id, - const std::string& mp_name, std::string& error_str) + const std::string& mp_name, int& app_id, std::string& error_str) { // ------------------------------------------------------------------------- // Build the marketplace app object @@ -229,6 +229,8 @@ int MarketPlaceAppPool::import(const std::string& t64, int mp_id, if( mp_aux != 0 ) //Marketplace app already imported { + app_id = mp_aux->oid; + if ( mp_aux->version != app->version || mp_aux->md5 != app->md5 ) { mp_aux->from_template64(t64, error_str); @@ -248,15 +250,17 @@ int MarketPlaceAppPool::import(const std::string& t64, int mp_id, // ------------------------------------------------------------------------- if (Nebula::instance().is_federation_slave()) { - int oid = master_allocate(app, error_str); + app_id = master_allocate(app, error_str); app->lock(); delete app; - return oid; + return app_id; } - return PoolSQL::allocate(app, error_str); + app_id = PoolSQL::allocate(app, error_str); + + return app_id; } /* -------------------------------------------------------------------------- */ @@ -308,4 +312,39 @@ int MarketPlaceAppPool::update(PoolObjectSQL * objsql) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +const int MarketPlaceAppPool::MAX_MISSING_MONITORS = 3; +bool MarketPlaceAppPool::test_map_check(int app_id) +{ + map::iterator it = map_check.find(app_id); + + if ( it == map_check.end() ) + { + return false; + } + + it->second++; + + bool to_delete = it->second >= MAX_MISSING_MONITORS; + + if ( to_delete ) + { + map_check.erase(it); + } + + return to_delete; +} + +void MarketPlaceAppPool::reset_map_check(int app_id) +{ + map::iterator it = map_check.find(app_id); + + if ( it == map_check.end() ) + { + map_check.insert(make_pair(app_id, -1)); + } + else + { + it->second = -1; + } +} diff --git a/src/market/MarketPlaceManagerDriver.cc b/src/market/MarketPlaceManagerDriver.cc index fece9c6844..695391389d 100644 --- a/src/market/MarketPlaceManagerDriver.cc +++ b/src/market/MarketPlaceManagerDriver.cc @@ -120,7 +120,6 @@ static void monitor_action( return; } - std::string name; MarketPlace * market = marketpool->get(id, true); if (market == 0 ) @@ -128,7 +127,8 @@ static void monitor_action( return; } - name = market->get_name(); + set apps_mp = market->get_marketapp_ids(); + std::string name = market->get_name(); market->update_monitor(monitor_data); @@ -143,7 +143,8 @@ static void monitor_action( for (int i=0; i< num ; i++) { - int rc = apppool->import(apps[i]->value(), id, name, err); + int app_id; + int rc = apppool->import(apps[i]->value(), id, name, app_id, err); if ( rc == -1 ) { @@ -162,6 +163,37 @@ static void monitor_action( market->unlock(); } } + + apppool->reset_map_check(app_id); + + apps_mp.erase(app_id); + } + + for (set::iterator i = apps_mp.begin(); i != apps_mp.end(); ++i) + { + if (apppool->test_map_check(*i)) //delete app + { + std::string error; + + MarketPlaceApp * app = apppool->get(*i, true); + + if ( app == 0 ) + { + continue; + } + + rc = apppool->drop(app, error); + + app->unlock(); + + market = marketpool->get(id, true); + + market->del_marketapp(*i); + + marketpool->update(market); + + market->unlock(); + } } oss << "Marketplace " << name << " (" << id << ") successfully monitored.";