From 234cef2dfec78ad5adcb495d047b13823c915852 Mon Sep 17 00:00:00 2001 From: hong seokho Date: Tue, 30 Jul 2024 00:08:48 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20broker=20ssl=20&=20port=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/celery_app.py | 20 ++++++++++++------- .../application/config/queue_config.py | 20 ++++++++++--------- .../AnimatedDrawings/application/config/yml | 2 +- .../application/kombu_connection_pool.py | 10 +++++++++- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/backend/AnimatedDrawings/application/celery_app.py b/backend/AnimatedDrawings/application/celery_app.py index 151a098a9..894a72ef6 100644 --- a/backend/AnimatedDrawings/application/celery_app.py +++ b/backend/AnimatedDrawings/application/celery_app.py @@ -5,12 +5,18 @@ queue_config = QueueConfig() celery = Celery('application.task', - broker=queue_config.get_celery_broker_url(), - include=['application.task.tasks']) + broker=queue_config.get_broker_url(), + include=['application.task.tasks'] + ) celery.conf.result_expires = 300 -celery.conf.task_queues = ( - Queue('task.makeAnimation.queue'), - Queue('task.saveCapsuleSkin.queue'), - Queue('task.sendNotification.queue') -) +celery.conf.task_queues = (Queue('task.makeAnimation.queue'), + Queue('task.saveCapsuleSkin.queue'), + Queue('task.sendNotification.queue') + ) + +if queue_config.PROTOCOL == 'amqps': + import ssl + celery.conf.broker_use_ssl = { + 'cert_reqs': ssl.CERT_REQUIRED + } diff --git a/backend/AnimatedDrawings/application/config/queue_config.py b/backend/AnimatedDrawings/application/config/queue_config.py index 04d70546a..2a0a072f9 100644 --- a/backend/AnimatedDrawings/application/config/queue_config.py +++ b/backend/AnimatedDrawings/application/config/queue_config.py @@ -2,9 +2,11 @@ class QueueConfig: + PROTOCOL = RootConfig.CONFIG_FILE['rabbitmq']['protocol'] USERNAME = RootConfig.CONFIG_FILE['rabbitmq']['username'] BROKER_HOST = RootConfig.CONFIG_FILE['rabbitmq']['host'] PASSWORD = RootConfig.CONFIG_FILE['rabbitmq']['password'] + PORT = RootConfig.CONFIG_FILE['rabbitmq']['port'] VIRTUAL_HOST = RootConfig.CONFIG_FILE['rabbitmq']['virtual-host'] CAPSULE_SKIN_REQUEST_QUEUE_NAME = RootConfig.CONFIG_FILE['rabbitmq'][ 'queue_name'] @@ -16,12 +18,12 @@ class QueueConfig: 'notification_queue_name'] @staticmethod - def get_celery_broker_url() -> str: - return 'pyamqp://%s:%s@%s:5672%s' % (QueueConfig.USERNAME, - QueueConfig.PASSWORD, - QueueConfig.BROKER_HOST, - QueueConfig.VIRTUAL_HOST) - - @staticmethod - def get_kombu_broker_url() -> str: - return f'amqp://{QueueConfig.USERNAME}:{QueueConfig.PASSWORD}@{QueueConfig.BROKER_HOST}/{QueueConfig.VIRTUAL_HOST}' + def get_broker_url() -> str: + return ( + f'{QueueConfig.PROTOCOL}://' + f'{QueueConfig.USERNAME}:' + f'{QueueConfig.PASSWORD}@' + f'{QueueConfig.BROKER_HOST}:' + f'{QueueConfig.PORT}' + f'{QueueConfig.VIRTUAL_HOST}' + ) diff --git a/backend/AnimatedDrawings/application/config/yml b/backend/AnimatedDrawings/application/config/yml index 443596e26..6b0cddbbd 160000 --- a/backend/AnimatedDrawings/application/config/yml +++ b/backend/AnimatedDrawings/application/config/yml @@ -1 +1 @@ -Subproject commit 443596e26746e9818a2ba4a5e8e88056cdbb9e86 +Subproject commit 6b0cddbbd5852fa9f4534ed031102e19349d884b diff --git a/backend/AnimatedDrawings/application/kombu_connection_pool.py b/backend/AnimatedDrawings/application/kombu_connection_pool.py index db57e3727..e0ce840c5 100644 --- a/backend/AnimatedDrawings/application/kombu_connection_pool.py +++ b/backend/AnimatedDrawings/application/kombu_connection_pool.py @@ -18,8 +18,16 @@ def errback(exc, interval): connections = pools.Connections(limit=2) producers = pools.Producers(limit=2) -connection = Connection(hostname=QueueConfig.get_kombu_broker_url(), +if QueueConfig.PROTOCOL == 'amqps': + import ssl + ssl_option = { + 'cert_reqs': ssl.CERT_REQUIRED + } +else: + ssl_option = None +connection = Connection(hostname=QueueConfig.get_broker_url(), connect_timeout=7, + ssl=ssl_option, errback=errback) logger.info('레빗 엠큐 커넥션 풀 연결 설정 완료')