Clean shutdown

This commit is contained in:
Johan B.W. de Vries 2023-04-14 14:48:28 +02:00
parent 0d0af0e728
commit b06a604fd4

View File

@ -1,4 +1,4 @@
from typing import Callable, Dict, List, Optional, Tuple from typing import Callable, Dict, List, Optional, Tuple, Union
import datetime import datetime
import functools import functools
@ -19,11 +19,18 @@ from .runners.base import RunnerInterface
from .runners.wasmtime import WasmTimeRunner from .runners.wasmtime import WasmTimeRunner
def runner_thread(runner: RunnerInterface, queue: Queue[MethodCall]) -> None: class ShuttingDown():
pass
def runner_thread(runner: RunnerInterface, queue: Queue[Union[MethodCall, ShuttingDown]]) -> None:
while True: while True:
try: try:
call = queue.get(block=True, timeout=1) call = queue.get(block=True, timeout=1)
except Empty: except Empty:
continue
if isinstance(call, ShuttingDown):
break break
runner.do_call(call) runner.do_call(call)
@ -55,12 +62,12 @@ class LocalhostRunner(RunnerInterface):
class LocalhostServiceDiscovery(ServiceDiscoveryInterface): class LocalhostServiceDiscovery(ServiceDiscoveryInterface):
services: Dict[str, Tuple[Service, Queue[MethodCall]]] services: Dict[str, Tuple[Service, Queue[Union[MethodCall, ShuttingDown]]]]
def __init__(self) -> None: def __init__(self) -> None:
self.services = {} self.services = {}
def register_service(self, service: Service, queue: Queue[MethodCall]) -> None: def register_service(self, service: Service, queue: Queue[Union[MethodCall, ShuttingDown]]) -> None:
self.services[service.name] = (service, queue, ) self.services[service.name] = (service, queue, )
def find_service(self, name: str) -> Optional[Service]: def find_service(self, name: str) -> Optional[Service]:
@ -105,9 +112,9 @@ def main() -> int:
# TODO: Replace the stuff below with the loading from the example state # TODO: Replace the stuff below with the loading from the example state
localhost_queue: Queue[MethodCall] = Queue() localhost_queue: Queue[Union[MethodCall, ShuttingDown]] = Queue()
echo_client_queue: Queue[MethodCall] = Queue() echo_client_queue: Queue[Union[MethodCall, ShuttingDown]] = Queue()
echo_server_queue: Queue[MethodCall] = Queue() echo_server_queue: Queue[Union[MethodCall, ShuttingDown]] = Queue()
service_discovery = LocalhostServiceDiscovery() service_discovery = LocalhostServiceDiscovery()
method_call_router = LocalhostMethodCallRouter(config.worker_config, service_discovery) method_call_router = LocalhostMethodCallRouter(config.worker_config, service_discovery)
@ -148,7 +155,15 @@ def main() -> int:
echo_client_thread.start() echo_client_thread.start()
echo_server_thread.start() echo_server_thread.start()
time.sleep(3) try:
while 1:
time.sleep(1)
except KeyboardInterrupt:
pass
localhost_queue.put(ShuttingDown())
echo_client_queue.put(ShuttingDown())
echo_server_queue.put(ShuttingDown())
return 0 return 0