Skip to content

Commit

Permalink
Log catched exception in close in global_discovery_socket.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tkilias committed Jul 14, 2023
1 parent 403ec9e commit dfaa06d
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import socket
import time
from typing import Optional

import structlog
from structlog.typing import FilteringBoundLogger

from exasol_advanced_analytics_framework.udf_communication.ip_address import IPAddress, Port

NANO_SECOND = 10 ** -9

LOGGER: FilteringBoundLogger = structlog.getLogger()


class GlobalDiscoverySocket:

def __init__(self, ip_address: IPAddress, port: Port):
self._port = port
self._ip_address = ip_address
self._logger = LOGGER.bind(
ip_address=ip_address.dict(),
port=port.dict()
)
self._logger.info("create")
self._udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

def bind(self):
self._logger.info("bind")
self._udp_socket.bind((self._ip_address.ip_address, self._port.port))

def send(self, message: bytes):
self._logger.debug("send", message=message)
self._udp_socket.sendto(message, (self._ip_address.ip_address, self._port.port))

def recvfrom(self, timeout_in_seconds: float) -> bytes:
Expand All @@ -29,13 +39,15 @@ def recvfrom(self, timeout_in_seconds: float) -> bytes:
adjusted_timeout = timeout_in_seconds + NANO_SECOND
self._udp_socket.settimeout(adjusted_timeout)
data = self._udp_socket.recv(1024)
self._logger.debug("recvfrom", data=data)
return data

def close(self):
self._logger.info("close")
try:
self._udp_socket.close()
except:
pass
except Exception as e:
self._logger.exception("Catched exception during self._udp_socket.close")

def __del__(self):
self.close()

0 comments on commit dfaa06d

Please sign in to comment.