Skip to content

Commit cbb8db7

Browse files
committed
first cut of redis pubsub [redis_pubsub_first_cut]
1 parent 8ba2a1b commit cbb8db7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/allocation/redis_pubsub.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import json
2+
import logging
3+
from dataclasses import asdict
4+
import redis
5+
6+
from allocation import config, orm, services, 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+
services.change_batch_quantity(
26+
ref=data['batchref'], qty=data['qty'],
27+
uow=unit_of_work.SqlAlchemyUnitOfWork(),
28+
)
29+
30+
31+
def publish(channel, event):
32+
logging.debug('publishing: channel=%s, event=%s', channel, event)
33+
r.publish(channel, json.dumps(asdict(event)))
34+
35+
36+
if __name__ == '__main__':
37+
main()

0 commit comments

Comments
 (0)