JobManage.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from hub import Global, methods
  2. import os
  3. # --- for linux
  4. # save_dir = f'/home/server/logs'
  5. # --- for windows
  6. save_dir = r'C:\logs' # sri内网测试环境
  7. class JobManage(object):
  8. """"""
  9. # --- 接口认证信息 ---
  10. # username = '500A7062'
  11. # password = '198797#cjhxbin'
  12. @classmethod
  13. def run(cls):
  14. """启动全部任务"""
  15. # --- test ---
  16. # Global.aps.create_job(func=cls.job101, trigger='interval', seconds=3) # 循环测试
  17. # Global.aps.create_job(func=cls.job101, trigger='date', run_date='2022-02-23 18:54:00') # 定时测试
  18. # Global.aps.create_job(func=cls.job301, trigger='interval', seconds=60) # 循环测试
  19. # Global.aps.create_job(func=cls.job20102, trigger='date', run_date='2022-07-28 17:15:30') # 定时测试
  20. # Global.aps.create_job(func=cls.job101, trigger='cron', hour=22) # 每天晚10点
  21. # Global.aps.create_job(func=cls.job101, trigger='interval', seconds=60) # 每1分钟
  22. # Global.aps.create_job(func=cls.job101, trigger='interval', seconds=300) # 每5分钟
  23. # --- release ---
  24. Global.aps.create_job(func=cls.job101, trigger='interval', seconds=3600) # 每1小时
  25. @classmethod
  26. def end(cls):
  27. """暂停全部任务"""
  28. Global.aps.pause_all()
  29. @staticmethod
  30. def job101():
  31. """
  32. 每1小时清理7*24小时之前的日志
  33. """
  34. # --- log ---
  35. methods.debug_log("JobManage|49", f"------------------------------ start job101")
  36. # --- get log_file_uuid_list ---
  37. log_file_list = methods.get_file_path_list(save_dir)
  38. log_file_name_list = [i.split('\\')[-1] for i in log_file_list]
  39. log_file_uuid_list = [i.split('.')[0] for i in log_file_name_list]
  40. # --- test ---
  41. for log_file_uuid in log_file_uuid_list:
  42. # --- check ---
  43. file_path = os.path.join(save_dir, f"{log_file_uuid}.log")
  44. item = Global.mdb.get_one_by_id('UserWorkRecordList', log_file_uuid)
  45. if not item:
  46. methods.remove_file(file_path)
  47. # --- check ---
  48. now_at = methods.now_ts()
  49. if now_at > item.get('end_at') + 86400 * 1:
  50. methods.remove_file(file_path)