server/sources/ldapuser.py
branchstable
changeset 5637 b72a838aa109
parent 5604 60a92bf32a18
child 5642 6a90357b9769
equal deleted inserted replaced
5605:2604545d7dd9 5637:b72a838aa109
    31 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
    31 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
    32 WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    32 WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    33 WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    33 WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    34 FOR A PARTICULAR PURPOSE.
    34 FOR A PARTICULAR PURPOSE.
    35 """
    35 """
    36 
    36 from __future__ import division
    37 from base64 import b64decode
    37 from base64 import b64decode
    38 
    38 
    39 from logilab.common.textutils import splitstrip
    39 from logilab.common.textutils import splitstrip
    40 from rql.nodes import Relation, VariableRef, Constant, Function
    40 from rql.nodes import Relation, VariableRef, Constant, Function
    41 
    41 
   156           'group': 'ldap-source', 'level': 3,
   156           'group': 'ldap-source', 'level': 3,
   157           }),
   157           }),
   158         ('cache-life-time',
   158         ('cache-life-time',
   159          {'type' : 'time',
   159          {'type' : 'time',
   160           'default': '2h',
   160           'default': '2h',
   161           'help': 'life time of query cache in minutes (default to two hours).',
   161           'help': 'life time of query cache (default to two hours).',
   162           'group': 'ldap-source', 'level': 3,
   162           'group': 'ldap-source', 'level': 3,
   163           }),
   163           }),
   164 
   164 
   165     )
   165     )
   166 
   166 
   185             self.user_rev_attrs[cwattr] = ldapattr
   185             self.user_rev_attrs[cwattr] = ldapattr
   186         self.base_filters = [filter_format('(%s=%s)', ('objectClass', o))
   186         self.base_filters = [filter_format('(%s=%s)', ('objectClass', o))
   187                               for o in self.user_classes]
   187                               for o in self.user_classes]
   188         self._conn = None
   188         self._conn = None
   189         self._cache = {}
   189         self._cache = {}
       
   190         # ttlm is in minutes!
   190         ttlm = time_validator(None, None,
   191         ttlm = time_validator(None, None,
   191                               source_config.get('cache-life-time', 2*60))
   192                               source_config.get('cache-life-time', 2*60*60)) // 60
   192         self._query_cache = TimedCache(ttlm)
   193         self._query_cache = TimedCache(max(ttlm, 1))
       
   194         # interval is in seconds !
   193         self._interval = time_validator(None, None,
   195         self._interval = time_validator(None, None,
   194                                         source_config.get('synchronization-interval',
   196                                         source_config.get('synchronization-interval',
   195                                                24*60*60))
   197                                                24*60*60))
   196 
   198 
   197     def reset_caches(self):
   199     def reset_caches(self):
   198         """method called during test to reset potential source caches"""
   200         """method called during test to reset potential source caches"""
   199         self._cache = {}
   201         self._cache = {}
   200         self._query_cache = TimedCache(2*60)
   202         self._query_cache = TimedCache(2*60) # TimedCache is in minutes!
   201 
   203 
   202     def init(self):
   204     def init(self):
   203         """method called by the repository once ready to handle request"""
   205         """method called by the repository once ready to handle request"""
   204         self.info('ldap init')
   206         self.info('ldap init')
   205         self.repo.looping_task(self._interval, self.synchronize)
   207         # set minimum period of 5min 1s (the additional second is to minimize
   206         self.repo.looping_task(self._query_cache.ttl.seconds/10,
   208         # resonnance effet)
       
   209         self.repo.looping_task(max(301, self._interval), self.synchronize)
       
   210         self.repo.looping_task(max(7, self._query_cache.ttl.seconds // 10),
   207                                self._query_cache.clear_expired)
   211                                self._query_cache.clear_expired)
   208 
   212 
   209     def synchronize(self):
   213     def synchronize(self):
   210         """synchronize content known by this repository with content in the
   214         """synchronize content known by this repository with content in the
   211         external repository
   215         external repository