<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[13516] CalendarServer/trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/13516">13516</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2014-05-20 11:49:22 -0700 (Tue, 20 May 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add singleton and regenerating WorkItem types.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertapcaldavpy">CalendarServer/trunk/calendarserver/tap/caldav.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolsprincipalspy">CalendarServer/trunk/calendarserver/tools/principals.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolspurgepy">CalendarServer/trunk/calendarserver/tools/purge.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolsutilpy">CalendarServer/trunk/calendarserver/tools/util.py</a></li>
<li><a href="#CalendarServertrunkrequirementsstabletxt">CalendarServer/trunk/requirements-stable.txt</a></li>
<li><a href="#CalendarServertrunktxdavcaldavdatastoreschedulingimipinboundpy">CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/inbound.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresqlpy">CalendarServer/trunk/txdav/common/datastore/sql.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoreworkinbox_cleanuppy">CalendarServer/trunk/txdav/common/datastore/work/inbox_cleanup.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoreworkrevision_cleanuppy">CalendarServer/trunk/txdav/common/datastore/work/revision_cleanup.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoreworktesttest_inbox_cleanuppy">CalendarServer/trunk/txdav/common/datastore/work/test/test_inbox_cleanup.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoreworktesttest_revision_cleanuppy">CalendarServer/trunk/txdav/common/datastore/work/test/test_revision_cleanup.py</a></li>
<li><a href="#CalendarServertrunktxdavwhogroupspy">CalendarServer/trunk/txdav/who/groups.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertapcaldavpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tap/caldav.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tap/caldav.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/calendarserver/tap/caldav.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -69,7 +69,7 @@
</span><span class="cx"> from twext.enterprise.jobqueue import PeerConnectionPool
</span><span class="cx"> from twext.enterprise.jobqueue import WorkerFactory as QueueWorkerFactory
</span><span class="cx"> from twext.application.service import ReExecService
</span><del>-from txdav.who.groups import scheduleNextGroupCachingUpdate
</del><ins>+from txdav.who.groups import GroupCacherPollingWork
</ins><span class="cx"> 
</span><span class="cx"> from txweb2.channel.http import (
</span><span class="cx">     LimitingHTTPFactory, SSLRedirectRequest, HTTPChannel
</span><span class="lines">@@ -89,10 +89,8 @@
</span><span class="cx">     UpgradeAcquireLockStep, UpgradeReleaseLockStep,
</span><span class="cx">     UpgradeDatabaseNotificationDataStep
</span><span class="cx"> )
</span><del>-from txdav.common.datastore.work.inbox_cleanup import scheduleFirstInboxCleanup
-from txdav.common.datastore.work.revision_cleanup import (
-    scheduleFirstFindMinRevision
-)
</del><ins>+from txdav.common.datastore.work.inbox_cleanup import InboxCleanupWork
+from txdav.common.datastore.work.revision_cleanup import FindMinValidRevisionWork
</ins><span class="cx"> from txdav.who.util import directoryFromConfig
</span><span class="cx"> from txdav.dps.client import DirectoryService as DirectoryProxyClientService
</span><span class="cx"> from txdav.who.groups import GroupCacher
</span><span class="lines">@@ -593,22 +591,22 @@
</span><span class="cx">                 int(config.LogID) if config.LogID else 5
</span><span class="cx">             )
</span><span class="cx">         if self.doGroupCaching:
</span><del>-            yield scheduleNextGroupCachingUpdate(
</del><ins>+            yield GroupCacherPollingWork.initialSchedule(
</ins><span class="cx">                 self.store,
</span><span class="cx">                 int(config.LogID) if config.LogID else 5
</span><span class="cx">             )
</span><del>-        yield scheduleFirstFindMinRevision(
</del><ins>+        yield FindMinValidRevisionWork.initialSchedule(
</ins><span class="cx">             self.store,
</span><span class="cx">             int(config.LogID) if config.LogID else 5
</span><span class="cx">         )
</span><del>-        yield scheduleFirstInboxCleanup(
</del><ins>+        yield InboxCleanupWork.initialSchedule(
</ins><span class="cx">             self.store,
</span><span class="cx">             int(config.LogID) if config.LogID else 5
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         # FIXME: uncomment this when purge is working
</span><span class="cx">         # from calendarserver.tools.purge import scheduleNextPrincipalPurgeUpdate
</span><del>-        # yield scheduleNextPrincipalPurgeUpdate(
</del><ins>+        # yield PrincipalPurgePollingWork.initialSchedule(
</ins><span class="cx">         #     self.store,
</span><span class="cx">         #     int(config.LogID) if config.LogID else 5
</span><span class="cx">         # )
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolsprincipalspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/principals.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/principals.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/calendarserver/tools/principals.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -595,12 +595,8 @@
</span><span class="cx">         )
</span><span class="cx">         yield proxyGroup.setMembers(proxyRecords)
</span><span class="cx"> 
</span><del>-    # if store is not None:
-    #     # Schedule work the PeerConnectionPool will pick up as overdue
-    #     yield schedulePolledGroupCachingUpdate(store)
</del><span class="cx"> 
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> @inlineCallbacks
</span><span class="cx"> def getProxies(record):
</span><span class="cx">     &quot;&quot;&quot;
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolspurgepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/purge.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/purge.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/calendarserver/tools/purge.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -30,10 +30,10 @@
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><span class="cx"> from twext.enterprise.dal.syntax import Delete, Select, Union
</span><del>-from twext.enterprise.jobqueue import WorkItem
</del><ins>+from twext.enterprise.jobqueue import WorkItem, RegeneratingWorkItem
</ins><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> 
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue
</del><ins>+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</ins><span class="cx"> 
</span><span class="cx"> from twistedcaldav import caldavxml
</span><span class="cx"> from twistedcaldav.config import config
</span><span class="lines">@@ -50,7 +50,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> class PrincipalPurgePollingWork(
</span><del>-    WorkItem,
</del><ins>+    RegeneratingWorkItem,
</ins><span class="cx">     fromTable(schema.PRINCIPAL_PURGE_POLLING_WORK)
</span><span class="cx"> ):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="lines">@@ -61,32 +61,31 @@
</span><span class="cx"> 
</span><span class="cx">     group = &quot;principal_purge_polling&quot;
</span><span class="cx"> 
</span><ins>+    @classmethod
+    def initialSchedule(cls, store, seconds):
+        def _enqueue(txn):
+            return PrincipalPurgePollingWork.reschedule(txn, seconds)
+
+        if config.AutomaticPurging.Enabled:
+            return store.inTransaction(&quot;PrincipalPurgePollingWork.initialSchedule&quot;, _enqueue)
+        else:
+            return succeed(None)
+
+
+    def regenerateInterval(self):
+        &quot;&quot;&quot;
+        Return the interval in seconds between regenerating instances.
+        &quot;&quot;&quot;
+        return config.AutomaticPurging.PollingIntervalSeconds if config.AutomaticPurging.Enabled else None
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def doWork(self):
</span><span class="cx"> 
</span><del>-        # Delete all other work items
-        yield Delete(From=self.table, Where=None).on(self.transaction)
-
</del><span class="cx">         # If not enabled, punt here
</span><span class="cx">         if not config.AutomaticPurging.Enabled:
</span><span class="cx">             returnValue(None)
</span><span class="cx"> 
</span><del>-        # Schedule next update, 7 days out (default)
-        # Special - for testing it is handy to have this work item not regenerate, so
-        # we use an interval of -1 to signify a one-shot operation
-        if config.AutomaticPurging.PollingIntervalSeconds != -1:
-            notBefore = (
-                datetime.datetime.utcnow() +
-                datetime.timedelta(seconds=config.AutomaticPurging.PollingIntervalSeconds)
-            )
-            log.info(
-                &quot;Scheduling next principal purge scan update: {when}&quot;, when=notBefore
-            )
-            yield self.transaction.enqueue(
-                PrincipalPurgePollingWork,
-                notBefore=notBefore
-            )
-
</del><span class="cx">         # Do the scan
</span><span class="cx">         allUIDs = set()
</span><span class="cx">         for home in (schema.CALENDAR_HOME, schema.ADDRESSBOOK_HOME):
</span><span class="lines">@@ -276,26 +275,6 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-@inlineCallbacks
-def scheduleNextPrincipalPurgeUpdate(store, seconds):
-
-    notBefore = (
-        datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-    )
-
-    log.debug(
-        &quot;Scheduling next principal purge update: {when}&quot;, when=notBefore
-    )
-
-    def _enqueue(txn):
-        return txn.enqueue(PrincipalPurgePollingWork, notBefore=notBefore)
-
-    wp = yield store.inTransaction(&quot;scheduleNextPrincipalPurgeUpdate&quot;, _enqueue)
-
-    returnValue(wp)
-
-
-
</del><span class="cx"> class PurgeOldEventsService(WorkerService):
</span><span class="cx"> 
</span><span class="cx">     cutoff = None
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolsutilpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/util.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/util.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/calendarserver/tools/util.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -42,7 +42,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> from twistedcaldav import memcachepool
</span><del>-from txdav.who.groups import schedulePolledGroupCachingUpdate
</del><ins>+from txdav.who.groups import GroupCacherPollingWork
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> log = Logger()
</span><span class="lines">@@ -511,7 +511,9 @@
</span><span class="cx">         principal, proxyPrincipal, proxyTypes=proxyTypes))
</span><span class="cx"> 
</span><span class="cx">     # Schedule work the PeerConnectionPool will pick up as overdue
</span><del>-    yield schedulePolledGroupCachingUpdate(store)
</del><ins>+    def groupPollNow(txn):
+        return GroupCacherPollingWork.reschedule(txn, 0, force=True)
+    yield store.inTransaction(groupPollNow)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -545,7 +547,9 @@
</span><span class="cx"> 
</span><span class="cx">     if removed:
</span><span class="cx">         # Schedule work the PeerConnectionPool will pick up as overdue
</span><del>-        yield schedulePolledGroupCachingUpdate(store)
</del><ins>+        def groupPollNow(txn):
+            return GroupCacherPollingWork.reschedule(txn, 0, force=True)
+        yield store.inTransaction(groupPollNow)
</ins><span class="cx">     returnValue(removed)
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunkrequirementsstabletxt"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/requirements-stable.txt (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/requirements-stable.txt        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/requirements-stable.txt        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -5,7 +5,7 @@
</span><span class="cx"> # For CalendarServer development, don't try to get these projects from PyPI; use svn.
</span><span class="cx"> 
</span><span class="cx"> -e .
</span><del>--e svn+http://svn.calendarserver.org/repository/calendarserver/twext/trunk@13494#egg=twextpy
</del><ins>+-e svn+http://svn.calendarserver.org/repository/calendarserver/twext/trunk@13515#egg=twextpy
</ins><span class="cx"> -e svn+http://svn.calendarserver.org/repository/calendarserver/PyKerberos/trunk@13420#egg=kerberos
</span><span class="cx"> -e svn+http://svn.calendarserver.org/repository/calendarserver/PyCalendar/trunk@13420#egg=pycalendar
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavcaldavdatastoreschedulingimipinboundpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/inbound.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/inbound.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/inbound.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -19,8 +19,7 @@
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><del>-from twext.enterprise.dal.syntax import Delete
-from twext.enterprise.jobqueue import WorkItem
</del><ins>+from twext.enterprise.jobqueue import WorkItem, RegeneratingWorkItem
</ins><span class="cx"> from twext.internet.gaiendpoint import GAIEndpoint
</span><span class="cx"> from twext.python.log import Logger, LegacyLogger
</span><span class="cx"> 
</span><span class="lines">@@ -81,25 +80,25 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class IMIPPollingWork(WorkItem, fromTable(schema.IMIP_POLLING_WORK)):
</del><ins>+class IMIPPollingWork(RegeneratingWorkItem, fromTable(schema.IMIP_POLLING_WORK)):
</ins><span class="cx"> 
</span><span class="cx">     # FIXME: purge all old tokens here
</span><span class="cx">     group = &quot;imip_polling&quot;
</span><span class="cx"> 
</span><ins>+    def regenerateInterval(self):
+        &quot;&quot;&quot;
+        Return the interval in seconds between regenerating instances.
+        &quot;&quot;&quot;
+        mailRetriever = self.transaction._mailRetriever
+        return mailRetriever.settings[&quot;seconds&quot;]
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def doWork(self):
</span><span class="cx"> 
</span><del>-        # Delete all other work items
-        yield Delete(From=self.table, Where=None).on(self.transaction)
-
</del><span class="cx">         mailRetriever = self.transaction._mailRetriever
</span><span class="cx">         if mailRetriever is not None:
</span><del>-            try:
-                yield mailRetriever.fetchMail()
-            except Exception, e:
-                log.error(&quot;Failed to fetch mail (%s)&quot; % (e,))
-            finally:
-                yield mailRetriever.scheduleNextPoll()
</del><ins>+            yield mailRetriever.fetchMail()
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -140,7 +139,7 @@
</span><span class="cx">     def scheduleNextPoll(self, seconds=None):
</span><span class="cx">         if seconds is None:
</span><span class="cx">             seconds = self.settings[&quot;PollingSeconds&quot;]
</span><del>-        yield scheduleNextMailPoll(self.store, seconds)
</del><ins>+        yield IMIPPollingWork.reschedule(self.store, seconds)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -583,6 +583,7 @@
</span><span class="cx">             label = &quot;{}#{}${}&quot;.format(tr.filename, tr.lineno, tr.function)
</span><span class="cx"> 
</span><span class="cx">         self._store = store
</span><ins>+        self._queuer = self._store.queuer
</ins><span class="cx">         self._calendarHomes = {}
</span><span class="cx">         self._addressbookHomes = {}
</span><span class="cx">         self._notificationHomes = {}
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoreworkinbox_cleanuppy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/work/inbox_cleanup.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/work/inbox_cleanup.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/txdav/common/datastore/work/inbox_cleanup.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -20,10 +20,10 @@
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><del>-from twext.enterprise.dal.syntax import Delete, Select, Count
-from twext.enterprise.jobqueue import WorkItem
</del><ins>+from twext.enterprise.dal.syntax import Select, Count
+from twext.enterprise.jobqueue import WorkItem, RegeneratingWorkItem
</ins><span class="cx"> from twext.python.log import Logger
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue
</del><ins>+from twisted.internet.defer import inlineCallbacks, succeed
</ins><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> from txdav.common.datastore.sql_tables import schema, _HOME_STATUS_NORMAL
</span><span class="cx"> import datetime
</span><span class="lines">@@ -31,26 +31,31 @@
</span><span class="cx"> log = Logger()
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class InboxCleanupWork(WorkItem,
-    fromTable(schema.INBOX_CLEANUP_WORK)):
</del><ins>+class InboxCleanupWork(RegeneratingWorkItem, fromTable(schema.INBOX_CLEANUP_WORK)):
</ins><span class="cx"> 
</span><span class="cx">     group = &quot;inbox_cleanup&quot;
</span><span class="cx"> 
</span><span class="cx">     @classmethod
</span><del>-    @inlineCallbacks
-    def _schedule(cls, txn, seconds):
-        notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-        log.debug(&quot;Scheduling clean inboxes work: {}&quot;.format(notBefore,))
-        wp = yield txn.enqueue(cls, notBefore=notBefore)
-        returnValue(wp)
</del><ins>+    def initialSchedule(cls, store, seconds):
+        def _enqueue(txn):
+            return InboxCleanupWork.reschedule(txn, seconds)
</ins><span class="cx"> 
</span><ins>+        if config.InboxCleanup.Enabled:
+            return store.inTransaction(&quot;InboxCleanupWork.initialSchedule&quot;, _enqueue)
+        else:
+            return succeed(None)
</ins><span class="cx"> 
</span><ins>+
+    def regenerateInterval(self):
+        &quot;&quot;&quot;
+        Return the interval in seconds between regenerating instances.
+        &quot;&quot;&quot;
+        return float(config.InboxCleanup.CleanupPeriodDays) * 24 * 60 * 60
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def doWork(self):
</span><span class="cx"> 
</span><del>-        # Delete all other work items
-        yield Delete(From=self.table, Where=None).on(self.transaction)
-
</del><span class="cx">         # exit if not done with last delete:
</span><span class="cx">         coiw = schema.CLEANUP_ONE_INBOX_WORK
</span><span class="cx">         queuedCleanupOneInboxWorkItems = (yield Select(
</span><span class="lines">@@ -71,32 +76,16 @@
</span><span class="cx">             ).on(self.transaction)
</span><span class="cx"> 
</span><span class="cx">             for homeRow in homeRows:
</span><del>-                yield CleanupOneInboxWork._schedule(self.transaction, homeID=homeRow[0], seconds=0)
</del><ins>+                yield CleanupOneInboxWork.reschedule(self.transaction, seconds=0, homeID=homeRow[0])
</ins><span class="cx"> 
</span><del>-        # Schedule next check
-        yield self._schedule(
-            self.transaction,
-            float(config.InboxCleanup.CleanupPeriodDays) * 24 * 60 * 60
-        )
</del><span class="cx"> 
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> class CleanupOneInboxWork(WorkItem,
</span><span class="cx">     fromTable(schema.CLEANUP_ONE_INBOX_WORK)):
</span><span class="cx"> 
</span><span class="cx">     group = property(lambda self: &quot;cleanup_inbox_in_homeid_{}&quot;.format(self.homeID))
</span><span class="cx"> 
</span><del>-    @classmethod
</del><span class="cx">     @inlineCallbacks
</span><del>-    def _schedule(cls, txn, homeID, seconds):
-        notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-        log.debug(&quot;Scheduling Inbox cleanup work: {notBefore} in home id: {homeID}&quot;.format(
-            notBefore=notBefore, homeID=homeID))
-        wp = yield txn.enqueue(cls, notBefore=notBefore, homeID=homeID)
-        returnValue(wp)
-
-
-    @inlineCallbacks
</del><span class="cx">     def doWork(self):
</span><span class="cx"> 
</span><span class="cx">         # No need to delete other work items.  They are unique
</span><span class="lines">@@ -143,15 +132,3 @@
</span><span class="cx">             inbox = yield home.childWithName(&quot;inbox&quot;)
</span><span class="cx">             for item in (yield inbox.objectResourcesWithNames(itemNamesToDelete)):
</span><span class="cx">                 yield item.remove()
</span><del>-
-
-
-@inlineCallbacks
-def scheduleFirstInboxCleanup(store, seconds):
-    if config.InboxCleanup.Enabled:
-        txn = store.newTransaction(label=&quot;scheduleFirstInboxCleanup&quot;)
-        wp = yield InboxCleanupWork._schedule(txn, seconds)
-        yield txn.commit()
-        returnValue(wp)
-    else:
-        log.debug(&quot;Inbox cleanup work disabled.&quot;)
</del></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoreworkrevision_cleanuppy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/work/revision_cleanup.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/work/revision_cleanup.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/txdav/common/datastore/work/revision_cleanup.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -20,10 +20,10 @@
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><del>-from twext.enterprise.dal.syntax import Delete, Select, Max
-from twext.enterprise.jobqueue import WorkItem
</del><ins>+from twext.enterprise.dal.syntax import Select, Max
+from twext.enterprise.jobqueue import SingletonWorkItem, RegeneratingWorkItem
</ins><span class="cx"> from twext.python.log import Logger
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue
</del><ins>+from twisted.internet.defer import inlineCallbacks, succeed
</ins><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><span class="cx"> import datetime
</span><span class="lines">@@ -31,26 +31,31 @@
</span><span class="cx"> log = Logger()
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class FindMinValidRevisionWork(WorkItem,
-    fromTable(schema.FIND_MIN_VALID_REVISION_WORK)):
</del><ins>+class FindMinValidRevisionWork(RegeneratingWorkItem, fromTable(schema.FIND_MIN_VALID_REVISION_WORK)):
</ins><span class="cx"> 
</span><span class="cx">     group = &quot;find_min_revision&quot;
</span><span class="cx"> 
</span><span class="cx">     @classmethod
</span><del>-    @inlineCallbacks
-    def _schedule(cls, txn, seconds):
-        notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-        log.debug(&quot;Scheduling find minimum valid revision work: %s&quot; % (notBefore,))
-        wp = yield txn.enqueue(cls, notBefore=notBefore)
-        returnValue(wp)
</del><ins>+    def initialSchedule(cls, store, seconds):
+        def _enqueue(txn):
+            return FindMinValidRevisionWork.reschedule(txn, seconds)
</ins><span class="cx"> 
</span><ins>+        if config.RevisionCleanup.Enabled:
+            return store.inTransaction(&quot;FindMinValidRevisionWork.initialSchedule&quot;, _enqueue)
+        else:
+            return succeed(None)
</ins><span class="cx"> 
</span><ins>+
+    def regenerateInterval(self):
+        &quot;&quot;&quot;
+        Return the interval in seconds between regenerating instances.
+        &quot;&quot;&quot;
+        return float(config.RevisionCleanup.CleanupPeriodDays) * 24 * 60 * 60
+
+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def doWork(self):
</span><span class="cx"> 
</span><del>-        # Delete all other work items
-        yield Delete(From=self.table, Where=None).on(self.transaction)
-
</del><span class="cx">         # Get the minimum valid revision
</span><span class="cx">         minValidRevision = int((yield self.transaction.calendarserverValue(&quot;MIN-VALID-REVISION&quot;)))
</span><span class="cx"> 
</span><span class="lines">@@ -82,57 +87,19 @@
</span><span class="cx">             yield self.transaction.updateCalendarserverValue(&quot;MIN-VALID-REVISION&quot;, maxRevOlderThanDate)
</span><span class="cx"> 
</span><span class="cx">             # Schedule revision cleanup
</span><del>-            yield RevisionCleanupWork._schedule(self.transaction, seconds=0)
</del><ins>+            yield RevisionCleanupWork.reschedule(self.transaction, seconds=0)
</ins><span class="cx"> 
</span><del>-        else:
-            # Schedule next check
-            yield FindMinValidRevisionWork._schedule(
-                self.transaction,
-                float(config.RevisionCleanup.CleanupPeriodDays) * 24 * 60 * 60
-            )
</del><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class RevisionCleanupWork(SingletonWorkItem, fromTable(schema.REVISION_CLEANUP_WORK)):
</ins><span class="cx"> 
</span><del>-class RevisionCleanupWork(WorkItem,
-    fromTable(schema.REVISION_CLEANUP_WORK)):
-
</del><span class="cx">     group = &quot;group_revsion_cleanup&quot;
</span><span class="cx"> 
</span><del>-    @classmethod
</del><span class="cx">     @inlineCallbacks
</span><del>-    def _schedule(cls, txn, seconds):
-        notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-        log.debug(&quot;Scheduling revision cleanup work: %s&quot; % (notBefore,))
-        wp = yield txn.enqueue(cls, notBefore=notBefore)
-        returnValue(wp)
-
-
-    @inlineCallbacks
</del><span class="cx">     def doWork(self):
</span><span class="cx"> 
</span><del>-        # Delete all other work items
-        yield Delete(From=self.table, Where=None).on(self.transaction)
-
</del><span class="cx">         # Get the minimum valid revision
</span><span class="cx">         minValidRevision = int((yield self.transaction.calendarserverValue(&quot;MIN-VALID-REVISION&quot;)))
</span><span class="cx"> 
</span><span class="cx">         # delete revisions
</span><span class="cx">         yield self.transaction.deleteRevisionsBefore(minValidRevision)
</span><del>-
-        # Schedule next update
-        yield FindMinValidRevisionWork._schedule(
-            self.transaction,
-            float(config.RevisionCleanup.CleanupPeriodDays) * 24 * 60 * 60
-        )
-
-
-
-@inlineCallbacks
-def scheduleFirstFindMinRevision(store, seconds):
-    if config.RevisionCleanup.Enabled:
-        txn = store.newTransaction(label=&quot;scheduleFirstFindMinRevision&quot;)
-        wp = yield FindMinValidRevisionWork._schedule(txn, seconds)
-        yield txn.commit()
-        returnValue(wp)
-    else:
-        log.debug(&quot;Revision cleanup work disabled.&quot;)
</del></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoreworktesttest_inbox_cleanuppy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/work/test/test_inbox_cleanup.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/work/test/test_inbox_cleanup.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/txdav/common/datastore/work/test/test_inbox_cleanup.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -126,25 +126,20 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Verify that InboxCleanupWork queues one CleanupOneInboxBoxWork per home
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        class FakeInboxCleanupWork(WorkItem):
-            @classmethod
-            def _schedule(cls, txn, seconds):
-                pass
</del><ins>+        self.patch(config.InboxCleanup, &quot;CleanupPeriodDays&quot;, -1)
</ins><span class="cx"> 
</span><del>-        self.patch(InboxCleanupWork, &quot;_schedule&quot;, FakeInboxCleanupWork._schedule)
-
</del><span class="cx">         class FakeCleanupOneInboxWork(WorkItem):
</span><span class="cx">             scheduledHomeIDs = []
</span><span class="cx"> 
</span><span class="cx">             @classmethod
</span><del>-            def _schedule(cls, txn, homeID, seconds):
</del><ins>+            def reschedule(cls, txn, seconds, homeID):
</ins><span class="cx">                 cls.scheduledHomeIDs.append(homeID)
</span><span class="cx">                 pass
</span><span class="cx"> 
</span><del>-        self.patch(CleanupOneInboxWork, &quot;_schedule&quot;, FakeCleanupOneInboxWork._schedule)
</del><ins>+        self.patch(CleanupOneInboxWork, &quot;reschedule&quot;, FakeCleanupOneInboxWork.reschedule)
</ins><span class="cx"> 
</span><span class="cx">         # do cleanup
</span><del>-        yield self.transactionUnderTest().enqueue(InboxCleanupWork, notBefore=datetime.datetime.utcnow())
</del><ins>+        yield InboxCleanupWork.reschedule(self.transactionUnderTest(), 0)
</ins><span class="cx">         yield self.commit()
</span><span class="cx">         yield JobItem.waitEmpty(self.storeUnderTest().newTransaction, reactor, 60)
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoreworktesttest_revision_cleanuppy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/work/test/test_revision_cleanup.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/work/test/test_revision_cleanup.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/txdav/common/datastore/work/test/test_revision_cleanup.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -17,7 +17,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.syntax import Select
</span><del>-from twext.enterprise.jobqueue import WorkItem, JobItem
</del><ins>+from twext.enterprise.jobqueue import JobItem
</ins><span class="cx"> from twext.python.clsprop import classproperty
</span><span class="cx"> from twisted.internet import reactor
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue
</span><span class="lines">@@ -43,14 +43,9 @@
</span><span class="cx">         yield self.buildStoreAndDirectory()
</span><span class="cx">         yield self.populate()
</span><span class="cx"> 
</span><del>-        class FakeWork(WorkItem):
-            @classmethod
-            def _schedule(cls, txn, seconds):
-                pass
-
-        self.patch(FindMinValidRevisionWork, &quot;_schedule&quot;, FakeWork._schedule)
-        self.patch(RevisionCleanupWork, &quot;_schedule&quot;, FakeWork._schedule)
</del><ins>+        self.patch(config.RevisionCleanup, &quot;Enabled&quot;, True)
</ins><span class="cx">         self.patch(config.RevisionCleanup, &quot;SyncTokenLifetimeDays&quot;, 0)
</span><ins>+        self.patch(config.RevisionCleanup, &quot;CleanupPeriodDays&quot;, -1)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="lines">@@ -266,7 +261,7 @@
</span><span class="cx">         self.assertNotEqual(len(revisionRows), 0)
</span><span class="cx"> 
</span><span class="cx">         # do FindMinValidRevisionWork
</span><del>-        yield self.transactionUnderTest().enqueue(FindMinValidRevisionWork, notBefore=datetime.datetime.utcnow())
</del><ins>+        yield FindMinValidRevisionWork.reschedule(self.transactionUnderTest(), 0)
</ins><span class="cx">         yield self.commit()
</span><span class="cx">         yield JobItem.waitEmpty(self.storeUnderTest().newTransaction, reactor, 60)
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhogroupspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/groups.py (13515 => 13516)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/groups.py        2014-05-20 18:38:40 UTC (rev 13515)
+++ CalendarServer/trunk/txdav/who/groups.py        2014-05-20 18:49:22 UTC (rev 13516)
</span><span class="lines">@@ -21,9 +21,9 @@
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import fromTable
</span><span class="cx"> from twext.enterprise.dal.syntax import Delete, Select
</span><del>-from twext.enterprise.jobqueue import WorkItem
</del><ins>+from twext.enterprise.jobqueue import WorkItem, RegeneratingWorkItem
</ins><span class="cx"> from twext.python.log import Logger
</span><del>-from twisted.internet.defer import inlineCallbacks, returnValue
</del><ins>+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</ins><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> from txdav.caldav.datastore.sql import CalendarStoreFeatures
</span><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><span class="lines">@@ -34,36 +34,38 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> class GroupCacherPollingWork(
</span><del>-    WorkItem,
</del><ins>+    RegeneratingWorkItem,
</ins><span class="cx">     fromTable(schema.GROUP_CACHER_POLLING_WORK)
</span><span class="cx"> ):
</span><span class="cx"> 
</span><span class="cx">     group = &quot;group_cacher_polling&quot;
</span><span class="cx"> 
</span><del>-    @inlineCallbacks
-    def doWork(self):
</del><ins>+    @classmethod
+    def initialSchedule(cls, store, seconds):
+        def _enqueue(txn):
+            return GroupCacherPollingWork.reschedule(txn, seconds)
</ins><span class="cx"> 
</span><del>-        # Delete all other work items
-        yield Delete(From=self.table, Where=None).on(self.transaction)
</del><ins>+        if config.InboxCleanup.Enabled:
+            return store.inTransaction(&quot;GroupCacherPollingWork.initialSchedule&quot;, _enqueue)
+        else:
+            return succeed(None)
</ins><span class="cx"> 
</span><ins>+
+    def regenerateInterval(self):
+        &quot;&quot;&quot;
+        Return the interval in seconds between regenerating instances.
+        &quot;&quot;&quot;
</ins><span class="cx">         groupCacher = getattr(self.transaction, &quot;_groupCacher&quot;, None)
</span><del>-        if groupCacher is not None:
</del><ins>+        return groupCacher.updateSeconds if groupCacher else 10
</ins><span class="cx"> 
</span><del>-            # Schedule next update
</del><span class="cx"> 
</span><del>-            notBefore = (
-                datetime.datetime.utcnow() +
-                datetime.timedelta(seconds=groupCacher.updateSeconds)
-            )
-            log.debug(
-                &quot;Scheduling next group cacher update: {when}&quot;, when=notBefore
-            )
-            yield self.transaction.enqueue(
-                GroupCacherPollingWork,
-                notBefore=notBefore
-            )
</del><ins>+    @inlineCallbacks
+    def doWork(self):
</ins><span class="cx"> 
</span><del>-            # New implmementation
</del><ins>+        groupCacher = getattr(self.transaction, &quot;_groupCacher&quot;, None)
+        if groupCacher is not None:
+
+            # New implementation
</ins><span class="cx">             try:
</span><span class="cx">                 yield groupCacher.update(self.transaction)
</span><span class="cx">             except Exception, e:
</span><span class="lines">@@ -72,49 +74,8 @@
</span><span class="cx">                     error=e
</span><span class="cx">                 )
</span><span class="cx"> 
</span><del>-        else:
-            notBefore = (
-                datetime.datetime.utcnow() +
-                datetime.timedelta(seconds=10)
-            )
-            log.debug(
-                &quot;Rescheduling group cacher update: {when}&quot;, when=notBefore
-            )
-            yield self.transaction.enqueue(
-                GroupCacherPollingWork,
-                notBefore=notBefore
-            )
</del><span class="cx"> 
</span><span class="cx"> 
</span><del>-
-@inlineCallbacks
-def scheduleNextGroupCachingUpdate(store, seconds):
-
-    notBefore = (
-        datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-    )
-
-    log.debug(
-        &quot;Scheduling next group cacher update: {when}&quot;, when=notBefore
-    )
-
-    def _enqueue(txn):
-        return txn.enqueue(GroupCacherPollingWork, notBefore=notBefore)
-
-    wp = yield store.inTransaction(&quot;scheduleNextGroupCachingUpdate&quot;, _enqueue)
-
-    returnValue(wp)
-
-
-
-def schedulePolledGroupCachingUpdate(store):
-    &quot;&quot;&quot;
-    Schedules a group caching update work item to run immediately.
-    &quot;&quot;&quot;
-    return scheduleNextGroupCachingUpdate(store, 0)
-
-
-
</del><span class="cx"> class GroupRefreshWork(WorkItem, fromTable(schema.GROUP_REFRESH_WORK)):
</span><span class="cx"> 
</span><span class="cx">     group = property(lambda self: self.groupUid)
</span><span class="lines">@@ -163,26 +124,7 @@
</span><span class="cx">         lambda self: &quot;{0}, {1}&quot;.format(self.groupID, self.resourceID)
</span><span class="cx">     )
</span><span class="cx"> 
</span><del>-    @classmethod
-    @inlineCallbacks
-    def _schedule(cls, txn, eventID, groupID, seconds):
-        notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-        log.debug(
-            &quot;scheduling group reconciliation for &quot;
-            &quot;({resourceID}, {groupID},): {when}&quot;,
-            resourceID=eventID,
-            groupID=groupID,
-            when=notBefore
-        )
-        wp = yield txn.enqueue(
-            cls,
-            resourceID=eventID,
-            groupID=groupID,
-            notBefore=notBefore,
-        )
-        returnValue(wp)
</del><span class="cx"> 
</span><del>-
</del><span class="cx">     @inlineCallbacks
</span><span class="cx">     def doWork(self):
</span><span class="cx"> 
</span><span class="lines">@@ -432,11 +374,11 @@
</span><span class="cx"> 
</span><span class="cx">         wps = []
</span><span class="cx">         for [eventID] in rows:
</span><del>-            wp = yield GroupAttendeeReconciliationWork._schedule(
</del><ins>+            wp = yield GroupAttendeeReconciliationWork.reschedule(
</ins><span class="cx">                 txn,
</span><del>-                eventID=eventID,
</del><ins>+                seconds=float(config.GroupAttendees.ReconciliationDelaySeconds),
+                resourceID=eventID,
</ins><span class="cx">                 groupID=groupID,
</span><del>-                seconds=float(config.GroupAttendees.ReconciliationDelaySeconds)
</del><span class="cx">             )
</span><span class="cx">             wps.append(wp)
</span><span class="cx">         returnValue(tuple(wps))
</span></span></pre>
</div>
</div>

</body>
</html>