@@ -21,18 +21,18 @@ class RabbitMQStore:
21
21
22
22
MAX_SEND_ATTEMPTS : int = 6 # 最大发送重试次数
23
23
MAX_CONNECTION_ATTEMPTS : float = float ("inf" ) # 最大连接重试次数
24
- MAX_CONNECTION_DELAY : int = 2 ** 5 # 最大延迟时间
24
+ MAX_CONNECTION_DELAY : int = 2 ** 5 # 最大延迟时间
25
25
RECONNECTION_DELAY : int = 1
26
26
27
27
def __init__ (
28
- self ,
29
- * ,
30
- confirm_delivery : bool = True ,
31
- host : Optional [str ] = None ,
32
- port : Optional [int ] = None ,
33
- username : Optional [str ] = None ,
34
- password : Optional [str ] = None ,
35
- ** kwargs ,
28
+ self ,
29
+ * ,
30
+ confirm_delivery : bool = True ,
31
+ host : Optional [str ] = None ,
32
+ port : Optional [int ] = None ,
33
+ username : Optional [str ] = None ,
34
+ password : Optional [str ] = None ,
35
+ ** kwargs ,
36
36
):
37
37
"""
38
38
:param confirm_delivery: 是否开启消息确认
@@ -99,7 +99,7 @@ def connection(self) -> None:
99
99
@property
100
100
def channel (self ) -> amqpstorm .Channel :
101
101
if all ([self ._connection , self ._channel ]) and all (
102
- [self ._connection .is_open , self ._channel .is_open ]
102
+ [self ._connection .is_open , self ._channel .is_open ]
103
103
):
104
104
return self ._channel
105
105
self ._channel = self .connection .channel ()
@@ -131,11 +131,11 @@ def declare_queue(self, queue_name: str, durable: bool = True, **kwargs):
131
131
return self .channel .queue .declare (queue_name , durable = durable , ** kwargs )
132
132
133
133
def send (
134
- self ,
135
- queue_name : str ,
136
- message : Union [str , bytes ],
137
- priority : Optional [dict ] = None ,
138
- ** kwargs ,
134
+ self ,
135
+ queue_name : str ,
136
+ message : Union [str , bytes ],
137
+ priority : Optional [dict ] = None ,
138
+ ** kwargs ,
139
139
):
140
140
"""发送消息"""
141
141
attempts = 1
@@ -163,7 +163,7 @@ def get_message_counts(self, queue_name: str) -> int:
163
163
return queue_response .get ("message_count" , 0 )
164
164
165
165
def start_consuming (
166
- self , queue_name : str , callback : Callable , prefetch = 1 , ** kwargs
166
+ self , queue_name : str , callback : Callable , prefetch = 1 , ** kwargs
167
167
):
168
168
"""开始消费"""
169
169
self .__shutdown = False
@@ -226,18 +226,52 @@ def stop_listener(self, queue_name: str):
226
226
227
227
228
228
class RabbitListener :
229
- def __init__ (self , instance : RabbitMQStore , * , queue_name : str , no_ack : bool = False , ** kwargs ):
229
+ def __init__ (
230
+ self ,
231
+ instance : RabbitMQStore ,
232
+ * ,
233
+ queue_name : str ,
234
+ no_ack : bool = False ,
235
+ stop_listener : Callable = None ,
236
+ ** kwargs ,
237
+ ):
230
238
self .instance = instance
231
239
self .queue_name = queue_name
232
240
self .no_ack = no_ack
233
241
self .kwargs = kwargs
242
+ self .stop_listener = stop_listener
243
+ self .last_message_time = time .time ()
244
+ self .should_stop = False
234
245
235
246
def __call__ (self , callback : Callable [[amqpstorm .Message ], None ]):
236
- listener = self .instance .listener (self .queue_name , self .no_ack , ** self .kwargs )
237
- return listener (callback )
238
247
248
+ def wrapped_callback (message ):
249
+ self .last_message_time = time .time ()
250
+ callback (message )
251
+
252
+ def monitor_thread ():
253
+ while not self .should_stop :
254
+ time .sleep (1 )
255
+ if self .stop_listener and self .stop_listener (self ):
256
+ logger .info ("停止监听器" )
257
+ self .should_stop = True
258
+ self .instance .shutdown ()
259
+ break
239
260
240
- # alias
261
+ def consume_thread ():
262
+ listener = self .instance .listener (
263
+ self .queue_name , self .no_ack , ** self .kwargs
264
+ )
265
+ listener (wrapped_callback )
241
266
267
+ consume_thread = threading .Thread (target = consume_thread )
268
+ monitor_thread = threading .Thread (target = monitor_thread )
269
+
270
+ consume_thread .start ()
271
+ monitor_thread .start ()
272
+ return consume_thread , monitor_thread
273
+
274
+
275
+ # alias
242
276
useRabbitMQ = RabbitMQStore
243
277
useRabbitListener = RabbitListener
0 commit comments