Skip to content

Commit 1b6a940

Browse files
committed
feat: rabbitmq get channel 제거 및 연결, 채널 응답하도록 변경
1 parent 485837c commit 1b6a940

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

src/config/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
class GCS:
22
SIGNED_URL_EXPIRE = 60 * 24
33

4+
class RABBITMQ:
5+
HEART_BEAT = 60
6+
47
class MESSAGES:
58
class SUCCESS:
69
ANALYZE = "영상 분석을 완료했습니다."

src/config/rabbitmq.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,24 @@
22
import pika
33
import time
44

5-
from config.constants import MESSAGES
6-
7-
_channel = None
8-
9-
def connect(retry=0):
10-
global _channel
5+
from config.constants import MESSAGES, RABBITMQ
116

7+
def create_connection(retry=0):
128
if retry >= 5:
139
raise RuntimeError(MESSAGES.ERROR.FAILED_CONNECT_CHANNEL)
1410

1511
try:
1612
params = pika.URLParameters(environ["MQ_HOST"])
13+
params.heartbeat = RABBITMQ.HEART_BEAT
14+
1715
connection = pika.BlockingConnection(params)
18-
_channel = connection.channel()
16+
channel = connection.channel()
1917

20-
_channel.queue_declare(queue=environ["MQ_CONSUME_QUEUE"], durable=True)
21-
_channel.queue_declare(queue=environ["MQ_PUBLISH_QUEUE"], durable=True)
18+
channel.queue_declare(queue=environ["MQ_CONSUME_QUEUE"], durable=True)
19+
channel.queue_declare(queue=environ["MQ_PUBLISH_QUEUE"], durable=True)
20+
21+
return connection, channel
2222
except:
2323
time.sleep(5)
24-
connect(retry + 1)
25-
26-
def get_channel():
27-
global _channel
28-
29-
if _channel is None:
30-
connect(retry=0)
3124

32-
return _channel
25+
return create_connection(retry + 1)

src/rabbitmq/publish.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import json
22
from os import environ
33

4-
from config.rabbitmq import get_channel
4+
from config.rabbitmq import create_connection
55

66
def publish_message(message):
7-
channel = get_channel()
7+
connection, channel = create_connection()
88

99
channel.basic_publish(
1010
exchange="",
1111
routing_key=environ["MQ_PUBLISH_QUEUE"],
1212
body=json.dumps(message)
1313
)
14+
15+
connection.close()

0 commit comments

Comments
 (0)