-
Notifications
You must be signed in to change notification settings - Fork 208
/
call_back.py
62 lines (49 loc) · 1.56 KB
/
call_back.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import abc
import logging
from typing import List, Optional
LOGGER = logging.getLogger(__name__)
class Callback(object, metaclass=abc.ABCMeta):
"""
A callback interface that expected to fire "on_success" if the operation is successful, else "on_failure" if
operation failed.
"""
@abc.abstractmethod
def on_success(self) -> None:
"""
A call back method that will be called when operation is successful
:return: None
"""
pass
@abc.abstractmethod
def on_failure(self) -> None:
"""
A call back method that will be called when operation failed
:return: None
"""
pass
def notify_callbacks(callbacks: List[Callback], is_success: bool) -> None:
"""
A Utility method that notifies callback. If any callback fails it will still go through all the callbacks,
and raise the last exception it experienced.
:param callbacks:
:param is_success:
:return:
"""
if not callbacks:
LOGGER.info('No callbacks to notify')
return
LOGGER.info('Notifying callbacks')
last_exception: Optional[Exception] = None
for callback in callbacks:
try:
if is_success:
callback.on_success()
else:
callback.on_failure()
except Exception as e:
LOGGER.exception('Failed while notifying callback')
last_exception = e
if last_exception:
raise last_exception