-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_helpers.py
426 lines (360 loc) · 12.7 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import asyncio
import functools
import time
from abc import ABC
from abc import ABCMeta
from abc import abstractmethod
from typing import Any
from typing import Dict
from typing import Union
from urllib.parse import urljoin
import aio_pika
from httpx import AsyncClient
from httpx import Client as SyncClient
from ipfs_client.main import AsyncIPFSClient
from pydantic import BaseModel
from redis import asyncio as aioredis
from snapshotter.settings.config import settings
from snapshotter.utils.default_logger import default_logger
from snapshotter.utils.models.data_models import SnapshotterIssue
from snapshotter.utils.models.data_models import TelegramEpochProcessingReportMessage
from snapshotter.utils.models.message_models import EpochBase
from snapshotter.utils.models.message_models import PowerloomCalculateAggregateMessage
from snapshotter.utils.models.message_models import PowerloomDelegateWorkerRequestMessage
from snapshotter.utils.models.message_models import PowerloomSnapshotProcessMessage
from snapshotter.utils.models.message_models import PowerloomSnapshotSubmittedMessage
from snapshotter.utils.redis.redis_keys import callback_last_sent_by_issue
from snapshotter.utils.rpc import RpcHelper
# Setup logger for this module
helper_logger = default_logger.bind(module='Callback|Helpers')
async def get_rabbitmq_robust_connection_async():
"""
Returns a robust connection to RabbitMQ server using the settings specified in the configuration file.
Returns:
aio_pika.Connection: A robust connection to RabbitMQ.
"""
return await aio_pika.connect_robust(
host=settings.rabbitmq.host,
port=settings.rabbitmq.port,
virtual_host='/',
login=settings.rabbitmq.user,
password=settings.rabbitmq.password,
)
async def get_rabbitmq_basic_connection_async():
"""
Returns an async connection to RabbitMQ using the settings specified in the config file.
Returns:
aio_pika.Connection: An async connection to RabbitMQ.
"""
return await aio_pika.connect(
host=settings.rabbitmq.host,
port=settings.rabbitmq.port,
virtual_host='/',
login=settings.rabbitmq.user,
password=settings.rabbitmq.password,
)
async def get_rabbitmq_channel(connection_pool) -> aio_pika.Channel:
"""
Acquires a connection from the connection pool and returns a channel object for RabbitMQ communication.
Args:
connection_pool (aio_pika.pool.Pool): An instance of the connection pool.
Returns:
aio_pika.Channel: An instance of the RabbitMQ channel.
"""
async with connection_pool.acquire() as connection:
return await connection.channel()
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:
# Log the exception with full traceback if debug_mode is True
if settings.logs.debug_mode:
helper_logger.opt(exception=settings.logs.debug_mode).error(
'Exception while sending callback or notification: {}', e,
)
else:
helper_logger.error('Exception while sending callback or notification: {}', e)
else:
helper_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:
# Log the exception with full traceback if debug_mode is True
if settings.logs.debug_mode:
helper_logger.opt(exception=settings.logs.debug_mode).error(
'Exception while sending callback or notification: {}', exc,
)
else:
helper_logger.error('Exception while sending callback or notification: {}', exc)
else:
helper_logger.debug('Callback or notification result:{}', result)
async def send_failure_notifications_async(
client: AsyncClient,
message: SnapshotterIssue,
redis_conn: aioredis.Redis,
):
"""
Sends failure notifications asynchronously to the configured reporting services.
Args:
client (AsyncClient): The async HTTP client to use for sending notifications.
message (SnapshotterIssue): The message to send as notification.
redis_conn (aioredis.Redis): Redis connection for rate limiting.
Returns:
None
"""
# Check if the last notification was sent within the minimum reporting interval
caching_task = []
if settings.reporting.min_reporting_interval > 0:
last_sent_timestamp = await redis_conn.get(
callback_last_sent_by_issue(message.issueType),
)
if not last_sent_timestamp:
caching_task.append(
redis_conn.set(
name=callback_last_sent_by_issue(message.issueType),
value=message.timeOfReporting,
ex=settings.reporting.min_reporting_interval,
),
)
else:
helper_logger.debug(
'Not sending failure notification for {} because the last notification was sent within the minimum reporting interval',
message.issueType,
)
return
notification_tasks = []
if settings.reporting.service_url:
f = asyncio.create_task(
client.post(
url=urljoin(settings.reporting.service_url, '/reportIssue'),
json=message.dict(),
),
)
f.add_done_callback(misc_notification_callback_result_handler)
notification_tasks.append(f)
if settings.reporting.slack_url:
f = asyncio.create_task(
client.post(
url=settings.reporting.slack_url,
json=message.dict(),
),
)
f.add_done_callback(misc_notification_callback_result_handler)
notification_tasks.append(f)
if notification_tasks:
notification_tasks = caching_task + notification_tasks
await asyncio.gather(*notification_tasks)
def send_failure_notifications_sync(
client: SyncClient,
message: SnapshotterIssue,
redis_conn: aioredis.Redis,
):
"""
Sends failure notifications synchronously to the reporting service, Slack, and Telegram.
Args:
client (SyncClient): The HTTP client to use for sending notifications.
message (SnapshotterIssue): The message to send as notification.
Returns:
None
"""
# Send notification to the reporting service if configured
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)
# Send notification to Slack if configured
if settings.reporting.slack_url:
f = functools.partial(
client.post,
url=settings.reporting.slack_url,
json=message.dict(),
)
sync_notification_callback_result_handler(f)
# Send notification to Telegram if configured
if settings.reporting.telegram_url and settings.reporting.telegram_chat_id:
reporting_message = TelegramEpochProcessingReportMessage(
chatId=settings.reporting.telegram_chat_id,
slotId=settings.slot_id,
issue=message,
)
f = functools.partial(
client.post,
url=urljoin(settings.reporting.telegram_url, '/reportEpochProcessingIssue'),
json=reporting_message.dict(),
)
sync_notification_callback_result_handler(f)
class GenericProcessorSnapshot(ABC):
"""
Abstract base class for snapshot processors.
"""
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
epoch: PowerloomSnapshotProcessMessage,
redis: aioredis.Redis,
rpc_helper: RpcHelper,
):
"""
Abstract method to compute the snapshot.
Args:
epoch (PowerloomSnapshotProcessMessage): The epoch message.
redis (aioredis.Redis): Redis connection.
rpc_helper (RpcHelper): RPC helper instance.
"""
pass
class GenericPreloader(ABC):
"""
Abstract base class for preloaders.
"""
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
epoch: EpochBase,
redis_conn: aioredis.Redis,
rpc_helper: RpcHelper,
):
"""
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
class GenericDelegatorPreloader(GenericPreloader):
"""
Abstract base class for delegator preloaders.
"""
_epoch: EpochBase
_channel: aio_pika.abc.AbstractChannel
_exchange: aio_pika.abc.AbstractExchange
_q_obj: aio_pika.abc.AbstractQueue
_consumer_tag: str
_redis_conn: aioredis.Redis
_task_type: str
_epoch_id: int
_preload_successful_event: asyncio.Event
_awaited_delegated_response_ids: set
_collected_response_objects: Dict[int, Dict[str, Dict[Any, Any]]]
_request_id_query_obj_map: Dict[int, Any]
@abstractmethod
async def _on_delegated_responses_complete(self):
"""
Abstract method called when all delegated responses are complete.
"""
pass
@abstractmethod
async def _on_filter_worker_response_message(
self,
message: aio_pika.abc.AbstractIncomingMessage,
):
"""
Abstract method to handle filter worker response messages.
Args:
message (aio_pika.abc.AbstractIncomingMessage): The incoming message.
"""
pass
@abstractmethod
async def _handle_filter_worker_response_message(self, message_body: bytes):
"""
Abstract method to handle filter worker response message body.
Args:
message_body (bytes): The message body.
"""
pass
@abstractmethod
async def _periodic_awaited_responses_checker(self):
"""
Abstract method to periodically check for awaited responses.
"""
pass
class GenericDelegateProcessor(ABC):
"""
Abstract base class for delegate processors.
"""
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
msg_obj: PowerloomDelegateWorkerRequestMessage,
redis_conn: aioredis.Redis,
rpc_helper: RpcHelper,
):
"""
Abstract method to compute delegate processing.
Args:
msg_obj (PowerloomDelegateWorkerRequestMessage): The delegate worker request message.
redis_conn (aioredis.Redis): Redis connection.
rpc_helper (RpcHelper): RPC helper instance.
"""
pass
class GenericProcessorAggregate(ABC):
"""
Abstract base class for aggregate processors.
"""
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
msg_obj: Union[PowerloomSnapshotSubmittedMessage, PowerloomCalculateAggregateMessage],
redis: aioredis.Redis,
rpc_helper: RpcHelper,
anchor_rpc_helper: RpcHelper,
ipfs_reader: AsyncIPFSClient,
protocol_state_contract,
project_id: str,
):
"""
Abstract method to compute aggregate processing.
Args:
msg_obj (Union[PowerloomSnapshotSubmittedMessage, PowerloomCalculateAggregateMessage]): The message object.
redis (aioredis.Redis): Redis connection.
rpc_helper (RpcHelper): RPC helper instance.
anchor_rpc_helper (RpcHelper): Anchor RPC helper instance.
ipfs_reader (AsyncIPFSClient): IPFS reader instance.
protocol_state_contract: Protocol state contract.
project_id (str): Project ID.
"""
pass
class PreloaderAsyncFutureDetails(BaseModel):
"""
Pydantic model for preloader async future details.
"""
obj: GenericPreloader
future: asyncio.Task
class Config:
arbitrary_types_allowed = True