__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. logger.debug("open redis cluster tcp://%s:%s/%s",
  11. config['host'], config['port'], config['namespace'])
  12. cli = RedisCluster(host=config['host'], port=config['port'])
  13. logger.debug("%s", cli.get_nodes())
  14. return (cli, config['namespace'])
  15. def start_consumer(context, name, config, queue, callback):
  16. logger.debug("open rabbitmq %s@%s:%d/%s with timeout %ds",
  17. config['user'], config['host'], config['port'], config['virtual-host'], config['customer-timeout'])
  18. connection = pika.BlockingConnection(
  19. pika.ConnectionParameters(
  20. host=config['host'], port=config['port'],
  21. credentials=pika.PlainCredentials(
  22. config['user'], config['password']),
  23. virtual_host=config['virtual-host']))
  24. channel = connection.channel()
  25. def callback(ch, method, properties, body):
  26. logger.info("received message(%s,%s)",
  27. properties.message_id, properties.content_type)
  28. handle_message(context, ch, method, properties.message_id,
  29. properties.content_type, json.loads(
  30. body, object_hook=SimpleNamespace),
  31. callback, ['customer-timeout'])
  32. channel.basic_consume(
  33. queue=queue, on_message_callback=callback, auto_ack=False)
  34. logger.info('start a consumer(%s) for queue(%s)', name, queue)
  35. channel.start_consuming()
  36. def launch(name, queue, config_file):
  37. logger.debug('load configuration from %s', config_file)
  38. with open(config_file, "rb") as config_fd:
  39. config = tomllib.load(config_fd)
  40. logger.debug('api-url:(%s)', config['app']['api-url'])
  41. redis_cli = open_redis_cluster(config['redis'])
  42. start_consumer(redis_cli, name,
  43. config['rabbitmq'], queue, config['app']['api-url'])