office_helper.py 1.7 KB

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