File tree Expand file tree Collapse file tree 3 files changed +17
-19
lines changed Expand file tree Collapse file tree 3 files changed +17
-19
lines changed Original file line number Diff line number Diff line change 11class GCS :
22 SIGNED_URL_EXPIRE = 60 * 24
33
4+ class RABBITMQ :
5+ HEART_BEAT = 60
6+
47class MESSAGES :
58 class SUCCESS :
69 ANALYZE = "영상 분석을 완료했습니다."
Original file line number Diff line number Diff line change 22import pika
33import 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 )
Original file line number Diff line number Diff line change 11import json
22from os import environ
33
4- from config .rabbitmq import get_channel
4+ from config .rabbitmq import create_connection
55
66def 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 ()
You can’t perform that action at this time.
0 commit comments