[node-patches] Change in ovirt-node[master]: utils: Add lockfile to transactions

fabiand at fedoraproject.org fabiand at fedoraproject.org
Mon Jun 10 12:29:12 UTC 2013


Fabian Deutsch has uploaded a new change for review.

Change subject: utils: Add lockfile to transactions
......................................................................

utils: Add lockfile to transactions

Previously multiple transaction could be run at once. Now a lock is
acuired before each transaction run to enforce only one transaction urn
at a time.

Change-Id: I0f3bfd60f2cda41ffcf13ff6dfc224d6d2d4a3ab
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=971673
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M ovirt-node.spec.in
M src/ovirt/node/utils/__init__.py
2 files changed, 22 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/08/15508/1

diff --git a/ovirt-node.spec.in b/ovirt-node.spec.in
index a9cd42f..df169f3 100644
--- a/ovirt-node.spec.in
+++ b/ovirt-node.spec.in
@@ -25,6 +25,7 @@
 %if %{is_systemd}
 BuildRequires:  systemd-units
 %endif
+BuildRequires:  python-lockfile
 
 Requires(post):  /sbin/chkconfig
 Requires(preun): /sbin/chkconfig
diff --git a/src/ovirt/node/utils/__init__.py b/src/ovirt/node/utils/__init__.py
index f0c6b95..5641f15 100644
--- a/src/ovirt/node/utils/__init__.py
+++ b/src/ovirt/node/utils/__init__.py
@@ -20,6 +20,8 @@
 # also available at http://www.gnu.org/copyleft/gpl.html.
 from ovirt.node import base, exceptions
 import augeas as _augeas
+import glob
+import lockfile
 import time
 import traceback
 
@@ -185,6 +187,10 @@
     >>> txs
     [[<StepA 'None'>], [<StepB 'None'>]]
     """
+    title = None
+    _lockfilename = "/tmp/transaction-in-progress"
+    _prepared_elements = None
+
     def __init__(self, title, elements=[]):
         super(Transaction, self).__init__()
         base.Base.__init__(self)
@@ -216,30 +222,34 @@
         self._prepared_elements = []
         return True
 
-    def __call__(self):
+    def run(self):
         self.logger.debug("Running transaction '%s'" % self)
         try:
-            self.prepare()
-            self.commit()
+            with lockfile.FileLock(self._lockfilename):
+                self.prepare()
+                self.commit()
         except Exception as e:
-            self.logger.warning("Transaction failed (%s): %s" % (e, e.message),
-                                exc_info=True)
+            self.logger.exception("Transaction failed (%s): %s" % (e,
+                                                                   e.message))
             self.abort()
             raise exceptions.TransactionError("Transaction failed: " +
                                               "%s" % e.message)
         self.logger.info("Transaction '%s' succeeded" % self)
         return True
 
+    def __call__(self):
+        self.run()
+
     def __str__(self):
-        return "<%s '%s' with %s>" % (self.__class__.__name__,
-                                      self.title, list.__str__(self))
+        return self.build_str(["title"], {"elements": list.__str__(self)})
 
     def step(self):
         try:
-            self.logger.debug("Preparing transaction %s" % self)
-            self.prepare()
-            for idx, e in enumerate(self):
-                yield (idx, e)
+            with lockfile.FileLock(self._lockfilename):
+                self.logger.debug("Preparing transaction %s" % self)
+                self.prepare()
+                for idx, e in enumerate(self):
+                    yield (idx, e)
         except Exception as e:
             self.logger.warning("'%s' on transaction '%s': %s - %s" %
                                 (type(e), self, e, e.message))


-- 
To view, visit http://gerrit.ovirt.org/15508
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0f3bfd60f2cda41ffcf13ff6dfc224d6d2d4a3ab
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list