buz 2.22.0__py3-none-any.whl → 2.23.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,11 +3,18 @@ from typing import Generic, Type, TypeVar
3
3
 
4
4
  from buz import Handler
5
5
  from buz.command import Command
6
+ from buz.wrapper.async_unsafe import async_unsafe
6
7
 
7
8
  TCommand = TypeVar("TCommand", bound=Command)
8
9
 
9
10
 
10
11
  class CommandHandler(Generic[TCommand], Handler[TCommand], ABC):
12
+ def __new__(cls, *args, **kwargs):
13
+ instance = super().__new__(cls)
14
+ original_handle = instance.handle
15
+ instance.handle = async_unsafe()(original_handle)
16
+ return instance
17
+
11
18
  @classmethod
12
19
  @abstractmethod
13
20
  def handles(cls) -> Type[TCommand]:
@@ -16,6 +16,8 @@ from buz.kafka.domain.models.auto_create_topic_configuration import AutoCreateTo
16
16
  from buz.kafka.domain.models.create_kafka_topic import CreateKafkaTopic
17
17
  from buz.kafka.domain.services.kafka_admin_client import KafkaAdminClient
18
18
  from buz.kafka.domain.services.kafka_producer import KafkaProducer
19
+ from buz.kafka.infrastructure.serializers.implementations.cdc_partition_key_serializer import CDCPartitionKeySerializer
20
+ from buz.kafka.infrastructure.serializers.partitiion_key_generator import PartitionKeySerializer
19
21
 
20
22
 
21
23
  class BuzKafkaEventBus(EventBus):
@@ -28,6 +30,7 @@ class BuzKafkaEventBus(EventBus):
28
30
  kafka_admin_client: Optional[KafkaAdminClient] = None,
29
31
  publish_middlewares: Optional[list[PublishMiddleware]] = None,
30
32
  auto_create_topic_configuration: Optional[AutoCreateTopicConfiguration] = None,
33
+ partition_key_generator: Optional[PartitionKeySerializer] = None,
31
34
  ):
32
35
  self.__publish_middleware_chain_resolver = PublishMiddlewareChainResolver(publish_middlewares or [])
33
36
  self.__publish_strategy = publish_strategy
@@ -36,6 +39,7 @@ class BuzKafkaEventBus(EventBus):
36
39
  self.__kafka_admin_client = kafka_admin_client
37
40
  self.__auto_create_topic_configuration = auto_create_topic_configuration
38
41
  self.__logger = logger
42
+ self.__partition_key_generator: PartitionKeySerializer = partition_key_generator or CDCPartitionKeySerializer()
39
43
  self.__check_kafka_admin_client_is_needed()
40
44
 
41
45
  def __check_kafka_admin_client_is_needed(self) -> None:
@@ -74,7 +78,7 @@ class BuzKafkaEventBus(EventBus):
74
78
  message=event,
75
79
  headers=headers,
76
80
  topic=topic,
77
- partition_key=event.partition_key(),
81
+ partition_key=self.__partition_key_generator.generate_key(event),
78
82
  )
79
83
  except Exception as exc:
80
84
  raise EventNotPublishedException(event) from exc
@@ -0,0 +1,19 @@
1
+ from __future__ import annotations
2
+
3
+ from buz.event.event import Event
4
+ from buz.kafka.infrastructure.serializers.implementations.json_byte_serializer import JSONByteSerializer
5
+ from buz.kafka.infrastructure.serializers.partitiion_key_generator import PartitionKeySerializer
6
+
7
+ # This is a static string because the order matters and we can not trust that json encoder libraries are deterministic
8
+ PAYLOAD_CDC_SCHEMA = r"""{"schema":{"type":"string","optional":true},"payload":"[partition_key]"}"""
9
+
10
+
11
+ class CDCPartitionKeySerializer(PartitionKeySerializer):
12
+ def __init__(self) -> None:
13
+ self.__json_serializer = JSONByteSerializer()
14
+
15
+ def __generate_payload_schema(self, partition_key: str) -> str:
16
+ return PAYLOAD_CDC_SCHEMA.replace("[partition_key]", partition_key)
17
+
18
+ def generate_key(self, event: Event) -> str:
19
+ return self.__generate_payload_schema(event.partition_key())
@@ -0,0 +1,9 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from buz.event.event import Event
4
+
5
+
6
+ class PartitionKeySerializer(ABC):
7
+ @abstractmethod
8
+ def generate_key(self, event: Event) -> str:
9
+ pass
@@ -3,12 +3,19 @@ from typing import Generic, Type, TypeVar
3
3
 
4
4
  from buz import Handler
5
5
  from buz.query import Query, QueryResponse
6
+ from buz.wrapper.async_unsafe import async_unsafe
6
7
 
7
8
  TQuery = TypeVar("TQuery", bound=Query)
8
9
  TQueryResponse = TypeVar("TQueryResponse", bound=QueryResponse)
9
10
 
10
11
 
11
12
  class QueryHandler(Generic[TQuery, TQueryResponse], Handler[TQuery], ABC):
13
+ def __new__(cls, *args, **kwargs):
14
+ instance = super().__new__(cls)
15
+ original_handle = instance.handle
16
+ instance.handle = async_unsafe()(original_handle)
17
+ return instance
18
+
12
19
  @classmethod
13
20
  @abstractmethod
14
21
  def handles(cls) -> Type[TQuery]:
@@ -0,0 +1,67 @@
1
+ import logging
2
+ import os
3
+ from asyncio import get_running_loop
4
+ from functools import wraps
5
+ from typing import Callable, Any
6
+
7
+ from buz.wrapper.synchronous_only_operation_exception import SynchronousOnlyOperationException
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ def async_unsafe() -> Callable[[Callable], Callable]:
13
+ """
14
+ Copy-paste from Django source code. If BUZ_SYNC_HANDLERS_FAIL_ON_ASYNC env var is True then an exception is raised to
15
+ prevent event loop from being blocked. If the env var is False, a warning is logged.
16
+ """
17
+
18
+ def decorator(func: Callable) -> Callable:
19
+ @wraps(func)
20
+ def inner(*args: Any, **kwargs: Any) -> Any:
21
+ try:
22
+ get_running_loop()
23
+ except RuntimeError:
24
+ pass
25
+ else:
26
+ if __get_bool_env("BUZ_SYNC_HANDLERS_FAIL_ON_ASYNC") is True:
27
+ raise SynchronousOnlyOperationException()
28
+
29
+ logger.warning(
30
+ f"You should not call this from an async context - use a thread or sync_to_async. Class {__get_class_name(func, args)}"
31
+ )
32
+
33
+ return func(*args, **kwargs)
34
+
35
+ return inner
36
+
37
+ return decorator
38
+
39
+
40
+ def __get_bool_env(var_name: str) -> bool:
41
+ """
42
+ >>> get_bool_env("MY_BOOL_VAR")
43
+ False
44
+ >>> os.environ["MY_BOOL_VAR"] = "1"
45
+ >>> get_bool_env("MY_BOOL_VAR")
46
+ True
47
+ >>> os.environ["MY_BOOL_VAR"] = "true"
48
+ >>> get_bool_env("MY_BOOL_VAR")
49
+ True
50
+ >>> os.environ["MY_BOOL_VAR"] = "True"
51
+ >>> get_bool_env("MY_BOOL_VAR")
52
+ True
53
+ >>> os.environ["MY_BOOL_VAR"] = "0"
54
+ >>> get_bool_env("MY_BOOL_VAR")
55
+ False
56
+ >>> os.environ["MY_BOOL_VAR"] = "false"
57
+ >>> get_bool_env("MY_BOOL_VAR")
58
+ False
59
+ """
60
+ return os.environ.get(var_name, "0").lower() in ("true", "1")
61
+
62
+
63
+ def __get_class_name(func: Callable, args: tuple) -> str:
64
+ if hasattr(func, "__self__"):
65
+ return func.__self__.__class__.__name__
66
+
67
+ return args[0].__class__.__name__ if len(args) > 0 else "Unknown"
@@ -0,0 +1,3 @@
1
+ class SynchronousOnlyOperationException(Exception):
2
+ def __init__(self):
3
+ super().__init__("You cannot call this from an async context - use a thread or sync_to_async.")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: buz
3
- Version: 2.22.0
3
+ Version: 2.23.0
4
4
  Summary: Buz is a set of light, simple and extensible implementations of event, command and query buses.
5
5
  License: MIT
6
6
  Author: Luis Pintado Lozano
@@ -15,7 +15,7 @@ buz/command/more_than_one_command_handler_related_exception.py,sha256=dNCMXCAKVc
15
15
  buz/command/synchronous/__init__.py,sha256=ZAH35jpte6zpxb70iQfZfGSg_OsXqVNYwNkCJy7Qnzk,268
16
16
  buz/command/synchronous/base_command_handler.py,sha256=GkmxttA1_Wu2X5O7iR-tLRdnmT-JDgeyURPwkjLPGhg,1393
17
17
  buz/command/synchronous/command_bus.py,sha256=FHJH4lerThu5fWRISM0VIK8ebyfqgDGqNNCqItbmGPI,175
18
- buz/command/synchronous/command_handler.py,sha256=pP11Njbth89NQiVPqlALtKmabVtOX4sucdbiCp-sceI,422
18
+ buz/command/synchronous/command_handler.py,sha256=M9nQhW9Ilkix09U6Pv6ZpoqA-gezrm7H_gqjoLDc_A4,676
19
19
  buz/command/synchronous/middleware/__init__.py,sha256=wE97veOW2cCGpTZ-3bvc3aU6NdQmSOYfdX9kAUETJ0w,406
20
20
  buz/command/synchronous/middleware/base_handle_middleware.py,sha256=qHnbucL7j6Dnw7E8ACBf32pzDD9squYPp6UiK2heb5A,774
21
21
  buz/command/synchronous/middleware/handle_middleware.py,sha256=oIsmB30mu-Cd9IQc1qMKbUIaIMHipSEXOw2swVrtbkk,420
@@ -50,7 +50,7 @@ buz/event/infrastructure/buz_kafka/async_buz_kafka_event_bus.py,sha256=kxqXCf80L
50
50
  buz/event/infrastructure/buz_kafka/base_buz_aiokafka_async_consumer.py,sha256=7ZhaKaFXBpD3HVkuQMpAJvY8lfy7__1wxftLIwCmnMQ,21284
51
51
  buz/event/infrastructure/buz_kafka/buz_aiokafka_async_consumer.py,sha256=GmmuAZboDkrpNOLF8cE_F0t4I7ZnMiGsiGw4SYIvKGc,7303
52
52
  buz/event/infrastructure/buz_kafka/buz_aiokafka_multi_threaded_consumer.py,sha256=ZRLRoBRomqrXAiePSMn4gePF59AWPn6VQpQui1UVnyM,7246
53
- buz/event/infrastructure/buz_kafka/buz_kafka_event_bus.py,sha256=_CLD4gU7KikVTEydSMtPE3meFhlOtAx0km4cS6Q3tno,4654
53
+ buz/event/infrastructure/buz_kafka/buz_kafka_event_bus.py,sha256=bfxdwUh33yFIo0NCm_S_AMrfnLvTCL9dGZZXDaOkKNM,5094
54
54
  buz/event/infrastructure/buz_kafka/consume_strategy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
55
55
  buz/event/infrastructure/buz_kafka/consume_strategy/consume_strategy.py,sha256=RqlXe5W2S6rH3FTr--tcxzFJTAVLb-Dhl7m6qjgNz2M,331
56
56
  buz/event/infrastructure/buz_kafka/consume_strategy/kafka_on_fail_strategy.py,sha256=elNeyTubDuhHsLlTtDA1Nqz2hZe12PUcO9kz8upPby8,136
@@ -205,9 +205,11 @@ buz/kafka/infrastructure/kafka_python/kafka_python_producer.py,sha256=McH_YNQcte
205
205
  buz/kafka/infrastructure/kafka_python/translators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
206
206
  buz/kafka/infrastructure/kafka_python/translators/consumer_initial_offset_position_translator.py,sha256=hJ48_eyMcnbFL_Y5TOiMbGXrQSryuKk9CvP59MdqNOY,620
207
207
  buz/kafka/infrastructure/serializers/byte_serializer.py,sha256=T83sLdX9V5Oh1mzjRwHi_1DsTFI7KefFj7kmnz7JVy4,207
208
+ buz/kafka/infrastructure/serializers/implementations/cdc_partition_key_serializer.py,sha256=TNN1H5AndGvpxkfBTOHGSH94mZocQ4n3JApQnvp7TNw,902
208
209
  buz/kafka/infrastructure/serializers/implementations/cdc_record_bytes_to_event_serializer.py,sha256=c5VmnuTOIZJ_tjfOl_BQnPdr7D1OAOxGYVFE4O4X8Tk,1801
209
210
  buz/kafka/infrastructure/serializers/implementations/json_byte_serializer.py,sha256=yCPlZ-TNoF5Jh0WUlhseC8rTnHk-IJgmAHmU58ninyA,1523
210
211
  buz/kafka/infrastructure/serializers/kafka_header_serializer.py,sha256=ws9xr5lsJF6J-uVIplPym7vboo00KtXHfLJf8JjG0lo,649
212
+ buz/kafka/infrastructure/serializers/partitiion_key_generator.py,sha256=4FQmTuqrLp9xmaYg9qv42x5fPMQFaC4OmDnmuszGf94,190
211
213
  buz/locator/__init__.py,sha256=my8qfHL5htIT9RFFjzV4zGIPVW72tu4SMQbKKqBeSKo,293
212
214
  buz/locator/handler_fqn_not_found_exception.py,sha256=HfQb8gwqEpG1YfOc_IQlSCjRg0Ete0Aj_z3s7GPC-RM,206
213
215
  buz/locator/locator.py,sha256=1RqxXSkTw5PgwSB_AME3gYzYgN8ySV0FrUwW5N_0cAQ,512
@@ -248,7 +250,7 @@ buz/query/synchronous/middleware/base_handle_middleware.py,sha256=4lqJB2C-GHl3T5
248
250
  buz/query/synchronous/middleware/handle_middleware.py,sha256=Xr2HSlRrW3slluyUiJ6zH0Rw0oeWD3uMN-1hjuFbC_k,433
249
251
  buz/query/synchronous/middleware/handle_middleware_chain_resolver.py,sha256=OIpGLJ_2a8cGsp-LmA3Q1Lvb1pB65MIuA-geiPrQKHM,1070
250
252
  buz/query/synchronous/query_bus.py,sha256=eYl_sGH5TcELkOXc4RnV4Tkmcs-VLc1vzd-YMduQ1YI,189
251
- buz/query/synchronous/query_handler.py,sha256=q8zqXjU9btid_q4wbL73QgaiWjMzGDFvwZ5AQN4q0CA,505
253
+ buz/query/synchronous/query_handler.py,sha256=NuolVPZelmjOe6-q_hFC3Y0gm7ih2A6aanKufo6wHR0,759
252
254
  buz/query/synchronous/self_process/__init__.py,sha256=fU1OoXXXH5dMGKz8y7mwTVvyhNj6BCKDTxuxH_q-leM,125
253
255
  buz/query/synchronous/self_process/self_process_query_bus.py,sha256=pKGJxXBWtqU4i0fzb30OCNhAVPCkUh7IlfNzgAhCUC8,1157
254
256
  buz/query/synchronous/synced_async/__init__.py,sha256=TdFmIBeFIpl3Tvmh_FJpJMXJdPdfRxOstVqnPUi23mo,125
@@ -262,8 +264,10 @@ buz/serializer/message_to_bytes_serializer.py,sha256=uQ6JSTn24mLQvN48alwVXa6lfgf
262
264
  buz/serializer/message_to_json_bytes_serializer.py,sha256=RGZJ64t4t4Pz2FCASZZCv-2LiWnertC8scE9ZzQkBsU,764
263
265
  buz/wrapper/__init__.py,sha256=GnRdJFcncn-qp0hzDG9dBHLmTJSbHFVjE_yr-MdW_n4,77
264
266
  buz/wrapper/async_to_sync.py,sha256=OfK-vrVUhuN-LLLvekLdMbQYtH0ue5lfbvuasj6ovMI,698
267
+ buz/wrapper/async_unsafe.py,sha256=vg-BM8Dh7MqKiMAc8qsMPexLUuHI1bn4APzMXeSrBgk,2026
265
268
  buz/wrapper/event_loop.py,sha256=pfBJ1g-8A2a3YgW8Gf9Fg0kkewoh3-wgTy2KIFDyfHk,266
266
- buz-2.22.0.dist-info/LICENSE,sha256=jcLgcIIVaBqaZNwe0kzGWSU99YgwMcI0IGv142wkYSM,1062
267
- buz-2.22.0.dist-info/METADATA,sha256=A_w-t53670N0vP0iwVhKBEbIx1z14OhHNSyS5F4urHs,12580
268
- buz-2.22.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
269
- buz-2.22.0.dist-info/RECORD,,
269
+ buz/wrapper/synchronous_only_operation_exception.py,sha256=qCTaNSuIY7_HXTOmzlNGhks1nRApeW5K8W5aHC-dL-4,180
270
+ buz-2.23.0.dist-info/LICENSE,sha256=jcLgcIIVaBqaZNwe0kzGWSU99YgwMcI0IGv142wkYSM,1062
271
+ buz-2.23.0.dist-info/METADATA,sha256=YxHyoXLE0SlA_UxWPBACGbNIeXkeOnEW9_gfe6yDOo8,12580
272
+ buz-2.23.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
273
+ buz-2.23.0.dist-info/RECORD,,
File without changes
File without changes