Skip to content

Commit

Permalink
Raise SocketError on UpdateHandler socket trouble
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed May 24, 2024
1 parent d86c867 commit a92d7cb
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/zinolib/controllers/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
For updates, either regularly use ``get_events()`` or utilize the UpdateHandler::
> updater = UpdateHandler(event_manager)
> updater.connect()
> updated = updater.get_event_update()
The updater is unique per authenticated user and is only available after login.
Expand Down Expand Up @@ -127,16 +128,30 @@ class UpdateType(StrEnum):
class UpdateError(Zino1Error):
pass

class SocketError(UpdateError):
pass

def __init__(self, manager, autoremove=False):
self._connected = False
if not manager.is_authenticated:
msg = "Cannot initiate update handler, not authenticated"
raise self.UpdateError(msg)
self.manager = manager
if not self.manager.session.push:
self.manager.connect_push_channel()
self.events = manager.events
self.autoremove = autoremove

def connect(self):
if not self.manager.session.push:
self.manager.connect_push_channel()
self.check_connection()

Check warning on line 146 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L144-L146

Added lines #L144 - L146 were not covered by tests

def check_connection(self):
if self.manager.session.push._sock.fileno() >= 0:
self._connected = True
return True
self._connected = False
raise self.SocektError("Push socket reports failure, fileno = -1")

Check warning on line 153 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L149-L153

Added lines #L149 - L153 were not covered by tests

def get_event_update(self):
"""
Fetches one update for a single event and runs the appropriate handler
Expand All @@ -150,6 +165,7 @@ def get_event_update(self):
Run in a loop/every N seconds for a lightweight way to update the event
list
"""
self.check_connection()

Check warning on line 168 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L168

Added line #L168 was not covered by tests
update = self.manager.session.push.poll()
if not update:
return False
Expand Down Expand Up @@ -268,10 +284,16 @@ def authenticate(session, username=None, password=None):
return session

@staticmethod
def close_session(session):
def close_push_channel(session):
if hasattr(session.push, '_sock'):
session.push._sock.close()
session.push = None

Check warning on line 290 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L290

Added line #L290 was not covered by tests

@classmethod
def close_session(cls, session):
cls.close_push_channel()

Check warning on line 294 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L294

Added line #L294 was not covered by tests
session.request.close()
session.request = None

Check warning on line 296 in src/zinolib/controllers/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/controllers/zino1.py#L296

Added line #L296 was not covered by tests
return None


Expand Down

0 comments on commit a92d7cb

Please sign in to comment.