Skip to content

Commit

Permalink
Initial solution to #210
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Feb 1, 2017
1 parent e0eb48d commit 6c80435
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions IM/InfrastructureList.py
Expand Up @@ -18,6 +18,7 @@
import sys
import logging
import threading
from datetime import datetime, timedelta

from IM.db import DataBase
from IM.config import Config
Expand All @@ -37,6 +38,9 @@ class InfrastructureList():

infrastructure_list = {}
"""Map from string to :py:class:`InfrastructureInfo`."""

infrastructure_last_access = {}
"""Map from string to :py:class:`DateTime`."""

logger = logging.getLogger('InfrastructureManager')
"""Logger object."""
Expand All @@ -50,6 +54,7 @@ def add_infrastructure(inf):

with InfrastructureList._lock:
InfrastructureList.infrastructure_list[inf.id] = inf
InfrastructureList.infrastructure_last_access[inf.id] = datetime.now()

@staticmethod
def remove_inf(del_inf):
Expand All @@ -63,10 +68,29 @@ def get_inf_ids():
""" Get the IDs of the Infrastructures """
return InfrastructureList._get_inf_ids_from_db()

@staticmethod
def clean_inf_memory_data():
"""
Clean old memory data to assure HA works correctly
"""
now = datetime.now()
with InfrastructureList._lock:
inf_list = {}
delay = timedelta(seconds=Config.INF_CACHE_TIME)
for inf_id, inf in InfrastructureList.infrastructure_list.items():
if now - InfrastructureList.infrastructure_last_access[inf_id] > delay:
InfrastructureList.save_data(inf_id)
InfrastructureList.logger.debug("Infrastructure %s data expired. Remove from memory." % inf_id)
else:
inf_list[inf_id] = inf
InfrastructureList.infrastructure_list = inf_list

@staticmethod
def get_infrastructure(inf_id):
""" Get the infrastructure object """
InfrastructureList.clean_inf_memory_data()
if inf_id in InfrastructureList.infrastructure_list:
InfrastructureList.infrastructure_last_access[inf_id] = datetime.now()
return InfrastructureList.infrastructure_list[inf_id]
elif inf_id in InfrastructureList.get_inf_ids():
# Load the data from DB:
Expand All @@ -92,6 +116,8 @@ def load_data():
try:
inf_list = InfrastructureList._get_data_from_db(Config.DATA_DB)
InfrastructureList.infrastructure_list = inf_list
for inf_id in inf_list.keys():
InfrastructureList.infrastructure_last_access[inf_id] = datetime.now()
except Exception, ex:
InfrastructureList.logger.exception("ERROR loading data. Correct or delete it!!")
sys.stderr.write("ERROR loading data: " + str(ex) + ".\nCorrect or delete it!! ")
Expand All @@ -108,6 +134,11 @@ def save_data(inf_id=None):
"""
with InfrastructureList._lock:
try:
if inf_id:
InfrastructureList.infrastructure_last_access[inf_id] = datetime.now()
else:
for inf_id in InfrastructureList.infrastructure_list.keys():
InfrastructureList.infrastructure_last_access[inf_id] = datetime.now()
res = InfrastructureList._save_data_to_db(Config.DATA_DB,
InfrastructureList.infrastructure_list,
inf_id)
Expand Down
1 change: 1 addition & 0 deletions IM/config.py
Expand Up @@ -90,6 +90,7 @@ class Config:
CONFMAMAGER_CHECK_STATE_INTERVAL = 5
UPDATE_CTXT_LOG_INTERVAL = 20
ANSIBLE_INSTALL_TIMEOUT = 900
INF_CACHE_TIME = 900

config = ConfigParser.ConfigParser()
config.read([Config.IM_PATH + '/../im.cfg', Config.IM_PATH +
Expand Down

0 comments on commit 6c80435

Please sign in to comment.