1
+ import asyncio
2
+ import logging
3
+
4
+ from aioamqp .exceptions import AioamqpException
1
5
from sage_utils .amqp .base import AmqpWorker
2
6
from sage_utils .amqp .clients import RpcAmqpClient
3
7
4
8
9
+ LOGGER = logging .getLogger (__name__ )
10
+
11
+
5
12
class BaseRegisterWorker (AmqpWorker ):
6
13
"""
7
14
Base class for implementing worker which is registering a new
8
15
microservice in Open Matchmaking platform.
9
16
"""
17
+ MAX_RETRIES = 5
18
+ RETRY_TIMEOUT = 10
19
+
10
20
REQUEST_QUEUE_NAME = "auth.microservices.register"
11
21
REQUEST_EXCHANGE_NAME = "open-matchmaking.direct"
12
22
RESPONSE_EXCHANGE_NAME = "open-matchmaking.responses.direct"
@@ -24,8 +34,31 @@ async def run(self, *args, **kwargs):
24
34
response_queue = '' ,
25
35
response_exchange = self .RESPONSE_EXCHANGE_NAME
26
36
)
27
- response = await client .send (self .get_microservice_data (self .app ))
28
37
29
- assert 'error' not in response .keys (), response ['error' ]
30
- assert 'content' in response .keys ()
31
- assert response ['content' ] == 'OK'
38
+ is_registered = False
39
+ microservice_data = self .get_microservice_data (self .app )
40
+ for _ in range (self .MAX_RETRIES ):
41
+ try :
42
+ response = await client .send (
43
+ payload = microservice_data ,
44
+ consume_timeout = self .RETRY_TIMEOUT
45
+ )
46
+
47
+ if 'error' in response .keys ():
48
+ LOGGER .error ("Received validation errors: {}" .format (response ['error' ]))
49
+ else :
50
+ assert 'content' in response .keys ()
51
+ assert response ['content' ] == 'OK'
52
+ is_registered = True
53
+
54
+ break
55
+ except (AioamqpException , TimeoutError ):
56
+ LOGGER .error (
57
+ "Can't receive a response because the Auth/Auth microservice isn't "
58
+ "responding or the required queues and the exchanges aren't created. "
59
+ "Retry after {} second(s)." .format (self .RETRY_TIMEOUT )
60
+ )
61
+ await asyncio .sleep (self .RETRY_TIMEOUT , loop = self .app .loop )
62
+
63
+ if not is_registered :
64
+ raise ConnectionError ('Occurred an error during registering microservice.' )
0 commit comments