__init__.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import logging
  2. import tomllib
  3. import json
  4. import pika
  5. from redis.cluster import RedisCluster
  6. from types import SimpleNamespace
  7. from .worker import handle_message
  8. logger = logging.getLogger(__name__)
  9. def open_redis_cluster(config):
  10. cli = RedisCluster(host=config['host'], port=config['port'])
  11. logger.debug("%s", cli.get_nodes())
  12. return (cli, config['namespace'])
  13. def start_consumer(context, name, queue, config):
  14. mq_config = config['rabbitmq']
  15. connection = pika.BlockingConnection(
  16. pika.ConnectionParameters(
  17. host=mq_config['host'], port=mq_config['port'],
  18. credentials=pika.PlainCredentials(
  19. mq_config['user'], mq_config['password']),
  20. virtual_host=mq_config['virtual-host']))
  21. channel = connection.channel()
  22. def callback(ch, method, properties, body):
  23. logger.info("received message(%s,%s)",
  24. properties.message_id, properties.content_type)
  25. handle_message(context, ch, method, properties.message_id,
  26. properties.content_type, json.loads(
  27. body, object_hook=SimpleNamespace),
  28. config['app']['api-url'], config['rabbitmq']['customer-timeout'])
  29. channel.basic_consume(
  30. queue=queue, on_message_callback=callback, auto_ack=False)
  31. logger.info('start a consumer(%s) for queue(%s)', name, queue)
  32. channel.start_consuming()
  33. def launch(name, queue, config_file):
  34. logger.debug('load configuration from %s', config_file)
  35. with open(config_file, "rb") as config_fd:
  36. config = tomllib.load(config_fd)
  37. redis_cli = open_redis_cluster(config['redis'])
  38. logger.info('api-url:(%s)', config['app']['api-url'])
  39. logger.info('customer-timeout:(%s)',
  40. config['rabbitmq']['customer-timeout'])
  41. start_consumer(redis_cli, name, queue, config)