[CalendarServer-changes] [1033] CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py

source_changes at macosforge.org source_changes at macosforge.org
Thu Jan 11 17:42:56 PST 2007


Revision: 1033
          http://trac.macosforge.org/projects/calendarserver/changeset/1033
Author:   wsanchez at apple.com
Date:     2007-01-11 17:42:56 -0800 (Thu, 11 Jan 2007)

Log Message:
-----------
Load OD records at launch time.

Load OD records immediately if they are missing (shouldn't happen, due to the above).

Load OD records after a timeout in the background.
(Stale records are vended in the meantime.)

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py

Modified: CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py	2007-01-12 01:36:23 UTC (rev 1032)
+++ CalendarServer/trunk/twistedcaldav/directory/appleopendirectory.py	2007-01-12 01:42:56 UTC (rev 1033)
@@ -31,7 +31,8 @@
 import dsattributes
 
 from twisted.python import log
-from twisted.internet import reactor
+from twisted.internet.threads import deferToThread
+from twisted.internet.reactor import callLater
 from twisted.cred.credentials import UsernamePassword
 
 from twistedcaldav.directory.directory import DirectoryService, DirectoryRecord
@@ -61,6 +62,9 @@
         self.node = node
         self._records = {}
 
+        for recordType in self.recordTypes():
+            self.recordsForType(recordType)
+
     def __cmp__(self, other):
         if not isinstance(other, DirectoryRecord):
             return super(DirectoryRecord, self).__eq__(other)
@@ -92,7 +96,7 @@
         type.  Keys are short names and values are the cooresponding
         OpenDirectoryRecord for the given record type.
         """
-        if recordType not in self._records:
+        def reloadCache():
             log.msg("Reloading %s record cache" % (recordType,))
 
             attrs = [
@@ -157,14 +161,35 @@
                     memberGUIDs           = memberGUIDs,
                 )
 
-            self._records[recordType] = records
-            def flush():
-                log.msg("Flushing %s record cache" % (recordType,))
-                del self._records[recordType]
-            reactor.callLater(recordListCacheTimeout, flush)
+            storage = {
+                "status": "new",
+                "records": records,
+            }
 
-        return self._records[recordType]
+            def rot():
+                storage["status"] = "stale"
+            callLater(recordListCacheTimeout, rot)
 
+            self._records[recordType] = storage
+
+        try:
+            storage = self._records[recordType]
+        except KeyError:
+            reloadCache()
+        else:
+            if storage["status"] == "stale":
+                storage["status"] = "loading"
+
+                def onError(f):
+                    storage["status"] = "stale" # Keep trying
+                    log.err("Unable to load records of type %s from OpenDirectory due to unexpected error: %s"
+                            % (recordType, f))
+
+                d = deferToThread(reloadCache)
+                d.addErrback(onError)
+
+        return self._records[recordType]["records"]
+
     def listRecords(self, recordType):
         return self.recordsForType(recordType).itervalues()
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20070111/660647e9/attachment.html


More information about the calendarserver-changes mailing list