diff --git a/app/scheduler.py b/app/scheduler.py index ef02aaf8..06acb337 100644 --- a/app/scheduler.py +++ b/app/scheduler.py @@ -9,7 +9,7 @@ from django.db.models import Q import random logger = logging.getLogger('app.logger') -scheduler = None +scheduler = BackgroundScheduler() def job(func): """ @@ -17,7 +17,8 @@ def job(func): so that we can call update_nodes_info(background=True) from the outside """ def wrapper(*args,**kwargs): - if (kwargs.get('background', False)): + background = kwargs.get('background', False) + if background: t = Thread(target=func) t.start() return t @@ -59,16 +60,13 @@ def process_pending_tasks(): if tasks.count() > 0: pool = ThreadPool(tasks.count()) - pool.map(process, tasks) + pool.map(process, tasks, chunksize=1) pool.close() pool.join() def setup(): - global scheduler - logger.info("Starting background scheduler...") try: - scheduler = BackgroundScheduler() scheduler.start() scheduler.add_job(update_nodes_info, 'interval', seconds=30) scheduler.add_job(process_pending_tasks, 'interval', seconds=5) @@ -76,9 +74,8 @@ def setup(): logger.warn("Scheduler already running (this is OK while testing)") def teardown(): - if scheduler != None: - logger.info("Stopping scheduler...") - try: - scheduler.shutdown() - except SchedulerNotRunningError: - logger.warn("Scheduler not running") + logger.info("Stopping scheduler...") + try: + scheduler.shutdown() + except SchedulerNotRunningError: + logger.warn("Scheduler not running") diff --git a/nodeodm/api_client.py b/nodeodm/api_client.py index 55f00d98..13d0f926 100644 --- a/nodeodm/api_client.py +++ b/nodeodm/api_client.py @@ -6,6 +6,7 @@ import requests import mimetypes import json import os +from urlparse import urlunparse class ApiClient: def __init__(self, host, port): @@ -13,7 +14,10 @@ class ApiClient: self.port = port def url(self, url): - return "http://{}:{}{}".format(self.host, self.port, url) + netloc = self.host if self.port == 80 else "{}:{}".format(self.host, self.port) + + # TODO: https support + return urlunparse(('http', netloc, url, '', '', '')) def info(self): return requests.get(self.url('/info')).json()