Skip to content

Commit

Permalink
git - Merge pull request #11 from DinoTools/reconnect
Browse files Browse the repository at this point in the history
Handle closed connection
  • Loading branch information
phibos authored Dec 17, 2024
2 parents 799990d + 482438c commit 2381a0e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions routeros_log_exporter/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from queue import Queue

from librouteros import connect
from librouteros.exceptions import ConnectionClosed

from .exception import ConfigError
from .output import Output
Expand Down Expand Up @@ -191,12 +192,16 @@ def _process_messages(self):
"follow-only": "",
}
)
for message in log_stream:
if self._should_terminate:
return
if message.get(".dead") is True:
continue
self._message_queue.put(LogMessage(fetcher=self, message=message))
try:
for message in log_stream:
if self._should_terminate:
return
if message.get(".dead") is True:
continue
self._message_queue.put(LogMessage(fetcher=self, message=message))
except ConnectionClosed as e:
logger.error(f"Connection to device '{self.hostname}' closed: {str(e)}")
self.disconnect()

@property
def api(self) -> RouterOSApi:
Expand Down Expand Up @@ -229,6 +234,12 @@ def api(self) -> RouterOSApi:
logger.info("Connected")
return self._api

def disconnect(self):
if not self._api:
return
self._api.close()
self._api = None

def handle_signal(self, signal_number):
pass

Expand Down

0 comments on commit 2381a0e

Please sign in to comment.