-
Notifications
You must be signed in to change notification settings - Fork 7
/
callback_helpers.py
291 lines (236 loc) · 7.87 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
import asyncio
import functools
from abc import ABC
from abc import ABCMeta
from abc import abstractmethod
from abc import abstractproperty
from typing import Any
from typing import Dict
from typing import Union
from urllib.parse import urljoin
import aio_pika
import loguru._logger
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 logger
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.rpc import RpcHelper
# setup logger
helper_logger = logger.bind(module='Powerloom|Callback|Helpers')
async def get_rabbitmq_robust_connection_async():
"""
Returns a robust connection to RabbitMQ server using the settings specified in the configuration file.
"""
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.
:return: 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: An instance of `aio_pika.pool.Pool`.
Returns:
An instance of `aio_pika.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:
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: BaseModel):
"""
Sends failure notifications to the configured reporting services.
Args:
client (AsyncClient): The async HTTP client to use for sending notifications.
message (BaseModel): The message to send as notification.
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: BaseModel):
"""
Sends failure notifications synchronously to the reporting service and/or Slack.
Args:
client (SyncClient): The HTTP client to use for sending notifications.
message (BaseModel): The message to send as notification.
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)
class GenericProcessorSnapshot(ABC):
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractproperty
def transformation_lambdas(self):
pass
@abstractmethod
async def compute(
self,
epoch: PowerloomSnapshotProcessMessage,
redis_conn: aioredis.Redis,
rpc_helper: RpcHelper,
):
pass
class GenericPreloader(ABC):
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
epoch: EpochBase,
redis_conn: aioredis.Redis,
rpc_helper: RpcHelper,
):
pass
@abstractmethod
async def cleanup(self):
pass
class GenericDelegatorPreloader(GenericPreloader):
_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]]]
_logger: loguru._logger.Logger
_request_id_query_obj_map: Dict[int, Any]
@abstractmethod
async def _on_delegated_responses_complete(self):
pass
@abstractmethod
async def _on_filter_worker_response_message(
self,
message: aio_pika.abc.AbstractIncomingMessage,
):
pass
@abstractmethod
async def _handle_filter_worker_response_message(self, message_body: bytes):
pass
@abstractmethod
async def _periodic_awaited_responses_checker(self):
pass
class GenericDelegateProcessor(ABC):
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractmethod
async def compute(
self,
msg_obj: PowerloomDelegateWorkerRequestMessage,
redis_conn: aioredis.Redis,
rpc_helper: RpcHelper,
):
pass
class GenericProcessorAggregate(ABC):
__metaclass__ = ABCMeta
def __init__(self):
pass
@abstractproperty
def transformation_lambdas(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,
):
pass