]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
scheduler: Improve task descriptions in debug logs
authorZane Bitter <zbitter@redhat.com>
Mon, 17 Jun 2013 15:42:04 +0000 (17:42 +0200)
committerZane Bitter <zbitter@redhat.com>
Mon, 17 Jun 2013 15:42:04 +0000 (17:42 +0200)
Tasks that were simply defined as ordinary functions (not methods) were
identified by their repr() in the logs, which is only really desirable as a
last resort and for objects (where the __call__() method is the task). Use
the function name instead.

Change-Id: Ie34a3602deb968979118b0961bcddf226cf5983f

heat/engine/scheduler.py
heat/tests/test_scheduler.py

index b4f750695a002ec73fe0a5e1131d9697140d2bea..6857c9bb446385081ac517c2f9cfc0abb0bf6448 100644 (file)
@@ -31,11 +31,14 @@ def task_description(task):
     Return a human-readable string description of a task suitable for logging
     the status of the task.
     """
+    name = getattr(task, '__name__', None)
     if isinstance(task, types.MethodType):
-        name = getattr(task, '__name__')
-        obj = getattr(task, '__self__')
+        obj = getattr(task, '__self__', None)
         if name is not None and obj is not None:
             return '%s from %s' % (name, obj)
+    elif isinstance(task, types.FunctionType):
+        if name is not None:
+            return str(name)
     return repr(task)
 
 
index 95b0caebb1e00aaa8c93f8ba110a2efce37107c3..b7c54f6ed30b7207147477b43fead94cec14ab46 100644 (file)
@@ -595,6 +595,45 @@ class TaskTest(mox.MoxTestBase):
         self.mox.VerifyAll()
 
 
+class DescriptionTest(mox.MoxTestBase):
+    def test_func(self):
+        def f():
+            pass
+
+        self.assertEqual(scheduler.task_description(f), 'f')
+
+    def test_lambda(self):
+        l = lambda: None
+
+        self.assertEqual(scheduler.task_description(l), '<lambda>')
+
+    def test_method(self):
+        class C(object):
+            def __str__(self):
+                return 'C "o"'
+
+            def __repr__(self):
+                return 'o'
+
+            def m(self):
+                pass
+
+        self.assertEqual(scheduler.task_description(C().m), 'm from C "o"')
+
+    def test_object(self):
+        class C(object):
+            def __str__(self):
+                return 'C "o"'
+
+            def __repr__(self):
+                return 'o'
+
+            def __call__(self):
+                pass
+
+        self.assertEqual(scheduler.task_description(C()), 'o')
+
+
 class WrapperTaskTest(mox.MoxTestBase):
 
     def test_wrap(self):