streambus 0.1.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.
streambus/__init__.py ADDED
@@ -0,0 +1,16 @@
1
+ from .config import RedisConfig
2
+ from .event import StreamBusEvent
3
+ from .exceptions import ConfigurationError, EventValidationError, StreambusError
4
+ from .listener import EventListener
5
+ from .publisher import EventPublisher
6
+
7
+ __all__ = [
8
+ "EventListener",
9
+ "EventPublisher",
10
+ "RedisConfig",
11
+ "StreamBusEvent",
12
+ "StreambusError",
13
+ "ConfigurationError",
14
+ "EventValidationError",
15
+ ]
16
+ __version__ = "0.1.0"
streambus/config.py ADDED
@@ -0,0 +1,8 @@
1
+ from dataclasses import dataclass, field
2
+
3
+
4
+ @dataclass
5
+ class RedisConfig:
6
+ url: str
7
+ socket_timeout: float | None = field(default=None)
8
+ socket_connect_timeout: float | None = field(default=5.0)
streambus/event.py ADDED
@@ -0,0 +1,16 @@
1
+ from dataclasses import dataclass, asdict, field, fields as dc_fields
2
+ from datetime import datetime, timezone
3
+
4
+
5
+ @dataclass(kw_only=True)
6
+ class StreamBusEvent:
7
+ event_type: str
8
+ occurred_at: str = field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
9
+
10
+ def to_dict(self) -> dict[str, str]:
11
+ return {k: str(v) for k, v in asdict(self).items()}
12
+
13
+ @classmethod
14
+ def from_dict(cls, data: dict[str, str]) -> "StreamBusEvent":
15
+ known = {f.name for f in dc_fields(cls)}
16
+ return cls(**{k: v for k, v in data.items() if k in known})
@@ -0,0 +1,10 @@
1
+ class StreambusError(Exception):
2
+ pass
3
+
4
+
5
+ class ConfigurationError(StreambusError):
6
+ pass
7
+
8
+
9
+ class EventValidationError(StreambusError):
10
+ pass
streambus/listener.py ADDED
@@ -0,0 +1,94 @@
1
+ import logging
2
+ import time
3
+ from typing import Callable
4
+
5
+ import redis as redis_lib
6
+
7
+ from streambus.config import RedisConfig
8
+ from streambus.event import StreamBusEvent
9
+ from streambus.exceptions import ConfigurationError
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ class EventListener:
15
+ def __init__(
16
+ self,
17
+ stream: str,
18
+ group: str,
19
+ handler: Callable[[StreamBusEvent], None],
20
+ event_class: type[StreamBusEvent],
21
+ config: RedisConfig,
22
+ consumer: str | None = None,
23
+ batch_size: int = 10,
24
+ block_ms: int = 5000,
25
+ retry_delay: float = 3.0,
26
+ ):
27
+ if not stream:
28
+ raise ConfigurationError("stream is required")
29
+ if not group:
30
+ raise ConfigurationError("group is required")
31
+ if not callable(handler):
32
+ raise ConfigurationError("handler must be callable")
33
+ if not (isinstance(event_class, type) and issubclass(event_class, StreamBusEvent)):
34
+ raise ConfigurationError("event_class must be a subclass of StreamBusEvent")
35
+ if not isinstance(config, RedisConfig):
36
+ raise ConfigurationError("config must be a RedisConfig instance")
37
+ if not config.url:
38
+ raise ConfigurationError("config.url is required")
39
+
40
+ self.stream = stream
41
+ self.group = group
42
+ self.handler = handler
43
+ self.event_class = event_class
44
+ self.config = config
45
+ self.consumer = consumer or f"{group}-1"
46
+ self.batch_size = batch_size
47
+ self.block_ms = block_ms
48
+ self.retry_delay = retry_delay
49
+
50
+ def run(self) -> None:
51
+ r = self._connect()
52
+ self._ensure_group(r)
53
+ logger.info("[%s] listening on %s", self.group, self.stream)
54
+
55
+ while True:
56
+ try:
57
+ results = r.xreadgroup(
58
+ self.group,
59
+ self.consumer,
60
+ {self.stream: ">"},
61
+ count=self.batch_size,
62
+ block=self.block_ms,
63
+ )
64
+ for _stream, messages in (results or []):
65
+ for msg_id, data in messages:
66
+ try:
67
+ event = self.event_class.from_dict(data)
68
+ self.handler(event)
69
+ r.xack(self.stream, self.group, msg_id)
70
+ except Exception:
71
+ logger.exception("error processing message %s", msg_id)
72
+ except redis_lib.exceptions.ConnectionError:
73
+ logger.warning(
74
+ "[%s] redis connection lost, retrying in %.1fs",
75
+ self.group,
76
+ self.retry_delay,
77
+ )
78
+ time.sleep(self.retry_delay)
79
+ r = self._connect()
80
+
81
+ def _connect(self) -> redis_lib.Redis:
82
+ return redis_lib.from_url(
83
+ self.config.url,
84
+ decode_responses=True,
85
+ socket_timeout=self.config.socket_timeout,
86
+ socket_connect_timeout=self.config.socket_connect_timeout,
87
+ )
88
+
89
+ def _ensure_group(self, r: redis_lib.Redis) -> None:
90
+ try:
91
+ r.xgroup_create(self.stream, self.group, id="0", mkstream=True)
92
+ except redis_lib.exceptions.ResponseError as exc:
93
+ if "BUSYGROUP" not in str(exc):
94
+ raise
streambus/publisher.py ADDED
@@ -0,0 +1,40 @@
1
+ import logging
2
+
3
+ import redis as redis_lib
4
+
5
+ from streambus.config import RedisConfig
6
+ from streambus.event import StreamBusEvent
7
+ from streambus.exceptions import ConfigurationError, EventValidationError
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ class EventPublisher:
13
+ def __init__(self, stream: str, config: RedisConfig):
14
+ if not stream:
15
+ raise ConfigurationError("stream is required")
16
+ if not isinstance(config, RedisConfig):
17
+ raise ConfigurationError("config must be a RedisConfig instance")
18
+ if not config.url:
19
+ raise ConfigurationError("config.url is required")
20
+
21
+ self.stream = stream
22
+ self.config = config
23
+ self._redis: redis_lib.Redis | None = None
24
+
25
+ def publish(self, event: StreamBusEvent) -> None:
26
+ if not isinstance(event, StreamBusEvent):
27
+ raise EventValidationError("event must be a StreamBusEvent instance")
28
+ if not event.event_type:
29
+ raise EventValidationError("event.event_type is required")
30
+
31
+ self._connect().xadd(self.stream, event.to_dict())
32
+
33
+ def _connect(self) -> redis_lib.Redis:
34
+ if self._redis is None:
35
+ self._redis = redis_lib.from_url(
36
+ self.config.url,
37
+ socket_timeout=self.config.socket_timeout,
38
+ socket_connect_timeout=self.config.socket_connect_timeout,
39
+ )
40
+ return self._redis
@@ -0,0 +1,200 @@
1
+ Metadata-Version: 2.5
2
+ Name: streambus
3
+ Version: 0.1.0
4
+ Summary: Lightweight Redis Streams event listener with a clean callback API
5
+ Project-URL: Homepage, https://github.com/jmeiracorbal/streambus
6
+ Project-URL: Repository, https://github.com/jmeiracorbal/streambus
7
+ Project-URL: Issues, https://github.com/jmeiracorbal/streambus/issues
8
+ Author-email: Jose Meira <jmeiracorbal@gmail.com>
9
+ License: MIT License
10
+
11
+ Copyright (c) 2026 Jose Meira
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ SOFTWARE.
30
+ License-File: LICENSE
31
+ Keywords: consumer,event-bus,events,producer,pubsub,redis,streams
32
+ Classifier: Development Status :: 3 - Alpha
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Programming Language :: Python :: 3
36
+ Classifier: Programming Language :: Python :: 3.11
37
+ Classifier: Programming Language :: Python :: 3.12
38
+ Classifier: Topic :: Software Development :: Libraries
39
+ Classifier: Topic :: System :: Distributed Computing
40
+ Requires-Python: >=3.11
41
+ Requires-Dist: redis>=5.0
42
+ Description-Content-Type: text/markdown
43
+
44
+ # streambus
45
+
46
+ [![PyPI version](https://img.shields.io/pypi/v/streambus?color=e8445a&label=pypi)](https://pypi.org/project/streambus/)
47
+ [![Python](https://img.shields.io/badge/python-3.11%2B-4584b6?logo=python&logoColor=white)](https://www.python.org/)
48
+ [![License](https://img.shields.io/badge/license-MIT-22c55e)](LICENSE)
49
+ [![Tests](https://img.shields.io/badge/tests-18%20passed-22c55e)](#development)
50
+
51
+ Lightweight event bus over Redis Streams. Define typed events, publish from one service, consume in others without boilerplate or framework dependencies.
52
+
53
+ ## Install
54
+
55
+ ```bash
56
+ pip install streambus
57
+ ```
58
+
59
+ ```bash
60
+ uv add streambus
61
+ ```
62
+
63
+ ## Quick start
64
+
65
+ Define an event:
66
+
67
+ ```python
68
+ from dataclasses import dataclass
69
+ from streambus import StreamBusEvent
70
+
71
+ @dataclass(kw_only=True)
72
+ class OrderCreated(StreamBusEvent):
73
+ order_id: str
74
+ customer_id: str
75
+ total: str
76
+ ```
77
+
78
+ Publish it:
79
+
80
+ ```python
81
+ from streambus import EventPublisher, RedisConfig
82
+
83
+ publisher = EventPublisher(
84
+ stream="orders:events",
85
+ config=RedisConfig(url="redis://localhost:6379"),
86
+ )
87
+
88
+ publisher.publish(
89
+ OrderCreated(
90
+ event_type="order.created",
91
+ order_id="ord_123",
92
+ customer_id="cust_456",
93
+ total="99.90",
94
+ )
95
+ )
96
+ ```
97
+
98
+ Consume it:
99
+
100
+ ```python
101
+ from streambus import EventListener, RedisConfig
102
+
103
+ def handle_order(event: OrderCreated) -> None:
104
+ print(f"New order {event.order_id}: total {event.total}")
105
+
106
+ EventListener(
107
+ stream="orders:events",
108
+ group="notifications",
109
+ handler=handle_order,
110
+ event_class=OrderCreated,
111
+ config=RedisConfig(url="redis://localhost:6379"),
112
+ ).run()
113
+ ```
114
+
115
+ ## How it works
116
+
117
+ streambus uses [Redis Streams](https://redis.io/docs/data-types/streams/) for transport: persistent storage, consumer groups, and at-least-once delivery. The library manages group creation, the blocking read loop, message acknowledgement, and automatic reconnection on connection loss.
118
+
119
+ Each consumer group processes events independently. A message published to a stream reaches every group exactly once.
120
+
121
+ ## Events
122
+
123
+ All events extend `StreamBusEvent`:
124
+
125
+ | Field | Type | Required | Notes |
126
+ |---|---|---|---|
127
+ | `event_type` | `str` | yes | Identifies the event. Convention: `noun.verb` |
128
+ | `occurred_at` | `str` | no | ISO 8601 timestamp. Auto-set on creation. |
129
+
130
+ Subclasses declare only the fields they need. Unknown fields arriving from the stream are silently discarded — each consumer maps what it uses, nothing more.
131
+
132
+ Both `StreamBusEvent` and its subclasses must use `@dataclass(kw_only=True)`. This is required: the base class has a field with a default (`occurred_at`), and `kw_only=True` prevents the ordering conflict that would otherwise block subclasses from adding required fields.
133
+
134
+ ## Configuration
135
+
136
+ **RedisConfig**
137
+
138
+ ```python
139
+ RedisConfig(
140
+ url="redis://localhost:6379", # required
141
+ socket_timeout=None, # None = no timeout (correct for blocking reads)
142
+ socket_connect_timeout=5.0, # seconds to wait on initial connect
143
+ )
144
+ ```
145
+
146
+ **EventListener**
147
+
148
+ ```python
149
+ EventListener(
150
+ stream="stream:name", # required
151
+ group="consumer-group", # required
152
+ handler=my_handler, # required — callable(event) -> None
153
+ event_class=MyEvent, # required — must subclass StreamBusEvent
154
+ config=RedisConfig(...), # required
155
+ consumer=None, # defaults to "{group}-1"
156
+ batch_size=10,
157
+ block_ms=5000,
158
+ retry_delay=3.0,
159
+ )
160
+ ```
161
+
162
+ **EventPublisher**
163
+
164
+ ```python
165
+ EventPublisher(
166
+ stream="stream:name", # required
167
+ config=RedisConfig(...), # required
168
+ )
169
+ ```
170
+
171
+ ## Exceptions
172
+
173
+ | Exception | When |
174
+ |---|---|
175
+ | `ConfigurationError` | Invalid or missing parameters at construction time |
176
+ | `EventValidationError` | `publish()` receives a non-`StreamBusEvent` or empty `event_type` |
177
+ | `StreambusError` | Base class for all streambus exceptions |
178
+
179
+ ## Development
180
+
181
+ ```bash
182
+ git clone https://github.com/yourorg/streambus
183
+ cd streambus
184
+ uv sync --group dev
185
+ uv run pytest tests/ -v
186
+ ```
187
+
188
+ Run the example (requires Docker):
189
+
190
+ ```bash
191
+ cd example
192
+ docker compose up -d # start Redis
193
+ uv sync
194
+ uv run python consume.py # terminal 1
195
+ uv run python publish.py # terminal 2
196
+ ```
197
+
198
+ ## License
199
+
200
+ MIT
@@ -0,0 +1,10 @@
1
+ streambus/__init__.py,sha256=27cx-b9a7fjQZl3N_3YQ9QFEhYr18VZec8nQVhzr38g,418
2
+ streambus/config.py,sha256=Lt8j3W2i-nhsBJnCw7X5-RmStcjHXifIUkth2_GhJjM,203
3
+ streambus/event.py,sha256=6mVQ-wRjoMZ-JWawjnGBX1QFCreECi5wXbXOy5Mglo4,575
4
+ streambus/exceptions.py,sha256=JlcWovNkFmdrCKt7m6C7-ib-rx572VrhHIIcisQ5M9o,150
5
+ streambus/listener.py,sha256=MNIdBjilRqHDyvvVPLD4Fhqdq4hMd0eeGpQdTx7nxB4,3353
6
+ streambus/publisher.py,sha256=HoRyF_OWErhQp9RnQZCQ8vwZS17jhj1WcWtzeHCjgLA,1410
7
+ streambus-0.1.0.dist-info/METADATA,sha256=zEdAsq-uQrEqsqllLOpIvGDasIPG4rXCWXBAJOBd1zY,6365
8
+ streambus-0.1.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
9
+ streambus-0.1.0.dist-info/licenses/LICENSE,sha256=uFoMIS8c0UtdA2qO2wnC4EvksMXQb8pTyvY8oBMkgEg,1067
10
+ streambus-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jose Meira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.