JobManage.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. # --- release ---
  21. # Global.aps.create_job(func=cls.job101, trigger='cron', hour=22) # 每天晚10点
  22. Global.aps.create_job(func=cls.job101, trigger='interval', seconds=600) # 每10分钟
  23. # Global.aps.create_job(func=cls.job101, trigger='interval', seconds=300) # 每5分钟
  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. 每日22点清理7天之前的日志
  33. 每日22点清理1天之前的日志(测试)
  34. todo 根据UserWorkRecordList.start_at来判断过去7天的数据
  35. """
  36. # --- log ---
  37. methods.debug_log("JobManage|49", f"------------------------------ start")
  38. # --- get log_file_uuid_list ---
  39. log_file_list = methods.get_file_path_list(save_dir)
  40. log_file_name_list = [i.split('\\')[-1] for i in log_file_list]
  41. log_file_uuid_list = [i.split('.')[0] for i in log_file_name_list]
  42. # --- test ---
  43. for log_file_uuid in log_file_uuid_list:
  44. # --- check ---
  45. file_path = os.path.join(save_dir, f"{log_file_uuid}.log")
  46. item = Global.mdb.updage_one_by_id('UserWorkRecordList', log_file_uuid)
  47. if not item:
  48. methods.remove_file(file_path)
  49. # --- check ---
  50. now_at = methods.now_ts()
  51. if now_at > item.get('end_at') + 86400 * 1:
  52. methods.remove_file(file_path)