You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3KB

  1. # encoding:utf-8
  2. import os
  3. import signal
  4. import sys
  5. import time
  6. from channel import channel_factory
  7. from common import const
  8. from config import load_config
  9. from plugins import *
  10. import threading
  11. def sigterm_handler_wrap(_signo):
  12. old_handler = signal.getsignal(_signo)
  13. def func(_signo, _stack_frame):
  14. logger.info("signal {} received, exiting...".format(_signo))
  15. conf().save_user_datas()
  16. if callable(old_handler): # check old_handler
  17. return old_handler(_signo, _stack_frame)
  18. sys.exit(0)
  19. signal.signal(_signo, func)
  20. def start_channel(channel_name: str):
  21. channel = channel_factory.create_channel(channel_name)
  22. if channel_name in ["wx", "wxy", "terminal", "wechatmp", "wechatmp_service", "wechatcom_app", "wework",
  23. const.FEISHU, const.DINGTALK]:
  24. PluginManager().load_plugins()
  25. if conf().get("use_linkai"):
  26. try:
  27. from common import linkai_client
  28. threading.Thread(target=linkai_client.start, args=(channel,)).start()
  29. except Exception as e:
  30. pass
  31. # try:
  32. # if channel_name in ['wx']:
  33. # from common import platform_client
  34. # threading.Thread(target=platform_client.start, args=(channel,)).start()
  35. # except Exception as e:
  36. # pass
  37. try:
  38. from common import redis_helper
  39. threading.Thread(target=redis_helper.start).start()
  40. except Exception as e:
  41. pass
  42. try:
  43. from common import kafka_helper
  44. threading.Thread(target=kafka_helper.start).start()
  45. except Exception as e:
  46. pass
  47. channel.startup()
  48. def run():
  49. try:
  50. # load config
  51. load_config()
  52. # ctrl + c
  53. sigterm_handler_wrap(signal.SIGINT)
  54. # kill signal
  55. sigterm_handler_wrap(signal.SIGTERM)
  56. # create channel
  57. channel_name = conf().get("channel_type", "wx")
  58. if "--cmd" in sys.argv:
  59. channel_name = "terminal"
  60. if channel_name == "wxy":
  61. os.environ["WECHATY_LOG"] = "warn"
  62. start_channel(channel_name)
  63. while True:
  64. time.sleep(1)
  65. except Exception as e:
  66. logger.error("App startup failed!")
  67. logger.exception(e)
  68. if __name__ == "__main__":
  69. run()