|
9 | 9 | _RETRY_COUNT = 0 |
10 | 10 | _MAX_RETRY_ATTEMPTS = 3 |
11 | 11 |
|
| 12 | + |
12 | 13 | class SQSEvents: |
13 | 14 |
|
14 | | - def __init__(self, logger=None, config=None): |
| 15 | + def __init__(self, logger=None, config=None, profile=None, session=None): |
15 | 16 | # logger |
16 | 17 | self.logger = logger if logger is not None else get_logger() |
17 | 18 | # configurations |
18 | 19 | self.config = config if config is not None else get_config() |
19 | 20 | # last_exception |
20 | 21 | self.exception = None |
| 22 | + # profile |
| 23 | + self.profile = profile if profile is not None else \ |
| 24 | + os.environ['AWS_PROFILE'] if 'AWS_PROFILE' in os.environ else None |
| 25 | + # session |
| 26 | + self.session = session if session is not None else \ |
| 27 | + boto3.session.Session(profile_name=self.profile) |
21 | 28 |
|
22 | 29 | def connect(self, retry=False): |
23 | 30 | global _RETRY_COUNT, _MAX_RETRY_ATTEMPTS |
24 | 31 | connection = None |
25 | 32 | try: |
26 | 33 | endpoint_url = self.config.SQS_ENDPOINT |
27 | | - profile = os.environ['AWS_PROFILE'] if 'AWS_PROFILE' in os.environ else None |
| 34 | + |
28 | 35 | queue_name = os.path.basename(os.environ['APP_QUEUE']) if 'APP_QUEUE' in os.environ else None |
29 | | - self.logger.info('SQSEvents - profile: {}'.format(profile)) |
| 36 | + self.logger.info('SQSEvents - profile: {}'.format(self.profile)) |
30 | 37 | self.logger.info('SQSEvents - endpoint_url: {}'.format(endpoint_url)) |
31 | 38 | self.logger.info('SQSEvents - queue_name: {}'.format(queue_name)) |
32 | 39 | self.logger.info('SQSEvents - self.config.REGION_NAME: {}'.format(self.config.REGION_NAME)) |
33 | 40 |
|
34 | | - if profile: |
35 | | - session = boto3.session.Session(profile_name=profile) |
| 41 | + if self.profile: |
| 42 | + session = self.session |
36 | 43 | connection = session.resource( |
37 | 44 | 'sqs', |
38 | 45 | endpoint_url=endpoint_url, |
@@ -70,10 +77,12 @@ def connect(self, retry=False): |
70 | 77 | if not retry: |
71 | 78 | _RETRY_COUNT += 1 |
72 | 79 | # Fix para tratar diff entre docker/local |
73 | | - if self.config.SQS_ENDPOINT == 'http://0.0.0.0:4566' or self.config.SQS_ENDPOINT == 'http://localstack:4566': |
| 80 | + if self.config.SQS_ENDPOINT == 'http://0.0.0.0:4566' or \ |
| 81 | + self.config.SQS_ENDPOINT == 'http://localstack:4566': |
74 | 82 | old_value = self.config.SQS_ENDPOINT |
75 | 83 | self.config.SQS_ENDPOINT = 'http://localhost:4566' |
76 | | - self.logger.info('Changing the endpoint from {} to {}'.format(old_value, self.config.SQS_ENDPOINT)) |
| 84 | + self.logger.info( |
| 85 | + 'Changing the endpoint from {} to {}'.format(old_value, self.config.SQS_ENDPOINT)) |
77 | 86 | connection = self.connect(retry=True) |
78 | 87 | return connection |
79 | 88 |
|
@@ -166,7 +175,3 @@ def delete_queue(self, queue_name): |
166 | 175 | result = False |
167 | 176 |
|
168 | 177 | return result |
169 | | - |
170 | | - |
171 | | - |
172 | | - |
|
0 commit comments