[CalendarServer-changes] [4408] CalendarServer/branches/users/sagen/pending-invites-4403

source_changes at macosforge.org source_changes at macosforge.org
Thu Jul 2 11:13:37 PDT 2009


Revision: 4408
          http://trac.macosforge.org/projects/calendarserver/changeset/4408
Author:   sagen at apple.com
Date:     2009-07-02 11:13:36 -0700 (Thu, 02 Jul 2009)
Log Message:
-----------
Task file now contains inbox item paths, which are removed from the file as they are processed

Modified Paths:
--------------
    CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/__init__.py
    CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/task.py
    CalendarServer/branches/users/sagen/pending-invites-4403/twistedcaldav/upgrade.py

Modified: CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/__init__.py
===================================================================
--- CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/__init__.py	2009-07-02 16:51:56 UTC (rev 4407)
+++ CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/__init__.py	2009-07-02 18:13:36 UTC (rev 4408)
@@ -0,0 +1,19 @@
+##
+# Copyright (c) 2005-2009 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+CalendarServer "sidecar" processes
+"""

Modified: CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/task.py
===================================================================
--- CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/task.py	2009-07-02 16:51:56 UTC (rev 4407)
+++ CalendarServer/branches/users/sagen/pending-invites-4403/calendarserver/sidecar/task.py	2009-07-02 18:13:36 UTC (rev 4408)
@@ -103,7 +103,6 @@
 from twistedcaldav.ical import Component
 from twisted.web2.http_headers import Headers
 
-
 class FakeRequest(object):
 
     def __init__(self, rootResource, method):
@@ -146,9 +145,9 @@
         pass
 
 @inlineCallbacks
-def processInbox(rootResource, directory, inboxFile, uuid):
-    print "INSIDE PROCESS INBOX"
-    print rootResource, directory, inboxFile, uuid
+def processInboxItem(rootResource, directory, inboxFile, inboxItemFile, uuid):
+    print "INSIDE PROCESS INBOX ITEM"
+    print rootResource, directory, inboxItemFile, uuid
 
     principals = rootResource.getChild("principals")
     ownerPrincipal = principals.principalForUID(uuid)
@@ -158,65 +157,100 @@
         inboxFile, ownerPrincipal.scheduleInboxURL())
     print "Owner", owner
 
-    for name in inboxFile.listChildren():
-        icsFile = inboxFile.getChild(name)
-        data = icsFile.iCalendarText()
-        calendar = Component.fromString(data)
-        try:
-            method = calendar.propertyValue("METHOD")
-        except ValueError:
-            returnValue(None)
+    data = inboxItemFile.iCalendarText()
+    calendar = Component.fromString(data)
+    try:
+        method = calendar.propertyValue("METHOD")
+    except ValueError:
+        returnValue(None)
 
-        if method == "REPLY":
-            # originator is attendee sending reply
-            originator = calendar.getAttendees()[0]
-        else:
-            # originator is the organizer
-            originator = calendar.getOrganizer()
+    if method == "REPLY":
+        # originator is attendee sending reply
+        originator = calendar.getAttendees()[0]
+    else:
+        # originator is the organizer
+        originator = calendar.getOrganizer()
 
-        originatorPrincipal = principals.principalForCalendarUserAddress(originator)
-        originator = LocalCalendarUser(originator, originatorPrincipal)
-        recipients = (owner,)
-        scheduler = DirectScheduler(FakeRequest(rootResource, "PUT"), icsFile)
-        result = (yield scheduler.doSchedulingViaPUT(originator, recipients,
-            calendar, internal_request=False))
+    originatorPrincipal = principals.principalForCalendarUserAddress(originator)
+    originator = LocalCalendarUser(originator, originatorPrincipal)
+    recipients = (owner,)
+    scheduler = DirectScheduler(FakeRequest(rootResource, "PUT"), inboxItemFile)
+    result = (yield scheduler.doSchedulingViaPUT(originator, recipients,
+        calendar, internal_request=False))
 
-        if os.path.exists(icsFile.fp.path):
-            os.remove(icsFile.fp.path)
+    if os.path.exists(inboxItemFile.fp.path):
+        os.remove(inboxItemFile.fp.path)
 
 class Task(object):
 
-    def __init__(self, service, taskFile):
+    def __init__(self, service, fileName):
         self.service = service
-        self.taskFile = taskFile
+        self.taskName = fileName.split(".")[0]
+        self.taskFile = os.path.join(self.service.processingDir, fileName)
 
     @inlineCallbacks
     def run(self):
-        log.info("Running task %s" % (self.taskFile))
+        methodName = "task_%s" % (self.taskName,)
+        method = getattr(self, methodName, None)
+        if method:
+            try:
+                log.warn("Running task '%s'" % (self.taskName))
+                yield method()
+                log.warn("Completed task '%s'" % (self.taskName))
+            except Exception, e:
+                log.error("Failed task '%s' (%s)" % (self.taskName, e))
+                os.remove(self.taskFile)
+                raise
+        else:
+            log.error("Unknown task requested: '%s'" % (self.taskName))
+            os.remove(self.taskFile)
+            returnValue(None)
 
-        # Hardcoded task: process pending invites in all inboxes
+    @inlineCallbacks
+    def task_scheduleinboxes(self):
+
         calendars = self.service.root.getChild("calendars")
         uidDir = calendars.getChild("__uids__")
 
-        for first in os.listdir(uidDir.fp.path):
-            if len(first) == 2:
-                firstPath = os.path.join(uidDir.fp.path, first)
-                for second in os.listdir(firstPath):
-                    if len(second) == 2:
-                        secondPath = os.path.join(firstPath, second)
-                        for uuid in os.listdir(secondPath):
-                            homeFile = uidDir.getChild(uuid)
-                            inboxFile = homeFile.getChild("inbox")
-                            if inboxFile:
-                                yield processInbox(
-                                    self.service.root,
-                                    self.service.directory,
-                                    inboxFile,
-                                    uuid
-                                )
+        inboxItems = set()
+        with open(self.taskFile) as input:
+            for inboxItem in input:
+                inboxItem = inboxItem.strip()
+                inboxItems.add(inboxItem)
 
-        os.remove(os.path.join(self.service.processingDir, self.taskFile))
+        for inboxItem in list(inboxItems):
+            log.info("Processing inbox item: %s" % (inboxItem,))
+            ignore, uuid, ignore, fileName = inboxItem.rsplit("/", 3)
 
+            homeFile = uidDir.getChild(uuid)
+            if not homeFile:
+                continue
+
+            inboxFile = homeFile.getChild("inbox")
+            if not inboxFile:
+                continue
+
+            inboxItemFile = inboxFile.getChild(fileName)
+
+            yield processInboxItem(
+                self.service.root,
+                self.service.directory,
+                inboxFile,
+                inboxItemFile,
+                uuid
+            )
+            inboxItems.remove(inboxItem)
+
+            # Rewrite the task file in case we exit before we're done
+            with open(self.taskFile + ".tmp", "w") as output:
+                for inboxItem in inboxItems:
+                    output.write("%s\n" % (inboxItem,))
+            os.rename(self.taskFile + ".tmp", self.taskFile)
+
+        os.remove(self.taskFile)
+
+
+
 class CalDAVTaskService(Service):
 
     def __init__(self, root, directory):
@@ -246,18 +280,18 @@
         deferreds = []
 
         try:
-            log.info("PERIODIC, first=%s" % (first,))
-
             if first:
                 # check the processing directory to see if there are any tasks
                 # that didn't complete during the last server run; start those
-                for child in os.listdir(self.processingDir):
-                    deferreds.append(Task(self, child).run())
+                for fileName in os.listdir(self.processingDir):
+                    if fileName.endswith(".task"):
+                        deferreds.append(Task(self, fileName).run())
 
-            for child in os.listdir(self.incomingDir):
-                os.rename(os.path.join(self.incomingDir, child),
-                    os.path.join(self.processingDir, child))
-                deferreds.append(Task(self, child).run())
+            for fileName in os.listdir(self.incomingDir):
+                if fileName.endswith(".task"):
+                    os.rename(os.path.join(self.incomingDir, fileName),
+                        os.path.join(self.processingDir, fileName))
+                    deferreds.append(Task(self, fileName).run())
 
         finally:
             callLater(self.seconds, self.periodic)
@@ -265,6 +299,7 @@
         return DeferredList(deferreds)
 
 
+
 class CalDAVTaskOptions(Options):
     optParameters = [[
         "config", "f", defaultConfigFile, "Path to configuration file."
@@ -372,7 +407,6 @@
 
     def makeService(self, options):
 
-        print "CalDAVTaskServiceMaker -- makeService"
         #
         # Change default log level to "info" as its useful to have
         # that during startup

Modified: CalendarServer/branches/users/sagen/pending-invites-4403/twistedcaldav/upgrade.py
===================================================================
--- CalendarServer/branches/users/sagen/pending-invites-4403/twistedcaldav/upgrade.py	2009-07-02 16:51:56 UTC (rev 4407)
+++ CalendarServer/branches/users/sagen/pending-invites-4403/twistedcaldav/upgrade.py	2009-07-02 18:13:36 UTC (rev 4408)
@@ -298,15 +298,8 @@
             os.mkdir(incomingDir)
         os.chown(incomingDir, uid, gid)
 
-        processingDir = os.path.join(taskDir, "processing")
-        if not os.path.exists(processingDir):
-            os.mkdir(processingDir)
-        os.chown(processingDir, uid, gid)
+        return incomingDir
 
-        # cause inboxes to get processed
-        jobFile = os.path.join(incomingDir, "processinboxes")
-        with open(jobFile, "w") as out:
-            out.write("foo")
 
 
     directory = getDirectory()
@@ -376,8 +369,10 @@
                     os.rmdir(dirPath)
 
 
-            # Count how many calendar homes we'll be processing
+            # Count how many calendar homes we'll be processing, and build
+            # list of pending inbox items
             total = 0
+            inboxItems = set()
             for first in os.listdir(uidHomes):
                 if len(first) == 2:
                     firstPath = os.path.join(uidHomes, first)
@@ -386,7 +381,21 @@
                             secondPath = os.path.join(firstPath, second)
                             for home in os.listdir(secondPath):
                                 total += 1
+                                homePath = os.path.join(secondPath, home)
+                                inboxPath = os.path.join(homePath, "inbox")
+                                if os.path.exists(inboxPath):
+                                    for inboxItem in os.listdir(inboxPath):
+                                        if not inboxItem.startswith("."):
+                                            inboxItems.add(os.path.join(inboxPath, inboxItem))
 
+            incomingDir = createTaskServiceDirectory(config, uid, gid)
+            if inboxItems:
+                taskFile = os.path.join(incomingDir, "scheduleinboxes.task")
+                with open(taskFile, "w") as out:
+                    for item in inboxItems:
+                        out.write("%s\n" % (item))
+                os.chown(taskFile, uid, gid)
+
             if total:
                 log.warn("Processing %d calendar homes in %s" % (total, uidHomes))
 
@@ -415,7 +424,6 @@
 
     migrateResourceInfo(config, directory, uid, gid)
     createMailTokensDatabase(config, uid, gid)
-    createTaskServiceDirectory(config, uid, gid)
 
     if errorOccurred:
         raise UpgradeError("Data upgrade failed, see error.log for details")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20090702/f520e254/attachment-0001.html>


More information about the calendarserver-changes mailing list