Skip to content

Commit d64cf6d

Browse files
committed
first cut of redis pubsub [redis_pubsub_first_cut]
1 parent 3a52cce commit d64cf6d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/allocation/redis_pubsub.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import json
2+
import logging
3+
from dataclasses import asdict
4+
import redis
5+
6+
from allocation import config, commands, events, orm, messagebus, unit_of_work
7+
8+
logger = logging.getLogger(__name__)
9+
10+
r = redis.Redis(**config.get_redis_host_and_port())
11+
12+
13+
def main():
14+
orm.start_mappers()
15+
pubsub = r.pubsub(ignore_subscribe_messages=True)
16+
pubsub.subscribe('change_batch_quantity')
17+
18+
for m in pubsub.listen():
19+
handle_change_batch_quantity(m)
20+
21+
22+
def handle_change_batch_quantity(m):
23+
logging.debug('handling %s', m)
24+
data = json.loads(m['data'])
25+
cmd = commands.ChangeBatchQuantity(ref=data['batchref'], qty=data['qty'])
26+
messagebus.handle(cmd, uow=unit_of_work.SqlAlchemyUnitOfWork())
27+
28+
29+
def publish(channel, event: events.Event):
30+
logging.debug('publishing: channel=%s, event=%s', channel, event)
31+
r.publish(channel, json.dumps(asdict(event)))
32+
33+
34+
if __name__ == '__main__':
35+
main()

0 commit comments

Comments
 (0)