From fadb65a730891bfad2ce2c34970c5ac7440c2738 Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Sun, 1 May 2016 20:27:08 -0400
Subject: [PATCH] cleanup

---
 celerytasks.py | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/celerytasks.py b/celerytasks.py
index c26a2d7..983ad82 100644
--- a/celerytasks.py
+++ b/celerytasks.py
@@ -5,6 +5,7 @@ import time
 from app import app, db
 from models import Agency, Prediction
 from nextbus import Nextbus
+from lock import LockException
 
 """
 Celery is a task queue for background task processing. We're using it
@@ -44,11 +45,10 @@ def update_routes(agencies=None):
     """
     if not agencies:
         agencies = app.config['AGENCIES']
-    route_count = 0
     for agency_tag in agencies:
-        route_count += len(Nextbus.get_routes(agency_tag, truncate=True))
+        routes = Nextbus.get_routes(agency_tag, truncate=True)
     print("update_routes: Got {0} routes for {1} agencies"\
-          .format(route_count, len(agencies)))
+          .format(len(routes), len(agencies)))
 
 @celery.task()
 def update_predictions(agencies=None):
@@ -58,10 +58,11 @@ def update_predictions(agencies=None):
     start = time.time()
     if not agencies:
         agencies = app.config['AGENCIES']
-    prediction_count = len(Nextbus.get_predictions(agencies, truncate=False))
+    predictions = Nextbus.get_predictions(agencies,
+                                          truncate=False)
     elapsed = time.time() - start
-    print("Got {0} predictions for {1} agencies in {2:0.2f} sec."\
-          .format(prediction_count, len(agencies), elapsed))
+    print("Got {0} predictions for {1} agencies in {2:0.2f} seconds."\
+          .format(len(predictions), len(agencies), elapsed))
 
 @celery.task()
 def update_vehicle_locations(agencies=None):
@@ -71,10 +72,16 @@ def update_vehicle_locations(agencies=None):
     start = time.time()
     if not agencies:
         agencies = app.config['AGENCIES']
-    vl_count = len(Nextbus.get_vehicle_locations(agencies, truncate=False))
+    try:
+        vl = Nextbus.get_vehicle_locations(agencies,
+                                           truncate=False,
+                                           max_wait=0)
+    except LockException as e:
+        print(e)
+        return
     elapsed = time.time() - start
     print("Got {0} vehicle locations for {1} agencies in {2:0.2f} seconds."\
-          .format(vl_count, len(agencies), elapsed))
+          .format(len(vl), len(agencies), elapsed))
 
 
 @celery.task()
-- 
GitLab