Skip to content

Commit

Permalink
run_emulator_client retries.
Browse files Browse the repository at this point in the history
  • Loading branch information
amit lissack committed Oct 23, 2021
1 parent b7a53bd commit ac6b618
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
17 changes: 13 additions & 4 deletions api/src/opentrons/hardware_control/emulation/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
from enum import Enum

from opentrons.hardware_control.emulation.module_server import ModuleStatusServer
from opentrons.hardware_control.emulation.parser import Parser
Expand All @@ -11,6 +12,14 @@
logger = logging.getLogger(__name__)


class ModuleType(str, Enum):
"""Module type enumeration."""
Magnetic = "magnetic"
Temperature = "temperature"
Thermocycler = "thermocycler"
Heatershaker = "heatershaker"


class Application:
"""The emulator application."""

Expand All @@ -26,16 +35,16 @@ def __init__(self, settings: Settings) -> None:
parser=Parser(), settings=settings.smoothie
)
self._magdeck = Proxy(
"magdeck", self._status_server, self._settings.magdeck_proxy
ModuleType.Magnetic, self._status_server, self._settings.magdeck_proxy
)
self._temperature = Proxy(
"temperature", self._status_server, self._settings.temperature_proxy
ModuleType.Temperature, self._status_server, self._settings.temperature_proxy
)
self._thermocycler = Proxy(
"thermocycler", self._status_server, self._settings.thermocycler_proxy
ModuleType.Thermocycler, self._status_server, self._settings.thermocycler_proxy
)
self._heatershaker = Proxy(
"heatershaker", self._status_server, self._settings.heatershaker_proxy
ModuleType.Heatershaker, self._status_server, self._settings.heatershaker_proxy
)

async def run(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def on_server_disconnected(self, identifier: str) -> None:
log.exception("Failed to find identifier")

async def run(self) -> None:
""""""
"""Run the server."""
server = await asyncio.start_server(
self._handle_connection, host=self._settings.host, port=self._settings.port
)
Expand Down
42 changes: 38 additions & 4 deletions api/src/opentrons/hardware_control/emulation/run_emulator.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
import asyncio
import logging
from typing import Optional

from opentrons.hardware_control.emulation.abstract_emulator import AbstractEmulator
from opentrons.hardware_control.emulation.connection_handler import ConnectionHandler

log = logging.getLogger(__name__)


async def run_emulator_client(host: str, port: int, emulator: AbstractEmulator) -> None:
"""Run an emulator as a client."""
async def run_emulator_client(host: str, port: int, emulator: AbstractEmulator, retries: int = 3, interval_seconds:float=0.1) -> None:
"""Run an emulator as a client.
Args:
host: Host to connect to.
port: Port to connect on.
emulator: The emulator instance.
retries: Number of retries on a failed connection attempt.
interval_seconds: How long to wait between retries.
Returns:
None
"""
log.info(f"Connecting to {emulator.__class__.__name__} at {host}:{port}")
r, w = await asyncio.open_connection(host, port)

r: Optional[asyncio.StreamReader] = None
w: Optional[asyncio.StreamWriter] = None
for i in range(retries):
try:
r, w = await asyncio.open_connection(host, port)
break
except IOError:
log.error(f"{emulator.__class__.__name__} failed to connect on try {i + 1}. Retrying in {interval_seconds} seconds.")
await asyncio.sleep(interval_seconds)

if r is None or w is None:
raise IOError(f"Failed to connect to {emulator.__class__.__name__} at {host}:{port} after {retries} retries.")

connection = ConnectionHandler(emulator)
await connection(r, w)


async def run_emulator_server(host: str, port: int, emulator: AbstractEmulator) -> None:
"""Run an emulator as a server."""
"""Run an emulator as a server.
Args:
host: Host to listen on.
port: Port to listen on.
emulator: Emaulator instance.
Returns:
None
"""
log.info(f"Starting {emulator.__class__.__name__} at {host}:{port}")
server = await asyncio.start_server(ConnectionHandler(emulator), host, port)
await server.serve_forever()

0 comments on commit ac6b618

Please sign in to comment.