]> source.dussan.org Git - nextcloud-server.git/commitdiff
make JobList::next() lock free 4253/head
authorJörn Friedrich Dreyer <jfd@butonic.de>
Fri, 7 Apr 2017 10:53:44 +0000 (12:53 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Fri, 7 Apr 2017 18:07:09 +0000 (13:07 -0500)
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
lib/private/BackgroundJob/JobList.php

index b6a235e9e4037a33c3c074576e2c2c5ed6f99f65..0de5dfecc8bbbbba77bd4960fee89291614f6df0 100644 (file)
@@ -187,18 +187,24 @@ class JobList implements IJobList {
                $update->update('jobs')
                        ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
                        ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime()))
-                       ->where($update->expr()->eq('id', $update->createParameter('jobid')));
+                       ->where($update->expr()->eq('id', $update->createParameter('jobid')))
+                       ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at')))
+                       ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked')));
 
-               $this->connection->lockTable('jobs');
                $result = $query->execute();
                $row = $result->fetch();
                $result->closeCursor();
 
                if ($row) {
                        $update->setParameter('jobid', $row['id']);
-                       $update->execute();
-                       $this->connection->unlockTable();
+                       $update->setParameter('reserved_at', $row['reserved_at']);
+                       $update->setParameter('last_checked', $row['last_checked']);
+                       $count = $update->execute();
 
+                       if ($count === 0) {
+                               // Background job already executed elsewhere, try again.
+                               return $this->getNext();
+                       }
                        $job = $this->buildJob($row);
 
                        if ($job === null) {
@@ -208,7 +214,6 @@ class JobList implements IJobList {
 
                        return $job;
                } else {
-                       $this->connection->unlockTable();
                        return null;
                }
        }