Skip to content

Commit 4960836

Browse files
committed
CoordinatorTimeoutError and CoordinatorConnectionError as generic error handler
1 parent aebec4f commit 4960836

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/plumpy/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
'KilledError',
88
'PersistenceError',
99
'UnsuccessfulResult',
10+
'CoordinatorConnectionError',
11+
'CoordinatorTimeoutError',
1012
]
1113

1214

@@ -42,3 +44,11 @@ class ClosedError(Exception):
4244

4345
class TaskRejectedError(Exception):
4446
"""A task was rejected by the coordinacor"""
47+
48+
49+
class CoordinatorConnectionError(ConnectionError):
50+
"""Raised when coordinator cannot be connected"""
51+
52+
53+
class CoordinatorTimeoutError(TimeoutError):
54+
"""Raised when communicate with coordinator timeout"""

src/plumpy/processes.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,19 +741,15 @@ def on_entered(self, from_state: Optional[process_states.State]) -> None:
741741
call_with_super_check(self.on_killed)
742742

743743
if self._coordinator and isinstance(self.state, enum.Enum):
744-
# FIXME: this part should be tested first
745-
# FIXME: move all to `coordinator.broadcast()` call and in rmq implement coordinator
746-
from plumpy.rmq.exceptions import CommunicatorChannelInvalidStateError, CommunicatorConnectionClosed
747-
748744
from_label = cast(enum.Enum, from_state.LABEL).value if from_state is not None else None
749745
subject = f'state_changed.{from_label}.{self.state.value}'
750746
self.logger.info('Process<%s>: Broadcasting state change: %s', self.pid, subject)
751747
try:
752748
self._coordinator.broadcast_send(body=None, sender=self.pid, subject=subject)
753-
except (CommunicatorConnectionClosed, CommunicatorChannelInvalidStateError):
749+
except exceptions.CoordinatorConnectionError:
754750
message = 'Process<%s>: no connection available to broadcast state change from %s to %s'
755751
self.logger.warning(message, self.pid, from_label, self.state.value)
756-
except concurrent.futures.TimeoutError:
752+
except exceptions.CoordinatorTimeoutError:
757753
message = 'Process<%s>: sending broadcast of state change from %s to %s timed out'
758754
self.logger.warning(message, self.pid, from_label, self.state.value)
759755

tests/rmq/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
1+
# -*- coding: utf-8 -*-
22
import kiwipy
3+
import concurrent.futures
4+
5+
from plumpy.exceptions import CoordinatorConnectionError, CoordinatorTimeoutError
36

47

58
class RmqCoordinator:
@@ -40,11 +43,19 @@ def broadcast_send(
4043
subject=None,
4144
correlation_id=None,
4245
):
43-
return self._comm.broadcast_send(body, sender, subject, correlation_id)
46+
from aio_pika.exceptions import ChannelInvalidStateError, ConnectionClosed
47+
48+
try:
49+
rsp = self._comm.broadcast_send(body, sender, subject, correlation_id)
50+
except (ChannelInvalidStateError, ConnectionClosed) as exc:
51+
raise CoordinatorConnectionError from exc
52+
except concurrent.futures.TimeoutError as exc:
53+
raise CoordinatorTimeoutError from exc
54+
else:
55+
return rsp
4456

4557
def task_send(self, task, no_reply=False):
4658
return self._comm.task_send(task, no_reply)
4759

4860
def close(self):
4961
self._comm.close()
50-

0 commit comments

Comments
 (0)