diff --git a/celerytasks.py b/celerytasks.py index c26a2d7c061b9922c26911d94f9f68f251b08f06..983ad82b4a91994867c60d00221aae479c936aa7 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()