Merge "Fix referenced before assignment for BuildCompletedEvent"
diff --git a/zuul/scheduler.py b/zuul/scheduler.py
index f08612d..a30b735 100644
--- a/zuul/scheduler.py
+++ b/zuul/scheduler.py
@@ -1105,10 +1105,11 @@
             return
         if build.end_time and build.start_time and build.result:
             duration = build.end_time - build.start_time
-        try:
-            self.time_database.update(build.job.name, duration, build.result)
-        except Exception:
-            self.log.exception("Exception recording build time:")
+            try:
+                self.time_database.update(
+                    build.job.name, duration, build.result)
+            except Exception:
+                self.log.exception("Exception recording build time:")
         pipeline.manager.onBuildCompleted(event.build)
 
     def _doMergeCompletedEvent(self, event):