jm_job_info.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.decorators import verify_all
  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("/", dependencies=[Depends(verify_all)])
  26. @web_try()
  27. @sxtimeit
  28. def create_jm_job_info(item: schemas.JmJobInfoCreate, db: Session = Depends(get_db)):
  29. return create_jm_job_info_services(db, item)
  30. @router.get("/", dependencies=[Depends(verify_all)])
  31. @web_try()
  32. @sxtimeit
  33. def get_jm_job_infos(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. history = res[jm_job.id] if jm_job.id in res.keys() else []
  54. history.sort(key=lambda x: x['start_time'], reverse=True)
  55. jm_job_dict = jm_job.to_dict()
  56. history = history[0:10]
  57. history.sort(key=lambda x: x['start_time'], reverse=False)
  58. jm_job_dict.update({'history':history})
  59. requirements_status = get_requirements_status_by_job_id(db,jm_job.id)
  60. jm_job_dict.update({'requirements_status': 0 if requirements_status else 1})
  61. res_list.append(jm_job_dict)
  62. return res_list
  63. @router.get("/info", dependencies=[Depends(verify_all)])
  64. @web_try()
  65. @sxtimeit
  66. def get_jm_job_info(jm_job_id: int, db: Session = Depends(get_db)):
  67. jm_job = crud.get_jm_job_info(db,jm_job_id)
  68. jm_job_dict = jm_job.to_dict()
  69. nodes = crud.get_one_job_nodes(db,jm_job_id)
  70. cron_type, cron_select_type, cron_expression = jm_job_dict['cron_type'], jm_job_dict['cron_select_type'], jm_job_dict['cron_expression']
  71. cron_expression_dict = {}
  72. if cron_type == 2:
  73. cron_expression_dict = parsing_cron_expression(cron_expression)
  74. cron_expression_dict.update({
  75. 'cron_select_type': cron_select_type,
  76. 'cron_expression': cron_expression
  77. })
  78. jm_job_dict.update({
  79. 'nodes': nodes,
  80. 'cron_expression_dict': cron_expression_dict
  81. })
  82. return jm_job_dict
  83. @router.put("/", dependencies=[Depends(verify_all)])
  84. @web_try()
  85. @sxtimeit
  86. def update_jm_job_info(item: schemas.JmJobInfoUpdate, db: Session = Depends(get_db)):
  87. return update_jm_job_info_services(db, item)
  88. @router.delete("/", dependencies=[Depends(verify_all)])
  89. @web_try()
  90. @sxtimeit
  91. def delete_jm_job_info(jm_job_id: int, db: Session = Depends(get_db)):
  92. jm_job = crud.get_jm_job_info(db,jm_job_id)
  93. if jm_job.status == 1:
  94. raise Exception('任务未停用,不可删除')
  95. relation = crud.get_af_id(db, jm_job_id, 'job')
  96. send_delete('/af/af_job', relation.af_id)
  97. return crud.delete_jm_job_info(db,jm_job_id)
  98. @router.put("/status", dependencies=[Depends(verify_all)])
  99. @web_try()
  100. @sxtimeit
  101. def update_jm_job_status(item: schemas.JmJobInfoStatusUpdate, db: Session = Depends(get_db)):
  102. return update_jm_job_status_services(db, item.id, item.status)
  103. @router.post("/execute/{jm_job_id}", dependencies=[Depends(verify_all)])
  104. @web_try()
  105. @sxtimeit
  106. def execute_jm_job(jm_job_id: int, db: Session = Depends(get_db)):
  107. jm_job = crud.get_jm_job_info(db,jm_job_id)
  108. if jm_job.status == 0:
  109. raise Exception('任务已被停用')
  110. res = execute_job_services(db,jm_job_id)
  111. return res['data']
  112. @router.get("/api/{jm_job_id}", dependencies=[Depends(verify_all)])
  113. @web_try()
  114. @sxtimeit
  115. def copy_api_path(jm_job_id: int, db: Session = Depends(get_db)):
  116. initial = str(jm_job_id).encode('utf-8')
  117. encryption_id = base64.b64encode(initial).decode('utf-8')
  118. return f'/jpt/jm_job_info/api_execute/{encryption_id}'
  119. @router.get("/api_execute/{encryption_id}")
  120. @web_try()
  121. @sxtimeit
  122. def api_execute_jm_job(encryption_id: str, db: Session = Depends(get_db)):
  123. jm_job_id = 0
  124. try:
  125. initial = base64.b64decode(encryption_id)
  126. jm_job_id = int(initial.decode('utf-8'))
  127. except Exception as e:
  128. raise Exception('任务路径解析失败')
  129. jm_job = crud.get_jm_job_info(db,jm_job_id)
  130. if jm_job.status == 0:
  131. raise Exception('任务已被停用')
  132. res = execute_job_services(db,jm_job_id)
  133. return res['data']
  134. @router.post("/cron_expression", dependencies=[Depends(verify_all)])
  135. @web_try()
  136. @sxtimeit
  137. def get_cron_expression(cron_expression: schemas.CronExpression):
  138. cron = joint_cron_expression(cron_expression)
  139. return cron
  140. @router.get("/cron_next_execute", dependencies=[Depends(verify_all)])
  141. @web_try()
  142. @sxtimeit
  143. def get_cron_next_execute(cron_expression: str):
  144. execute_list = run_get_next_time(cron_expression)
  145. return execute_list
  146. @router.get("/all_timeout_jobs")
  147. @web_try()
  148. @sxtimeit
  149. def get_all_timeout_jobs(timeout: Optional[int] = job_timeout, auth_token: str = Header(), db: Session = Depends(get_db)):
  150. if auth_token != job_auth_token:
  151. raise Exception('Token校验失败')
  152. return get_all_timeout_jobs_services(db, timeout)