/
eventbus_wrapper_rabbitmq.py
203 lines (191 loc) · 7.04 KB
/
eventbus_wrapper_rabbitmq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from typing import Callable, Awaitable, Optional, Union, Any, List
from .async_eventbus_rabbitmq import AsyncEventbusRabbitMQ
from ..event import IntegrationEvent, AsyncSubscriberHandler
from ..exceptions import BlockingException, ThreadUnsafeException
from amqp_client_python.domain.models import Config
from pika import DeliveryMode
from threading import Thread, current_thread
from asyncio import new_event_loop, run_coroutine_threadsafe
from concurrent.futures import Future
class EventbusWrapperRabbitMQ:
def __init__(
self,
config: Config,
loop=None,
pub_publisher_confirms=True,
rpc_client_publisher_confirms=True,
rpc_server_publisher_confirms=False,
sub_prefetch_count=0,
rpc_client_prefetch_count=0,
rpc_server_prefetch_count=0,
sub_auto_ack=False,
rpc_client_auto_ack=False,
rpc_server_auto_ack=False,
) -> None:
self._loop = loop or new_event_loop()
self._async_eventbus = AsyncEventbusRabbitMQ(
config,
self._loop,
pub_publisher_confirms,
rpc_client_publisher_confirms,
rpc_server_publisher_confirms,
sub_prefetch_count,
rpc_client_prefetch_count,
rpc_server_prefetch_count,
sub_auto_ack,
rpc_client_auto_ack,
rpc_server_auto_ack,
)
self.on = self._async_eventbus.on
self._thread = Thread(target=self._loop.run_forever)
self._thread.start()
def rpc_client(
self,
exchange: str,
routing_key: str,
body: List[Any],
content_type="application/json",
timeout=5,
) -> Future:
if self._thread.ident == current_thread().ident:
raise BlockingException(
"Cannot run sync blocking call on async thread, try to use async methods with an await expression"
)
return run_coroutine_threadsafe(
self._async_eventbus.rpc_client(
exchange, routing_key, body, content_type, timeout
),
self._loop,
)
async def async_rpc_client(
self,
exchange: str,
routing_key: str,
body: List[Any],
content_type="application/json",
timeout=5,
):
if self._thread.ident != current_thread().ident:
raise ThreadUnsafeException(
"Cannot run async call on this thread, try to use sync thread safe methods"
)
return await self._async_eventbus.rpc_client(
exchange, routing_key, body, content_type, timeout
)
async def async_publish(
self,
event: IntegrationEvent,
routing_key: str,
body: List[Any],
content_type="application/json",
timeout=5,
connection_timeout: int = 16,
delivery_mode=DeliveryMode.Transient,
expiration: Optional[Union[str, None]] = None, # example: '60000' -> 60s
**kwargs
):
if self._thread.ident != current_thread().ident:
raise ThreadUnsafeException(
"Cannot run async call on this thread, try to use sync thread safe methods"
)
await self._async_eventbus.publish(
event,
routing_key,
body,
content_type,
timeout,
connection_timeout,
delivery_mode,
expiration,
**kwargs
)
def publish(
self,
event: IntegrationEvent,
routing_key: str,
body: List[Any],
content_type="application/json",
timeout=5,
connection_timeout: int = 16,
delivery_mode=DeliveryMode.Transient,
expiration: Optional[Union[str, None]] = None, # example: '60000' -> 60s
**kwargs
) -> Future:
if self._thread.ident == current_thread().ident:
raise BlockingException(
"Cannot run sync blocking call on async thread, try to use async methods with an await expression"
)
return run_coroutine_threadsafe(
self._async_eventbus.publish(
event, routing_key, body, content_type, timeout, connection_timeout,
delivery_mode, expiration, **kwargs
),
self._loop,
)
def subscribe(
self,
event: IntegrationEvent,
handler: AsyncSubscriberHandler,
routing_key: str,
response_timeout: int = None,
connection_timeout: int = 16
) -> Future:
if self._thread.ident == current_thread().ident:
raise BlockingException(
"Cannot run sync blocking call on async thread, try to use async methods with an await expression"
)
return run_coroutine_threadsafe(
self._async_eventbus.subscribe(
event, handler, routing_key, response_timeout, connection_timeout
),
self._loop,
)
async def async_subscribe(
self,
event: IntegrationEvent,
handler: AsyncSubscriberHandler,
routing_key: str,
response_timeout: int = None,
connection_timeout: int = 16
):
if self._thread.ident != current_thread().ident:
raise ThreadUnsafeException(
"Cannot run async call on this thread, try to use sync thread safe methods"
)
await self._async_eventbus.subscribe(
event, handler, routing_key, response_timeout, connection_timeout
)
def provide_resource(
self, name: str,
callback: Callable[[List[Any]], Awaitable[Union[bytes, str]]],
response_timeout: int = None,
connection_timeout: int = 16
) -> Future:
if self._thread.ident == current_thread().ident:
raise BlockingException(
"Cannot run sync blocking call on async thread, try to use async methods with an await expression"
)
return run_coroutine_threadsafe(
self._async_eventbus.provide_resource(name, callback, response_timeout, connection_timeout),
self._loop,
)
async def async_provide_resource(
self,
name: str,
callback: Callable[[List[Any]], Awaitable[Union[bytes, str]]],
response_timeout: int = None,
connection_timeout: int = 16
):
if self._thread.ident != current_thread().ident:
raise ThreadUnsafeException(
"Cannot run async call on this thread, try to use sync thread safe methods"
)
await self._async_eventbus.provide_resource(name, callback, response_timeout, connection_timeout)
def dispose(self):
run_coroutine_threadsafe(self._async_eventbus.dispose(True), self._loop)
async def async_dispose(self, stop_event_loop=True):
if self._thread.ident != current_thread().ident:
raise ThreadUnsafeException(
"Cannot run async call on this thread, try to use sync thread safe methods"
)
await self._async_eventbus.dispose(stop_event_loop=stop_event_loop)