<!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>[14775] twext/trunk/twext/enterprise</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/14775">14775</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-05-11 13:11:43 -0700 (Mon, 11 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Support the ability to pause/unpause a job.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextenterprisedalrecordpy">twext/trunk/twext/enterprise/dal/record.py</a></li>
<li><a href="#twexttrunktwextenterprisedaltesttest_recordpy">twext/trunk/twext/enterprise/dal/test/test_record.py</a></li>
<li><a href="#twexttrunktwextenterprisejobqueuepy">twext/trunk/twext/enterprise/jobqueue.py</a></li>
<li><a href="#twexttrunktwextenterprisetesttest_jobqueuepy">twext/trunk/twext/enterprise/test/test_jobqueue.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextenterprisedalrecordpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/dal/record.py (14774 => 14775)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/record.py        2015-05-10 15:40:44 UTC (rev 14774)
+++ twext/trunk/twext/enterprise/dal/record.py        2015-05-11 20:11:43 UTC (rev 14775)
</span><span class="lines">@@ -594,6 +594,21 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @classmethod
</span><ins>+    def updatesome(cls, transaction, where, **kw):
+        &quot;&quot;&quot;
+        Update rows matching the where expression from the table that corresponds to C{cls}.
+        &quot;&quot;&quot;
+        colmap = {}
+        for k, v in kw.iteritems():
+            colmap[cls.__attrmap__[k]] = v
+
+        return Update(
+            colmap,
+            Where=where
+        ).on(transaction)
+
+
+    @classmethod
</ins><span class="cx">     def deleteall(cls, transaction):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Delete all rows from the table that corresponds to C{cls}.
</span></span></pre></div>
<a id="twexttrunktwextenterprisedaltesttest_recordpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/dal/test/test_record.py (14774 => 14775)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/dal/test/test_record.py        2015-05-10 15:40:44 UTC (rev 14774)
+++ twext/trunk/twext/enterprise/dal/test/test_record.py        2015-05-11 20:11:43 UTC (rev 14775)
</span><span class="lines">@@ -363,6 +363,45 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><ins>+    def test_updatesome(self):
+        &quot;&quot;&quot;
+        L{Record.updatesome} will update all instances of the matching records.
+        &quot;&quot;&quot;
+        txn = self.pool.connection()
+        data = [(123, u&quot;one&quot;), (456, u&quot;four&quot;), (345, u&quot;three&quot;),
+                (234, u&quot;two&quot;), (356, u&quot;three&quot;)]
+        for beta, gamma in data:
+            yield txn.execSQL(&quot;insert into ALPHA values (:1, :2)&quot;,
+                              [beta, gamma])
+
+        yield TestRecord.updatesome(txn, where=(TestRecord.beta == 123), gamma=u&quot;changed&quot;)
+        yield txn.commit()
+
+        txn = self.pool.connection()
+        records = yield TestRecord.all(txn)
+        self.assertEqual(
+            set([(record.beta, record.gamma,) for record in records]),
+            set([
+                (123, u&quot;changed&quot;), (456, u&quot;four&quot;), (345, u&quot;three&quot;),
+                (234, u&quot;two&quot;), (356, u&quot;three&quot;)
+            ])
+        )
+
+        yield TestRecord.updatesome(txn, where=(TestRecord.beta.In((234, 345,))), gamma=u&quot;changed-2&quot;)
+        yield txn.commit()
+
+        txn = self.pool.connection()
+        records = yield TestRecord.all(txn)
+        self.assertEqual(
+            set([(record.beta, record.gamma,) for record in records]),
+            set([
+                (123, u&quot;changed&quot;), (456, u&quot;four&quot;), (345, u&quot;changed-2&quot;),
+                (234, u&quot;changed-2&quot;), (356, u&quot;three&quot;)
+            ])
+        )
+
+
+    @inlineCallbacks
</ins><span class="cx">     def test_deleteall(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         L{Record.deleteall} will delete all instances of the record.
</span></span></pre></div>
<a id="twexttrunktwextenterprisejobqueuepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/jobqueue.py (14774 => 14775)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/jobqueue.py        2015-05-10 15:40:44 UTC (rev 14774)
+++ twext/trunk/twext/enterprise/jobqueue.py        2015-05-11 20:11:43 UTC (rev 14775)
</span><span class="lines">@@ -146,7 +146,8 @@
</span><span class="cx"> )
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.model import ProcedureCall, Sequence
</span><del>-from twext.enterprise.dal.record import Record, fromTable, NoSuchRecord
</del><ins>+from twext.enterprise.dal.record import Record, fromTable, NoSuchRecord, \
+    SerializableRecord
</ins><span class="cx"> from twisted.python.failure import Failure
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.model import Table, Schema, SQLType, Constraint
</span><span class="lines">@@ -237,6 +238,7 @@
</span><span class="cx">     JobTable.addColumn(&quot;ASSIGNED&quot;, SQLType(&quot;timestamp&quot;, None), default=None)
</span><span class="cx">     JobTable.addColumn(&quot;OVERDUE&quot;, SQLType(&quot;timestamp&quot;, None), default=None)
</span><span class="cx">     JobTable.addColumn(&quot;FAILED&quot;, SQLType(&quot;integer&quot;, 0), default=0)
</span><ins>+    JobTable.addColumn(&quot;PAUSE&quot;, SQLType(&quot;integer&quot;, 0), default=0)
</ins><span class="cx"> 
</span><span class="cx">     return inSchema
</span><span class="cx"> 
</span><span class="lines">@@ -351,6 +353,7 @@
</span><span class="cx"> 
</span><span class="cx"> class JobItem(Record, fromTable(JobInfoSchema.JOB)):
</span><span class="cx">     &quot;&quot;&quot;
</span><ins>+    @DynamicAttrs
</ins><span class="cx">     An item in the job table. This is typically not directly used by code
</span><span class="cx">     creating work items, but rather is used for internal book keeping of jobs
</span><span class="cx">     associated with work items.
</span><span class="lines">@@ -443,6 +446,21 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def pauseIt(self, pause=False):
+        &quot;&quot;&quot;
+        Pause the L{JobItem} leaving all other attributes the same. The job processing loop
+        will skip paused items.
+
+        @param pause: indicates whether the job should be paused.
+        @type pause: L{bool}
+        @param delay: how long before the job is run again, or C{None} for a default
+            staggered delay behavior.
+        @type delay: L{int}
+        &quot;&quot;&quot;
+
+        return self.update(pause=pause)
+
+
</ins><span class="cx">     @classmethod
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def ultimatelyPerform(cls, txnFactory, jobID):
</span><span class="lines">@@ -612,15 +630,17 @@
</span><span class="cx">         @rtype: L{JobItem}
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><ins>+        queryExpr = (cls.notBefore &lt;= now).And(cls.priority &gt;= minPriority).And(cls.pause == 0).And(
+            (cls.assigned == None).Or(cls.overdue &lt; now)
+        )
+
</ins><span class="cx">         if txn.dialect == ORACLE_DIALECT:
</span><span class="cx">             # Oracle does not support a &quot;for update&quot; clause with &quot;order by&quot;. So do the
</span><span class="cx">             # &quot;for update&quot; as a second query right after the first. Will need to check
</span><span class="cx">             # how this might impact concurrency in a multi-host setup.
</span><span class="cx">             jobs = yield cls.query(
</span><span class="cx">                 txn,
</span><del>-                (cls.notBefore &lt;= now).And(cls.priority &gt;= minPriority).And(
-                    (cls.assigned == None).Or(cls.overdue &lt; now)
-                ),
</del><ins>+                queryExpr,
</ins><span class="cx">                 order=(cls.assigned, cls.priority),
</span><span class="cx">                 ascending=False,
</span><span class="cx">                 limit=limit,
</span><span class="lines">@@ -635,9 +655,7 @@
</span><span class="cx">         else:
</span><span class="cx">             jobs = yield cls.query(
</span><span class="cx">                 txn,
</span><del>-                (cls.notBefore &lt;= now).And(cls.priority &gt;= minPriority).And(
-                    (cls.assigned == None).Or(cls.overdue &lt; now)
-                ),
</del><ins>+                queryExpr,
</ins><span class="cx">                 order=(cls.assigned, cls.priority),
</span><span class="cx">                 ascending=False,
</span><span class="cx">                 forUpdate=True,
</span><span class="lines">@@ -900,7 +918,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class WorkItem(Record):
</del><ins>+class WorkItem(SerializableRecord):
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     A L{WorkItem} is an item of work which may be stored in a database, then
</span><span class="cx">     executed later.
</span><span class="lines">@@ -1028,6 +1046,7 @@
</span><span class="cx">         _transferArg(&quot;priority&quot;)
</span><span class="cx">         _transferArg(&quot;weight&quot;)
</span><span class="cx">         _transferArg(&quot;notBefore&quot;)
</span><ins>+        _transferArg(&quot;pause&quot;)
</ins><span class="cx"> 
</span><span class="cx">         # Always need a notBefore
</span><span class="cx">         if &quot;notBefore&quot; not in jobargs:
</span></span></pre></div>
<a id="twexttrunktwextenterprisetesttest_jobqueuepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/enterprise/test/test_jobqueue.py (14774 => 14775)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/enterprise/test/test_jobqueue.py        2015-05-10 15:40:44 UTC (rev 14774)
+++ twext/trunk/twext/enterprise/test/test_jobqueue.py        2015-05-11 20:11:43 UTC (rev 14775)
</span><span class="lines">@@ -207,7 +207,8 @@
</span><span class="cx">       NOT_BEFORE  timestamp not null,
</span><span class="cx">       ASSIGNED    timestamp default null,
</span><span class="cx">       OVERDUE     timestamp default null,
</span><del>-      FAILED      integer default 0
</del><ins>+      FAILED      integer default 0,
+      PAUSE       integer default 0
</ins><span class="cx">     );
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> )
</span><span class="lines">@@ -533,6 +534,31 @@
</span><span class="cx">         self.assertTrue(job is None)
</span><span class="cx">         self.assertTrue(work is None)
</span><span class="cx"> 
</span><ins>+        # Unassigned, paused job with past notBefore not returned
+        yield self._enqueue(dbpool, 3, 1, now + datetime.timedelta(days=-1), priority=WORK_PRIORITY_HIGH)
+        @inlineCallbacks
+        def pauseJob(txn, pause=True):
+            works = yield DummyWorkItem.all(txn)
+            for work in works:
+                if work.a == 3:
+                    job = yield JobItem.load(txn, work.jobID)
+                    yield job.pauseIt(pause)
+        yield inTransaction(dbpool.connection, pauseJob)
+        job, work = yield inTransaction(dbpool.connection, _next)
+        self.assertTrue(job is None)
+        self.assertTrue(work is None)
+
+        # Unassigned, paused then unpaused job with past notBefore is returned
+        yield inTransaction(dbpool.connection, pauseJob, pause=False)
+        job, work = yield inTransaction(dbpool.connection, _next)
+        self.assertTrue(job is not None)
+        self.assertTrue(work.a == 3)
+        @inlineCallbacks
+        def deleteJob(txn, jobID):
+            job = yield JobItem.load(txn, jobID)
+            yield job.delete()
+        yield inTransaction(dbpool.connection, deleteJob, jobID=job.jobID)
+
</ins><span class="cx">         # Unassigned low priority job with past notBefore not returned if high priority required
</span><span class="cx">         yield self._enqueue(dbpool, 4, 1, now + datetime.timedelta(days=-1))
</span><span class="cx">         job, work = yield inTransaction(dbpool.connection, _next, priority=WORK_PRIORITY_HIGH)
</span></span></pre>
</div>
</div>

</body>
</html>