Skip to content

Commit eaec9e7

Browse files
authored
Merge pull request #136 from baoj2010/main
Fix the bug of HANDLER is None
2 parents 97bdfb3 + 3ca945d commit eaec9e7

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

bali/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from bali.decorators import event_handler, init_handler
66
from bali.resources import Resource, ModelResource
77

8-
__version__ = '3.2.1'
8+
__version__ = '3.2.2'
99

1010

1111
class Schema(BaseModel):

bali/decorators.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def event_handler(event_type):
158158
# find queue by event_type
159159
def decorator(func):
160160
@functools.wraps(func)
161-
def wrapper(self, body, message):
161+
def wrapper(handler, body, message):
162162
try:
163163
if isinstance(body, str):
164164
body = json.loads(body)
@@ -175,12 +175,14 @@ def wrapper(self, body, message):
175175
):
176176
event = param.annotation(**body)
177177
break
178-
res = func(self, event or body)
178+
if not handler:
179+
handler = HANDLER
180+
res = func(handler, event or body)
179181
message.ack()
180182
return res
181183
except:
182184
logger.error(traceback.format_exc())
183-
callback = functools.partial(wrapper, HANDLER)
185+
callback = functools.partial(wrapper, None)
184186
register_callback(event_type, callback)
185187
return wrapper
186188

tests/test_event.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from kombu import Connection, Exchange, Queue
77

88
from bali.core import _settings
9-
from bali.decorators import event_handler
9+
from bali.decorators import event_handler, init_handler
1010
from bali.events import Event, dispatch, handle
1111

1212
amqp_uri = os.getenv('AMQP_SERVER_ADDRESS', default='amqp://127.0.0.1:5672')
@@ -38,12 +38,20 @@ class TestHandle:
3838
@event_handler(event_type='test0')
3939
def call_test0(self, event: Event):
4040
handle_test_handler_test0(event)
41+
self.instance_fun()
4142
return event
4243

4344
@event_handler(event_type='test1')
4445
def call_test1(self, event):
4546
handle_test_handler_test1(event)
4647

48+
@staticmethod
49+
def instance_fun():
50+
print('instance_fun')
51+
52+
53+
init_handler(TestHandle)
54+
4755

4856
def test_when_message_body_is_str():
4957
body = '{"type":"hello", "payload":""}'

0 commit comments

Comments
 (0)