huey_consumer.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!c:\tools\python27\python.exe
  2. import os
  3. import sys
  4. from huey.consumer import Consumer
  5. from huey.consumer_options import ConsumerConfig
  6. from huey.consumer_options import OptionParserHandler
  7. from huey.utils import load_class
  8. def err(s):
  9. sys.stderr.write('\033[91m%s\033[0m\n' % s)
  10. def load_huey(path):
  11. try:
  12. return load_class(path)
  13. except:
  14. cur_dir = os.getcwd()
  15. if cur_dir not in sys.path:
  16. sys.path.insert(0, cur_dir)
  17. return load_huey(path)
  18. err('Error importing %s' % path)
  19. raise
  20. def consumer_main():
  21. parser_handler = OptionParserHandler()
  22. parser = parser_handler.get_option_parser()
  23. options, args = parser.parse_args()
  24. if len(args) == 0:
  25. err('Error: missing import path to `Huey` instance')
  26. err('Example: huey_consumer.py app.queue.huey_instance')
  27. sys.exit(1)
  28. options = {k: v for k, v in options.__dict__.items()
  29. if v is not None}
  30. config = ConsumerConfig(**options)
  31. config.validate()
  32. huey_instance = load_huey(args[0])
  33. config.setup_logger()
  34. consumer = huey_instance.create_consumer(**config.values)
  35. consumer.run()
  36. if __name__ == '__main__':
  37. consumer_main()