office_helper.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import functools
  2. import os.path
  3. import sys
  4. import threading
  5. import time
  6. if getattr(sys, 'frozen', False):
  7. # 打包后的 exe
  8. os.chdir(os.path.dirname(sys.executable))
  9. else:
  10. # 普通 Python 脚本
  11. os.chdir(os.path.dirname(os.path.abspath(__file__)))
  12. from api.api_app import start_flask
  13. from modify_folder.monitor_file import start_watchdog
  14. from tools.logger_handle import logger
  15. from tools.serve_client import ServerClient
  16. from config import *
  17. def ApplicationInstance(func):
  18. @functools.wraps(func)
  19. def fun(*args, **kwargs):
  20. import socket
  21. try:
  22. global s
  23. s = socket.socket()
  24. host = socket.gethostname()
  25. s.bind((host, 60123))
  26. except:
  27. logger.error('不能重复打开已存在的程序')
  28. return None
  29. return func(*args, **kwargs)
  30. return fun
  31. # 启动所有后台服务
  32. @ApplicationInstance
  33. def start_all_services(serve_client, work_path):
  34. # 启动 Flask 服务
  35. threading.Thread(target=start_flask, args=[serve_client, work_path], daemon=True).start()
  36. # 启动文件监控
  37. threading.Thread(target=start_watchdog, args=[serve_client, work_path], daemon=True).start()
  38. logger.info('server running')
  39. while True:
  40. time.sleep(10)
  41. if __name__ == "__main__":
  42. if not (args.get('username') and args.get('password')):
  43. logger.error('The config is missing critical information')
  44. sys.exit(0)
  45. client = ServerClient(TARGET_URL, args['username'], args['password'])
  46. if not os.path.exists(os.path.join(args['worker_path'], args['username'])):
  47. os.makedirs(os.path.join(args['worker_path'], args['username']))
  48. start_all_services(client, os.path.join(args['worker_path'], args['username']))