jm_job_info.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import base64
  2. import re
  3. from typing import Optional, List
  4. from fastapi import APIRouter, Depends, Header
  5. from sqlalchemy.orm import Session
  6. from app import models, schemas
  7. from app.common.security.auth import verify_users
  8. import app.crud as crud
  9. from app.schemas import cron_expression
  10. from app.services.jm_job import on_off_control
  11. from app.services.jm_job_info import create_jm_job_info_services, execute_job_services, get_all_timeout_jobs_services, get_requirements_status_by_job_id, update_jm_job_info_services, update_jm_job_status_services
  12. from app.utils.cron_utils import *
  13. from app.utils.send_util import send_delete, send_execute
  14. from utils.sx_time import sxtimeit
  15. from utils.sx_web import web_try
  16. from fastapi_pagination import Page, add_pagination, paginate, Params
  17. from configs.settings import DefaultOption, config
  18. job_timeout = int(config.get('JOB_CONFIG', 'timeout'))
  19. job_auth_token = config.get('JOB_CONFIG', 'auth_token')
  20. from app import get_db
  21. router = APIRouter(
  22. prefix="/jpt/jm_job_info",
  23. tags=["jm_job_info-定时任务管理"],
  24. )
  25. @router.post("/")
  26. @web_try()
  27. @sxtimeit
  28. def create_jm_job_info(item: schemas.JmJobInfoCreate, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  29. return create_jm_job_info_services(db, item)
  30. @router.get("/")
  31. @web_try()
  32. @sxtimeit
  33. def get_jm_job_infos(token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  34. res_list = []
  35. jm_job_list = crud.get_jm_job_infos(db)
  36. jm_job_ids = [job.id for job in jm_job_list]
  37. relations = crud.get_af_ids(db,jm_job_ids, 'job')
  38. af_to_datax = {relation.af_id:relation.se_id for relation in relations}
  39. af_job_runs = crud.get_airflow_runs_by_af_job_ids(db, af_to_datax.keys())
  40. res = {}
  41. for af_job_run in af_job_runs:
  42. if af_job_run.status in [2,3]:
  43. job_id = af_to_datax[int(af_job_run.job_id)]
  44. log = {
  45. "start_time": af_job_run.start_time,
  46. "execute_result": af_job_run.status,
  47. }
  48. if job_id in res.keys():
  49. res[job_id].append(log)
  50. else:
  51. res.update({job_id: [log]})
  52. for jm_job in jm_job_list:
  53. # 历史运行
  54. history = res[jm_job.id] if jm_job.id in res.keys() else []
  55. history.sort(key=lambda x: x['start_time'], reverse=True)
  56. jm_job_dict = jm_job.to_dict()
  57. history = history[0:10]
  58. history.sort(key=lambda x: x['start_time'], reverse=False)
  59. jm_job_dict.update({'history':history})
  60. requirements_status = get_requirements_status_by_job_id(db,jm_job.id)
  61. jm_job_dict.update({'requirements_status': 0 if requirements_status else 1})
  62. res_list.append(jm_job_dict)
  63. return res_list
  64. @router.get("/info")
  65. @web_try()
  66. @sxtimeit
  67. def get_jm_job_info(jm_job_id: int, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  68. jm_job = crud.get_jm_job_info(db,jm_job_id)
  69. jm_job_dict = jm_job.to_dict()
  70. nodes = crud.get_one_job_nodes(db,jm_job_id)
  71. cron_type, cron_select_type, cron_expression = jm_job_dict['cron_type'], jm_job_dict['cron_select_type'], jm_job_dict['cron_expression']
  72. cron_expression_dict = {}
  73. if cron_type == 2:
  74. cron_expression_dict = parsing_cron_expression(cron_expression)
  75. cron_expression_dict.update({
  76. 'cron_select_type': cron_select_type,
  77. 'cron_expression': cron_expression
  78. })
  79. jm_job_dict.update({
  80. 'nodes': nodes,
  81. 'cron_expression_dict': cron_expression_dict
  82. })
  83. return jm_job_dict
  84. @router.put("/")
  85. @web_try()
  86. @sxtimeit
  87. def update_jm_job_info(item: schemas.JmJobInfoUpdate, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  88. return update_jm_job_info_services(db, item)
  89. @router.delete("/")
  90. @web_try()
  91. @sxtimeit
  92. def delete_jm_job_info(jm_job_id: int, db: Session = Depends(get_db)):
  93. jm_job = crud.get_jm_job_info(db,jm_job_id)
  94. if jm_job.status == 1:
  95. raise Exception('任务未停用,不可删除')
  96. relation = crud.get_af_id(db, jm_job_id, 'job')
  97. send_delete('/af/af_job', relation.af_id)
  98. return crud.delete_jm_job_info(db,jm_job_id)
  99. @router.put("/status")
  100. @web_try()
  101. @sxtimeit
  102. def update_jm_job_status(item: schemas.JmJobInfoStatusUpdate, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  103. return update_jm_job_status_services(db, item.id, item.status)
  104. @router.post("/execute/{jm_job_id}")
  105. @web_try()
  106. @sxtimeit
  107. def execute_jm_job(jm_job_id: int, db: Session = Depends(get_db)):
  108. jm_job = crud.get_jm_job_info(db,jm_job_id)
  109. if jm_job.status == 0:
  110. raise Exception('任务已被停用')
  111. res = execute_job_services(db,jm_job_id)
  112. return res['data']
  113. @router.get("/api/{jm_job_id}")
  114. @web_try()
  115. @sxtimeit
  116. def copy_api_path(jm_job_id: int, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  117. initial = str(jm_job_id).encode('utf-8')
  118. encryption_id = base64.b64encode(initial).decode('utf-8')
  119. return f'/jpt/jm_job_info/api_execute/{encryption_id}'
  120. @router.get("/api_execute/{encryption_id}")
  121. @web_try()
  122. @sxtimeit
  123. def api_execute_jm_job(encryption_id: str, db: Session = Depends(get_db)):
  124. jm_job_id = 0
  125. try:
  126. initial = base64.b64decode(encryption_id)
  127. jm_job_id = int(initial.decode('utf-8'))
  128. except Exception as e:
  129. raise Exception('任务路径解析失败')
  130. jm_job = crud.get_jm_job_info(db,jm_job_id)
  131. if jm_job.status == 0:
  132. raise Exception('任务已被停用')
  133. res = execute_job_services(db,jm_job_id)
  134. return res['data']
  135. @router.post("/cron_expression")
  136. @web_try()
  137. @sxtimeit
  138. def get_cron_expression(cron_expression: schemas.CronExpression, token_data: schemas.TokenData = Depends(verify_users)):
  139. cron = joint_cron_expression(cron_expression)
  140. return cron
  141. @router.get("/cron_next_execute")
  142. @web_try()
  143. @sxtimeit
  144. def get_cron_next_execute(cron_expression: str, token_data: schemas.TokenData = Depends(verify_users)):
  145. execute_list = run_get_next_time(cron_expression)
  146. return execute_list
  147. @router.get("/cron_next_execute")
  148. @web_try()
  149. @sxtimeit
  150. def get_cron_next_execute(cron_expression: str, token_data: schemas.TokenData = Depends(verify_users)):
  151. execute_list = run_get_next_time(cron_expression)
  152. return execute_list
  153. @router.get("/all_timeout_jobs")
  154. @web_try()
  155. @sxtimeit
  156. def get_all_timeout_jobs(timeout: Optional[int] = job_timeout, auth_token: str = Header(), db: Session = Depends(get_db)):
  157. if auth_token != job_auth_token:
  158. raise Exception('Token校验失败')
  159. return get_all_timeout_jobs_services(db, timeout)