streams.processor
processor
codex_platform.streams.processor
Redis Stream processing engine — continuous polling with consumer group.
Implements at-least-once delivery via XACK. Generic: works with any project, not tied to any framework (bot, Django, FastAPI, etc.).
Usage::
from codex_platform.streams import StreamConsumer
from codex_platform.streams.processor import StreamProcessor, StreamStorageProtocol
# Option 1: Use StreamConsumer directly (implements StreamStorageProtocol)
consumer = StreamConsumer(redis_client, "events:bot", "bot_group", "worker_1")
processor = StreamProcessor(storage=consumer, ...)
# Option 2: Implement StreamStorageProtocol yourself (e.g. for testing)
class FakeStorage:
async def create_group(self, stream, group): ...
async def read_events(self, stream, group, consumer, count): return []
async def ack_event(self, stream, group, message_id): ...
Classes
StreamStorageProtocol
Bases: Protocol
Adapter protocol for Redis Stream I/O.
StreamConsumer from streams.consumer implements this protocol.
You can also implement it yourself for testing or alternative backends.
Source code in src/codex_platform/streams/processor.py
Functions
create_group(stream_name, group_name)
async
read_events(stream_name, group_name, consumer_name, count)
async
Reads undelivered messages for the group (XREADGROUP ... >).
Returns:
| Type | Description |
|---|---|
list[tuple[str, dict[str, Any]]]
|
List of (message_id, data_dict). Empty if no messages. |
Source code in src/codex_platform/streams/processor.py
StreamProcessor
Async engine for consuming and dispatching Redis Stream events.
Runs a continuous background polling loop. On each iteration reads a batch of messages from the consumer group, dispatches each to the registered callback, and ACKs on success.
If the callback fails — message stays in PEL (unacknowledged) for future recovery. The processor recovers automatically if the consumer group is lost (NOGROUP error).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
StreamStorageProtocol
|
Any object implementing |
required |
stream_name
|
str
|
Redis Stream identifier. |
required |
consumer_group_name
|
str
|
Shared group name (same across all instances = load balancing). |
required |
consumer_name
|
str
|
Unique name for this processor instance. |
required |
batch_count
|
int
|
Max messages per poll cycle. |
10
|
poll_interval
|
float
|
Sleep duration (seconds) when no messages available. |
1.0
|
TODO
PEL recovery on startup — XPENDING/XCLAIM for messages stuck in PEL after a processor crash.
Example::
consumer = StreamConsumer(redis, "events:bot", "bot_group", "worker_1")
processor = StreamProcessor(
storage=consumer,
stream_name="events:bot",
consumer_group_name="bot_group",
consumer_name="worker_1",
)
processor.set_callback(my_async_handler)
await processor.start()
# ... later
await processor.stop()
Source code in src/codex_platform/streams/processor.py
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 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
Functions
set_callback(callback)
Registers the message handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
MessageCallback
|
|
required |
start()
async
Starts the background polling loop.
Creates the consumer group (up to 5 attempts with 3s delay),
then spawns _consume_loop as an asyncio Task.
Source code in src/codex_platform/streams/processor.py
stop()
async
Stops the polling loop gracefully.