-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcallback_helpers.py
254 lines (206 loc) · 7.58 KB
/
callback_helpers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import asyncio
import functools
from abc import ABC
from abc import ABCMeta
from abc import abstractmethod
from urllib.parse import urljoin
from httpx import AsyncClient
from httpx import Client as SyncClient
from ipfs_client.main import AsyncIPFSClient
from snapshotter.settings.config import settings
from snapshotter.utils.default_logger import logger
from snapshotter.utils.models.data_models import PreloaderResult
from snapshotter.utils.models.message_models import EpochBase
from snapshotter.utils.models.message_models import SnapshotProcessMessage
from snapshotter.utils.models.message_models import SnapshotterIssue
from snapshotter.utils.models.message_models import TelegramEpochProcessingReportMessage
from snapshotter.utils.models.message_models import TelegramMessage
from snapshotter.utils.models.message_models import TelegramSnapshotterReportMessage
from snapshotter.utils.rpc import RpcHelper
# setup logger
helper_logger = logger.bind(module='Callback|Helpers')
def misc_notification_callback_result_handler(fut: asyncio.Future):
"""
Handles the result of a callback or notification.
Args:
fut (asyncio.Future): The future object representing the callback or notification.
Returns:
None
"""
try:
r = fut.result()
except Exception as e:
if settings.logs.trace_enabled:
logger.opt(exception=True).error(
'Exception while sending callback or notification: {}', e,
)
else:
logger.error('Exception while sending callback or notification: {}', e)
else:
logger.debug('Callback or notification result:{}', r)
def sync_notification_callback_result_handler(f: functools.partial):
"""
Handles the result of a synchronous notification callback.
Args:
f (functools.partial): The function to handle.
Returns:
None
"""
try:
result = f()
except Exception as exc:
if settings.logs.trace_enabled:
logger.opt(exception=True).error(
'Exception while sending callback or notification: {}', exc,
)
else:
logger.error('Exception while sending callback or notification: {}', exc)
else:
logger.debug('Callback or notification result:{}', result)
async def send_failure_notifications_async(client: AsyncClient, message: SnapshotterIssue):
"""
Sends failure notifications to the configured reporting services.
Args:
client (AsyncClient): The async HTTP client to use for sending notifications.
message (SnapshotterIssue): The message to send to the reporting services.
Returns:
None
"""
if settings.reporting.service_url:
f = asyncio.ensure_future(
client.post(
url=urljoin(settings.reporting.service_url, '/reportIssue'),
json=message.dict(),
),
)
f.add_done_callback(misc_notification_callback_result_handler)
if settings.reporting.slack_url:
f = asyncio.ensure_future(
client.post(
url=settings.reporting.slack_url,
json=message.dict(),
),
)
f.add_done_callback(misc_notification_callback_result_handler)
def send_failure_notifications_sync(client: SyncClient, message: SnapshotterIssue):
"""
Sends failure notifications synchronously to to the configured reporting services.
Args:
client (SyncClient): The HTTP client to use for sending notifications.
message (SnapshotterIssue): The message to send to the reporting services.
Returns:
None
"""
if settings.reporting.service_url:
f = functools.partial(
client.post,
url=urljoin(settings.reporting.service_url, '/reportIssue'),
json=message.dict(),
)
sync_notification_callback_result_handler(f)
if settings.reporting.slack_url:
f = functools.partial(
client.post,
url=settings.reporting.slack_url,
json=message.dict(),
)
sync_notification_callback_result_handler(f)
async def send_telegram_notification_async(client: AsyncClient, message: TelegramMessage):
"""
Sends an asynchronous Telegram notification for reporting issues.
This function checks if Telegram reporting is configured, and then sends the appropriate
message based on its type (epoch processing issue or snapshotter issue).
Args:
client (AsyncClient): The async HTTP client to use for sending notifications.
message (TelegramMessage): The message to send as a Telegram notification.
Returns:
None
"""
if not settings.reporting.telegram_url or not settings.reporting.telegram_chat_id:
return
if isinstance(message, TelegramEpochProcessingReportMessage):
endpoint = '/reportEpochProcessingIssue'
elif isinstance(message, TelegramSnapshotterReportMessage):
endpoint = '/reportSnapshotIssue'
else:
helper_logger.error(
f'Unsupported telegram message type: {type(message)} - message not sent',
)
return
f = asyncio.ensure_future(
client.post(
url=urljoin(settings.reporting.telegram_url, endpoint),
json=message.dict(),
),
)
f.add_done_callback(misc_notification_callback_result_handler)
def send_telegram_notification_sync(client: SyncClient, message: TelegramMessage):
"""
Sends a synchronous Telegram notification for reporting issues.
This function checks if Telegram reporting is configured, and then sends the appropriate
message based on its type (epoch processing issue or snapshotter issue).
Args:
client (SyncClient): The synchronous HTTP client to use for sending notifications.
message (TelegramMessage): The message to send as a Telegram notification.
Returns:
None
"""
if not settings.reporting.telegram_url or not settings.reporting.telegram_chat_id:
return
if isinstance(message, TelegramEpochProcessingReportMessage):
endpoint = '/reportEpochProcessingIssue'
elif isinstance(message, TelegramSnapshotterReportMessage):
endpoint = '/reportSnapshotIssue'
else:
helper_logger.error(
f'Unsupported telegram message type: {type(message)} - message not sent',
)
return
f = functools.partial(
client.post,
url=urljoin(settings.reporting.telegram_url, endpoint),
json=message.dict(),
)
sync_notification_callback_result_handler(f)
class GenericProcessor(ABC):
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
msg_obj: SnapshotProcessMessage,
rpc_helper: RpcHelper,
anchor_rpc_helper: RpcHelper,
ipfs_reader: AsyncIPFSClient,
protocol_state_contract,
preloader_results: dict,
):
pass
class GenericPreloader(ABC):
"""
Abstract base class for preloaders.
"""
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
epoch: EpochBase,
rpc_helper: RpcHelper,
) -> PreloaderResult:
"""
Abstract method to compute preload data.
Args:
epoch (EpochBase): The epoch message.
redis_conn (aioredis.Redis): Redis connection.
rpc_helper (RpcHelper): RPC helper instance.
"""
pass
@abstractmethod
async def cleanup(self):
"""
Abstract method to clean up resources.
"""
pass