diff --git a/buildman/server.py b/buildman/server.py index ebbc558bb..21ce7cd89 100644 --- a/buildman/server.py +++ b/buildman/server.py @@ -8,15 +8,16 @@ from autobahn.wamp import types from aiowsgi import create_server as create_wsgi_server from flask import Flask from threading import Event +from trollius.tasks import Task from trollius.coroutines import From from datetime import timedelta from buildman.jobutil.buildstatus import StatusHandler from buildman.jobutil.buildjob import BuildJob, BuildJobLoadException from data import database -from data.queue import WorkQueue from app import app +# pylint: disable=invalid-name logger = logging.getLogger(__name__) WORK_CHECK_TIMEOUT = 10 @@ -26,16 +27,19 @@ MINIMUM_JOB_EXTENSION = timedelta(minutes=2) HEARTBEAT_PERIOD_SEC = 30 +# pylint: disable=too-few-public-methods class BuildJobResult(object): """ Build job result enum """ INCOMPLETE = 'incomplete' COMPLETE = 'complete' ERROR = 'error' +# pylint: disable=too-many-instance-attributes class BuilderServer(object): """ Server which handles both HTTP and WAMP requests, managing the full state of the build controller. """ + # pylint: disable=too-many-arguments def __init__(self, registry_hostname, queue, build_logs, user_files, lifecycle_manager_klass, lifecycle_manager_config, manager_hostname): self._loop = None @@ -82,8 +86,14 @@ class BuilderServer(object): logger.debug('Starting server on port %s, with controller on port %s', websocket_port, controller_port) + + tasks = [ + Task(self._initialize(loop, host, websocket_port, controller_port, ssl)), + Task(self._metrics_updater()), + ] + try: - loop.run_until_complete(self._initialize(loop, host, websocket_port, controller_port, ssl)) + loop.run_until_complete(trollius.wait(tasks)) except KeyboardInterrupt: pass finally: @@ -168,7 +178,11 @@ class BuilderServer(object): logger.debug('All workers are busy. Requeuing.') self._queue.incomplete(job_item, restore_retry=True, retry_after=0) - + @trollius.coroutine + def _metrics_updater(self): + while self._current_status == 'running': + yield From(trollius.sleep(30)) + self._queue.update_metrics() @trollius.coroutine def _initialize(self, loop, host, websocket_port, controller_port, ssl=None): @@ -182,5 +196,8 @@ class BuilderServer(object): create_wsgi_server(self._controller_app, loop=loop, host=host, port=controller_port, ssl=ssl) yield From(loop.create_server(transport_factory, host, websocket_port, ssl=ssl)) + # Initialize the coroutine reporting metrics + loop.create_task(self._metrics_updater()) + # Initialize the work queue checker. yield From(self._work_checker())