-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (54 loc) · 1.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import asyncio
import logging as log
import vars
from interactors.Kafka import Consumer, Kafka
from models.Creds import Creds
from utils.Handlers import Handler
from utils.utils import load_creds
def main():
credits = load_creds()
asyncio.run(_main(credits))
async def _main(credits: Creds):
log.info('Initialize Handler')
handler = Handler(credits)
log.info('Initialize Kafka')
kafka = Kafka(credits)
log.info('Initialize Consumer')
await kafka.consumer.start()
log.info('Get counts')
count = kafka.consumer.get_partitions_count()
workers = []
for partition_id in range(count):
worker_task = asyncio.create_task(worker(kafka.consumer, partition_id, handler))
workers.append(worker_task)
try:
await handler.start()
await asyncio.gather(*workers)
except asyncio.CancelledError:
log.info('Handler cancelled, stop.')
await handler.stop()
finally:
log.info('Handler stopped.')
async def worker(consumer: Consumer, number: int, handler: Handler) -> None:
log.info(f'Starting consumer #{number}')
match number:
case 0:
consumer = consumer
case _:
consumer = await consumer.fork()
while True:
try:
msg = await consumer.get_message()
log.info(f'Consumer #{number} get a new message.')
await handler.process(msg)
except asyncio.CancelledError:
log.info(f'Stopping consumer #{number}')
await consumer.stop()
log.info(f'Consumer #{number} stooped')
break
except Exception as e:
error_text = e.__class__.__name__ + ': ' + str(e)
log.critical(error_text)
if __name__ == "__main__":
log.basicConfig(level=vars.LOG_LEVEL)
main()