From d0c5fe6be4d6b00c5947f4b2a37ca9f496941e8b Mon Sep 17 00:00:00 2001 From: Michael Still Date: Sat, 12 Jan 2013 17:36:56 +1100 Subject: Fix logic error in periodic task wait code. I was calculating the time to wait for the next run of a periodic task incorrectly. Resolves bug 1098819. Change-Id: Ida60b69014aa06229111e58024e35268262f18fb --- nova/manager.py | 5 +++-- nova/tests/test_periodic_tasks.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nova/manager.py b/nova/manager.py index cb15b776e..7df63f719 100644 --- a/nova/manager.py +++ b/nova/manager.py @@ -215,8 +215,9 @@ class Manager(base.Base): if self._periodic_spacing[task_name] is None: wait = 0 else: - wait = time.time() - (self._periodic_last_run[task_name] + - self._periodic_spacing[task_name]) + due = (self._periodic_last_run[task_name] + + self._periodic_spacing[task_name]) + wait = max(0, due - time.time()) if wait > 0.2: if wait < idle_for: idle_for = wait diff --git a/nova/tests/test_periodic_tasks.py b/nova/tests/test_periodic_tasks.py index 5804ea49b..39669967f 100644 --- a/nova/tests/test_periodic_tasks.py +++ b/nova/tests/test_periodic_tasks.py @@ -17,6 +17,7 @@ import fixtures +import time from nova import manager from nova import test @@ -76,6 +77,19 @@ class Manager(test.TestCase): idle = m.periodic_tasks(None) self.assertAlmostEqual(60, idle, 1) + def test_periodic_tasks_idle_calculation(self): + class Manager(manager.Manager): + @manager.periodic_task(spacing=10) + def bar(self): + return 'bar' + + m = Manager() + m.periodic_tasks(None) + time.sleep(0.1) + idle = m.periodic_tasks(None) + self.assertTrue(idle > 9.7) + self.assertTrue(idle < 9.9) + def test_periodic_tasks_disabled(self): class Manager(manager.Manager): @manager.periodic_task(spacing=-1) -- cgit