__init__.py 2.0 KB

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