From a5ee031e5628fc78f636ecd6b5dccb8895b99fad Mon Sep 17 00:00:00 2001 From: Sinisa Veseli <sveseli@aps.anl.gov> Date: Tue, 14 Apr 2015 13:15:28 +0000 Subject: [PATCH] added generic tracker class that uses object cache; added experiment tracker to daq web service --- src/python/dm/common/utility/objectCache.py | 3 +- src/python/dm/common/utility/objectTracker.py | 52 +++++++++++++++++++ .../service/impl/experimentTracker.py | 21 ++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100755 src/python/dm/common/utility/objectTracker.py create mode 100755 src/python/dm/daq_web_service/service/impl/experimentTracker.py diff --git a/src/python/dm/common/utility/objectCache.py b/src/python/dm/common/utility/objectCache.py index 529717b0..7a519510 100755 --- a/src/python/dm/common/utility/objectCache.py +++ b/src/python/dm/common/utility/objectCache.py @@ -16,13 +16,14 @@ class ObjectCache: # Cache info expiration time. DEFAULT_OBJECT_LIFETIME = 60 # seconds - def __init__(self, cacheSize, objectLifetime=DEFAULT_OBJECT_LIFETIME): + def __init__(self, cacheSize, objectLifetime=DEFAULT_OBJECT_LIFETIME, objectClass=None): self.lock = threading.RLock() self.objectMap = {} # id/object map self.timeStampDeq = deque() # timeStamp deq self.cacheSize = cacheSize self.objectLifetime = objectLifetime self.deqSize = ObjectCache.DEFAULT_TIME_STAMP_DEQ_SIZE_FACTOR*cacheSize + self.objectClass = objectClass def setCacheSize(self, cacheSize): self.cacheSize = cacheSize diff --git a/src/python/dm/common/utility/objectTracker.py b/src/python/dm/common/utility/objectTracker.py new file mode 100755 index 00000000..66fd49cc --- /dev/null +++ b/src/python/dm/common/utility/objectTracker.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import threading +import time + +from dm.common.utility.loggingManager import LoggingManager +from dm.common.utility.configurationManager import ConfigurationManager +from dm.common.utility.objectCache import ObjectCache +from dm.common.utility.singleton import Singleton + +class ObjectTracker(Singleton): + + DEFAULT_CACHE_SIZE = 1000 + DEFAULT_OBJECT_LIFETIME_IN_SECONDS = 86400*30 # 30 days + + # Singleton. + __instanceLock = threading.RLock() + + # Cache configuration + objectClass = None + objectLifetime = DEFAULT_OBJECT_LIFETIME_IN_SECONDS + cacheSize = DEFAULT_CACHE_SIZE + + def __init__(self, *args, **kwargs): + ObjectTracker.__instanceLock.acquire() + try: + self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) + self.logger.debug('Initializing') + self.objectCache = ObjectCache(self.cacheSize, self.objectLifetime, self.objectClass) + self.logger.debug('Initialization complete') + finally: + ObjectTracker.__instanceLock.release() + + def put(self, id, item, objectLifetime=None): + return self.objectCache.put(id, item, objectLifetime) + + def get(self, id): + return self.objectCache.get(id) + + def remove(self, id): + return self.objectCache.remove(id) + +#################################################################### +# Testing + +if __name__ == '__main__': + ot = ObjectTracker.getInstance() + print ot + ot2 = ObjectTracker() + print ot2 + + diff --git a/src/python/dm/daq_web_service/service/impl/experimentTracker.py b/src/python/dm/daq_web_service/service/impl/experimentTracker.py new file mode 100755 index 00000000..df84a8f9 --- /dev/null +++ b/src/python/dm/daq_web_service/service/impl/experimentTracker.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +from dm.common.objects.experiment import Experiment +from dm.common.utility.objectTracker import ObjectTracker + +class ExperimentTracker(ObjectTracker): + + # Cache configuration + objectClass = Experiment + +#################################################################### +# Testing + +if __name__ == '__main__': + et = ExperimentTracker.getInstance() + print et + et2 = ExperimentTracker.getInstance() + print et2 + + + -- GitLab