more robust handling of looping task configuration in ldap source
* use minimum times
* updated documentation and help strings
* fix the minutes vs. seconds confusion
--- a/server/sources/__init__.py Sat May 29 10:06:07 2010 +0000
+++ b/server/sources/__init__.py Wed Jun 02 15:55:58 2010 +0000
@@ -54,7 +54,9 @@
class TimedCache(dict):
def __init__(self, ttlm, ttls=0):
# time to live in minutes
- self.ttl = timedelta(0, ttlm*60 + ttls, 0)
+ self.ttl = timedelta(seconds=ttlm*60 + ttls)
+ if self.ttl.seconds <= 0:
+ raise ValueError('TimedCache initialized with a ttl of %ss' % self.ttl.seconds)
def __setitem__(self, key, value):
dict.__setitem__(self, key, (datetime.now(), value))
--- a/server/sources/ldapuser.py Sat May 29 10:06:07 2010 +0000
+++ b/server/sources/ldapuser.py Wed Jun 02 15:55:58 2010 +0000
@@ -33,7 +33,7 @@
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE.
"""
-
+from __future__ import division
from base64 import b64decode
from logilab.common.textutils import splitstrip
@@ -158,7 +158,7 @@
('cache-life-time',
{'type' : 'time',
'default': '2h',
- 'help': 'life time of query cache in minutes (default to two hours).',
+ 'help': 'life time of query cache (default to two hours).',
'group': 'ldap-source', 'level': 3,
}),
@@ -187,9 +187,11 @@
for o in self.user_classes]
self._conn = None
self._cache = {}
+ # ttlm is in minutes!
ttlm = time_validator(None, None,
- source_config.get('cache-life-time', 2*60))
- self._query_cache = TimedCache(ttlm)
+ source_config.get('cache-life-time', 2*60*60)) // 60
+ self._query_cache = TimedCache(max(ttlm, 1))
+ # interval is in seconds !
self._interval = time_validator(None, None,
source_config.get('synchronization-interval',
24*60*60))
@@ -197,13 +199,15 @@
def reset_caches(self):
"""method called during test to reset potential source caches"""
self._cache = {}
- self._query_cache = TimedCache(2*60)
+ self._query_cache = TimedCache(2*60) # TimedCache is in minutes!
def init(self):
"""method called by the repository once ready to handle request"""
self.info('ldap init')
- self.repo.looping_task(self._interval, self.synchronize)
- self.repo.looping_task(self._query_cache.ttl.seconds/10,
+ # set minimum period of 5min 1s (the additional second is to minimize
+ # resonnance effet)
+ self.repo.looping_task(max(301, self._interval), self.synchronize)
+ self.repo.looping_task(max(7, self._query_cache.ttl.seconds // 10),
self._query_cache.clear_expired)
def synchronize(self):