-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnats_service.py
241 lines (214 loc) · 8.6 KB
/
nats_service.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
import asyncio
import time
from contextlib import asynccontextmanager
from datetime import datetime, timedelta
from typing import Optional
import orjson
from nats.errors import BadSubscriptionError, Error, TimeoutError
from nats.js.api import ConsumerConfig, DeliverPolicy
from nats.js.client import JetStreamContext
from nats.js.errors import FetchTimeoutError
from tenacity import (
RetryCallState,
retry,
retry_if_exception_type,
stop_never,
wait_exponential,
)
from shared.constants import (
NATS_STATE_STREAM,
NATS_STATE_SUBJECT,
SSE_LOOK_BACK,
SSE_TIMEOUT,
)
from shared.log_config import get_logger
from shared.models.webhook_events import CloudApiWebhookEventGeneric
logger = get_logger(__name__)
class NatsEventsProcessor:
"""
Class to handle processing of NATS events. Calling the process_events method will
subscribe to the NATS server and return an async generator that will yield events
"""
def __init__(self, jetstream: JetStreamContext):
self.js_context: JetStreamContext = jetstream
async def _subscribe(
self,
*,
group_id: Optional[str] = None,
wallet_id: str,
topic: str,
state: str,
start_time: str,
) -> JetStreamContext.PullSubscription:
bound_logger = logger.bind(
body={
"wallet_id": wallet_id,
"group_id": group_id,
"topic": topic,
"state": state,
"start_time": start_time,
}
)
group_id = group_id or "*"
subscribe_kwargs = {
"subject": f"{NATS_STATE_SUBJECT}.{group_id}.{wallet_id}.{topic}.{state}",
"stream": NATS_STATE_STREAM,
}
config = ConsumerConfig(
deliver_policy=DeliverPolicy.BY_START_TIME,
opt_start_time=start_time,
)
def _retry_log(retry_state: RetryCallState):
"""Custom logging for retry attempts."""
if retry_state.outcome.failed:
exception = retry_state.outcome.exception()
bound_logger.warning(
"Retry attempt {} failed due to {}: {}",
retry_state.attempt_number,
type(exception).__name__,
exception,
)
# This is a custom retry decorator that will retry on TimeoutError
# and wait exponentially up to a max of 16 seconds between retries indefinitely
@retry(
retry=retry_if_exception_type(TimeoutError),
wait=wait_exponential(multiplier=1, max=16),
after=_retry_log,
stop=stop_never,
)
async def pull_subscribe():
try:
bound_logger.trace("Attempting to subscribe to JetStream")
subscription = await self.js_context.pull_subscribe(
config=config, **subscribe_kwargs
)
bound_logger.debug("Successfully subscribed to JetStream")
return subscription
except BadSubscriptionError as e:
bound_logger.error("BadSubscriptionError subscribing to NATS: {}", e)
raise
except Error as e:
bound_logger.error("Error subscribing to NATS: {}", e)
raise
try:
return await pull_subscribe()
except Exception:
bound_logger.exception("An exception occurred subscribing to NATS")
raise
@asynccontextmanager
async def process_events(
self,
*,
group_id: Optional[str] = None,
wallet_id: str,
topic: str,
state: str,
stop_event: asyncio.Event,
duration: Optional[int] = None,
look_back: Optional[int] = None,
):
duration = duration or SSE_TIMEOUT
look_back = look_back or SSE_LOOK_BACK
bound_logger = logger.bind(
body={
"wallet_id": wallet_id,
"group_id": group_id,
"topic": topic,
"state": state,
"duration": duration,
"look_back": look_back,
}
)
bound_logger.debug("Processing events")
# Get the current time
current_time = datetime.now()
# Subtract look_back time from the current time
look_back_time = current_time - timedelta(seconds=look_back)
# Format the time in the required format
start_time = look_back_time.isoformat(timespec="milliseconds") + "Z"
async def event_generator(*, subscription: JetStreamContext.PullSubscription):
try:
end_time = time.time() + duration
while not stop_event.is_set():
remaining_time = end_time - time.time()
bound_logger.trace("Remaining time: {}", remaining_time)
if remaining_time <= 0:
bound_logger.debug("Timeout reached")
stop_event.set()
break
try:
messages = await subscription.fetch(
batch=5, timeout=0.5, heartbeat=0.2
)
for message in messages:
event = orjson.loads(message.data)
bound_logger.trace("Received event: {}", event)
yield CloudApiWebhookEventGeneric(**event)
await message.ack()
except FetchTimeoutError:
# Fetch timeout, continue
bound_logger.trace("Timeout fetching messages continuing...")
await asyncio.sleep(0.1)
except TimeoutError:
# Timeout error, resubscribe
bound_logger.warning(
"Subscription lost connection, attempting to resubscribe..."
)
try:
await subscription.unsubscribe()
except BadSubscriptionError as e:
# If we can't unsubscribe, log the error and continue
bound_logger.warning(
"BadSubscriptionError unsubscribing from NATS: {}", e
)
subscription = await self._subscribe(
group_id=group_id,
wallet_id=wallet_id,
topic=topic,
state=state,
start_time=start_time,
)
bound_logger.debug("Successfully resubscribed to NATS.")
except Exception: # pylint: disable=W0718
bound_logger.exception("Unexpected error in event generator")
stop_event.set()
raise
except asyncio.CancelledError:
bound_logger.debug("Event generator cancelled")
stop_event.set()
subscription = None
try:
subscription = await self._subscribe(
group_id=group_id,
wallet_id=wallet_id,
topic=topic,
state=state,
start_time=start_time,
)
yield event_generator(subscription=subscription)
except Exception as e: # pylint: disable=W0718
bound_logger.exception("Unexpected error processing events")
raise e
finally:
if subscription:
try:
bound_logger.trace("Closing subscription...")
await subscription.unsubscribe()
bound_logger.debug("Subscription closed")
except BadSubscriptionError as e:
bound_logger.warning(
"BadSubscriptionError unsubscribing from NATS: {}", e
)
async def check_jetstream(self):
try:
account_info = await self.js_context.account_info()
is_working = account_info.streams > 0
logger.trace("JetStream check completed. Is working: {}", is_working)
return {
"is_working": is_working,
"streams_count": account_info.streams,
"consumers_count": account_info.consumers,
}
except Exception: # pylint: disable=W0718
logger.exception("Caught exception while checking jetstream status")
return {"is_working": False}