Skip to content
Snippets Groups Projects
Commit 63231721 authored by Martin Blanchard's avatar Martin Blanchard
Browse files

Handle the server's main event loop internally

This turns Server.start() into a blocking call!

#135
parent a770c779
No related branches found
No related tags found
Loading
...@@ -20,7 +20,6 @@ Server command ...@@ -20,7 +20,6 @@ Server command
Create a BuildGrid server. Create a BuildGrid server.
""" """
import asyncio
import sys import sys
import click import click
...@@ -51,18 +50,14 @@ def start(context, config): ...@@ -51,18 +50,14 @@ def start(context, config):
click.echo("ERROR: Could not parse config: {}.\n".format(str(e)), err=True) click.echo("ERROR: Could not parse config: {}.\n".format(str(e)), err=True)
sys.exit(-1) sys.exit(-1)
loop = asyncio.get_event_loop()
try: try:
server.start() server.start()
loop.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
click.echo("Stopping server")
server.stop() server.stop()
loop.close()
def _create_server_from_config(config): def _create_server_from_config(config):
......
...@@ -13,18 +13,19 @@ ...@@ -13,18 +13,19 @@
# limitations under the License. # limitations under the License.
import asyncio
from concurrent import futures from concurrent import futures
import logging import logging
import os import os
import grpc import grpc
from .cas.service import ByteStreamService, ContentAddressableStorageService from buildgrid.server.actioncache.service import ActionCacheService
from .actioncache.service import ActionCacheService from buildgrid.server.bots.service import BotsService
from .execution.service import ExecutionService from buildgrid.server.cas.service import ByteStreamService, ContentAddressableStorageService
from .operations.service import OperationsService from buildgrid.server.execution.service import ExecutionService
from .bots.service import BotsService from buildgrid.server.operations.service import OperationsService
from .referencestorage.service import ReferenceStorageService from buildgrid.server.referencestorage.service import ReferenceStorageService
class BuildGridServer: class BuildGridServer:
...@@ -46,9 +47,10 @@ class BuildGridServer: ...@@ -46,9 +47,10 @@ class BuildGridServer:
# Use max_workers default from Python 3.5+ # Use max_workers default from Python 3.5+
max_workers = (os.cpu_count() or 1) * 5 max_workers = (os.cpu_count() or 1) * 5
server = grpc.server(futures.ThreadPoolExecutor(max_workers)) self.__grpc_executor = futures.ThreadPoolExecutor(max_workers)
self.__grpc_server = grpc.server(self.__grpc_executor)
self._server = server self.__main_loop = asyncio.get_event_loop()
self._execution_service = None self._execution_service = None
self._bots_service = None self._bots_service = None
...@@ -59,14 +61,16 @@ class BuildGridServer: ...@@ -59,14 +61,16 @@ class BuildGridServer:
self._bytestream_service = None self._bytestream_service = None
def start(self): def start(self):
"""Starts the server. """Starts the BuildGrid server."""
""" self.__grpc_server.start()
self._server.start()
def stop(self, grace=0): self.__main_loop.run_forever()
"""Stops the server.
""" def stop(self):
self._server.stop(grace) """Stops the BuildGrid server."""
self.__main_loop.close()
self.__grpc_server.stop(None)
def add_port(self, address, credentials): def add_port(self, address, credentials):
"""Adds a port to the server. """Adds a port to the server.
...@@ -80,11 +84,11 @@ class BuildGridServer: ...@@ -80,11 +84,11 @@ class BuildGridServer:
""" """
if credentials is not None: if credentials is not None:
self.__logger.info("Adding secure connection on: [%s]", address) self.__logger.info("Adding secure connection on: [%s]", address)
self._server.add_secure_port(address, credentials) self.__grpc_server.add_secure_port(address, credentials)
else: else:
self.__logger.info("Adding insecure connection on [%s]", address) self.__logger.info("Adding insecure connection on [%s]", address)
self._server.add_insecure_port(address) self.__grpc_server.add_insecure_port(address)
def add_execution_instance(self, instance, instance_name): def add_execution_instance(self, instance, instance_name):
"""Adds an :obj:`ExecutionInstance` to the service. """Adds an :obj:`ExecutionInstance` to the service.
...@@ -96,7 +100,7 @@ class BuildGridServer: ...@@ -96,7 +100,7 @@ class BuildGridServer:
instance_name (str): Instance name. instance_name (str): Instance name.
""" """
if self._execution_service is None: if self._execution_service is None:
self._execution_service = ExecutionService(self._server) self._execution_service = ExecutionService(self.__grpc_server)
self._execution_service.add_instance(instance_name, instance) self._execution_service.add_instance(instance_name, instance)
...@@ -110,7 +114,7 @@ class BuildGridServer: ...@@ -110,7 +114,7 @@ class BuildGridServer:
instance_name (str): Instance name. instance_name (str): Instance name.
""" """
if self._bots_service is None: if self._bots_service is None:
self._bots_service = BotsService(self._server) self._bots_service = BotsService(self.__grpc_server)
self._bots_service.add_instance(instance_name, instance) self._bots_service.add_instance(instance_name, instance)
...@@ -124,7 +128,7 @@ class BuildGridServer: ...@@ -124,7 +128,7 @@ class BuildGridServer:
instance_name (str): Instance name. instance_name (str): Instance name.
""" """
if self._operations_service is None: if self._operations_service is None:
self._operations_service = OperationsService(self._server) self._operations_service = OperationsService(self.__grpc_server)
self._operations_service.add_instance(instance_name, instance) self._operations_service.add_instance(instance_name, instance)
...@@ -138,7 +142,7 @@ class BuildGridServer: ...@@ -138,7 +142,7 @@ class BuildGridServer:
instance_name (str): Instance name. instance_name (str): Instance name.
""" """
if self._reference_storage_service is None: if self._reference_storage_service is None:
self._reference_storage_service = ReferenceStorageService(self._server) self._reference_storage_service = ReferenceStorageService(self.__grpc_server)
self._reference_storage_service.add_instance(instance_name, instance) self._reference_storage_service.add_instance(instance_name, instance)
...@@ -152,7 +156,7 @@ class BuildGridServer: ...@@ -152,7 +156,7 @@ class BuildGridServer:
instance_name (str): Instance name. instance_name (str): Instance name.
""" """
if self._action_cache_service is None: if self._action_cache_service is None:
self._action_cache_service = ActionCacheService(self._server) self._action_cache_service = ActionCacheService(self.__grpc_server)
self._action_cache_service.add_instance(instance_name, instance) self._action_cache_service.add_instance(instance_name, instance)
...@@ -166,7 +170,7 @@ class BuildGridServer: ...@@ -166,7 +170,7 @@ class BuildGridServer:
instance_name (str): Instance name. instance_name (str): Instance name.
""" """
if self._cas_service is None: if self._cas_service is None:
self._cas_service = ContentAddressableStorageService(self._server) self._cas_service = ContentAddressableStorageService(self.__grpc_server)
self._cas_service.add_instance(instance_name, instance) self._cas_service.add_instance(instance_name, instance)
...@@ -180,6 +184,6 @@ class BuildGridServer: ...@@ -180,6 +184,6 @@ class BuildGridServer:
instance_name (str): Instance name. instance_name (str): Instance name.
""" """
if self._bytestream_service is None: if self._bytestream_service is None:
self._bytestream_service = ByteStreamService(self._server) self._bytestream_service = ByteStreamService(self.__grpc_server)
self._bytestream_service.add_instance(instance_name, instance) self._bytestream_service.add_instance(instance_name, instance)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment