Skip to content

Commit db30b8a

Browse files
committed
split out redis
1 parent 81de780 commit db30b8a

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

‎src/allocation/__init__.py‎

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import json
2+
import logging
3+
from allocation import commands, messagebus, orm, redis_pubsub, unit_of_work
4+
5+
logger = logging.getLogger(__name__)
6+
7+
8+
def main():
9+
orm.start_mappers()
10+
subscription = redis_pubsub.get_subscription('change_batch_quantity')
11+
for m in subscription:
12+
handle_change_batch_quantity(m)
13+
14+
15+
def handle_change_batch_quantity(m):
16+
logging.debug('handling %s', m)
17+
data = json.loads(m['data'])
18+
cmd = commands.ChangeBatchQuantity(ref=data['batchref'], qty=data['qty'])
19+
messagebus.handle(cmd, uow=unit_of_work.SqlAlchemyUnitOfWork())
20+
21+
22+
if __name__ == '__main__':
23+
main()

‎src/allocation/redis_pubsub.py‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,21 @@
33
from dataclasses import asdict
44
import redis
55

6-
from allocation import config, commands, events, orm, messagebus, unit_of_work
6+
from allocation import config, events
77

88
logger = logging.getLogger(__name__)
99

1010
r = redis.Redis(**config.get_redis_host_and_port())
1111

1212

13-
def main():
14-
orm.start_mappers()
13+
def get_subscription(name):
1514
pubsub = r.pubsub(ignore_subscribe_messages=True)
16-
pubsub.subscribe('change_batch_quantity')
15+
pubsub.subscribe(name)
1716

1817
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())
18+
yield m
2719

2820

2921
def publish(channel, event: events.Event):
3022
logging.debug('publishing: channel=%s, event=%s', channel, event)
3123
r.publish(channel, json.dumps(asdict(event)))
32-
33-
34-
if __name__ == '__main__':
35-
main()

0 commit comments

Comments
 (0)