introduce an ApplicationConfig module to manage the environment configs in one location

This commit is contained in:
Adrian Manteza 2022-03-17 20:33:48 +00:00
parent 2410e34c05
commit fede094eeb
4 changed files with 29 additions and 15 deletions

View File

@ -1,17 +1,18 @@
import boto3 import boto3
import os
import json import json
from lib.application_configs import ApplicationConfigs
session = boto3.Session( session = boto3.Session(
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'], aws_access_key_id=ApplicationConfigs.aws_access_key_id,
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY']) aws_secret_access_key=ApplicationConfigs.aws_secret_key)
s3 = session.resource('s3', s3 = session.resource('s3',
region_name=os.environ['AWS_REGION']) region_name=ApplicationConfigs.region_name,
# endpoint_url=os.environ['ENDPOINT_URL']) endpoint_url=ApplicationConfigs.endpoint_url)
sqs = session.client('sqs', sqs = session.client('sqs',
region_name=os.environ['AWS_REGION']) region_name=ApplicationConfigs.region_name,
# endpoint_url=os.environ['ENDPOINT_URL']) endpoint_url=ApplicationConfigs.endpoint_url)
def get_key_from_message(body): def get_key_from_message(body):

View File

@ -0,0 +1,11 @@
import os
from pydantic.dataclasses import dataclass
@dataclass
class ApplicationConfigs():
region_name = os.environ.get('AWS_REGION', 'ca-central-1')
aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID', '')
aws_secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY', '')
sqs_queue = os.environ.get('SQS_QUEUE', '')
endpoint_url = os.environ.get('ENDPOINT_URL', '')
s3_processed_bucket = os.environ.get('S3_PROCESSED_BUCKET', 'measure-local-solver-processed')

View File

@ -1,5 +1,6 @@
import os, sys, logging import os, sys, logging
from lib.application_configs import ApplicationConfigs
from services.loft_service import LoftService from services.loft_service import LoftService
from helpers import aws_helper from helpers import aws_helper
@ -27,12 +28,12 @@ def main():
logging.info('Starting Solver Service (v1.1.4)...') logging.info('Starting Solver Service (v1.1.4)...')
listener = ServiceListener( listener = ServiceListener(
None, None,
os.environ['SQS_QUEUE'], ApplicationConfigs.sqs_queue,
create_queue=False, create_queue=False,
region_name=os.environ['AWS_REGION'], region_name=ApplicationConfigs.region_name,
aws_access_key=os.environ['AWS_ACCESS_KEY_ID'], aws_access_key=ApplicationConfigs.aws_access_key_id,
aws_secret_key=os.environ['AWS_SECRET_ACCESS_KEY']) aws_secret_key=ApplicationConfigs.aws_secret_key,
# endpoint_url=os.environ['ENDPOINT_URL']) endpoint_url=ApplicationConfigs.endpoint_url)
listener.listen() listener.listen()

View File

@ -2,6 +2,7 @@ import os, json, random, io, logging
from pulp import LpProblem, LpVariable, LpMinimize, LpMaximize, LpAffineExpression, LpConstraint, LpStatus, lpSum from pulp import LpProblem, LpVariable, LpMinimize, LpMaximize, LpAffineExpression, LpConstraint, LpStatus, lpSum
from lib.application_configs import ApplicationConfigs
from helpers import aws_helper, tar_helper, csv_helper, service_helper, solver_helper from helpers import aws_helper, tar_helper, csv_helper, service_helper, solver_helper
from lib.errors.item_generation_error import ItemGenerationError from lib.errors.item_generation_error import ItemGenerationError
@ -201,14 +202,14 @@ class LoftService(Base):
if error: if error:
logging.info('Streaming %s error response to s3 bucket - %s', logging.info('Streaming %s error response to s3 bucket - %s',
self.file_name, os.environ['S3_PROCESSED_BUCKET']) self.file_name, ApplicationConfigs.s3_processed_bucket)
solution_file = service_helper.error_to_file(buffer, error) solution_file = service_helper.error_to_file(buffer, error)
else: else:
logging.info('Streaming %s to s3 bucket - %s', self.file_name, logging.info('Streaming %s to s3 bucket - %s', self.file_name,
os.environ['S3_PROCESSED_BUCKET']) ApplicationConfigs.s3_processed_bucket)
solution_file = service_helper.solution_to_file( solution_file = service_helper.solution_to_file(
buffer, self.solver_run.total_form_items, self.solution.forms) buffer, self.solver_run.total_form_items, self.solution.forms)
# upload generated file to s3 and return result # upload generated file to s3 and return result
return aws_helper.file_stream_upload(solution_file, self.file_name, return aws_helper.file_stream_upload(solution_file, self.file_name,
os.environ['S3_PROCESSED_BUCKET']) ApplicationConfigs.s3_processed_bucket)