jm_job_info.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import datetime
  2. import croniter
  3. import re
  4. from typing import Optional, List
  5. from fastapi import APIRouter
  6. from fastapi import Depends
  7. from sqlalchemy.orm import Session
  8. from app import models, schemas
  9. import app.crud as crud
  10. from app.schemas import cron_expression
  11. from app.utils.cron_utils import *
  12. from app.utils.send_util import send_delete, send_execute
  13. from utils.sx_time import sxtimeit
  14. from utils.sx_web import web_try
  15. from fastapi_pagination import Page, add_pagination, paginate, Params
  16. from app import get_db
  17. router = APIRouter(
  18. prefix="/jpt/jm_job_info",
  19. tags=["jm_job_info-定时任务管理"],
  20. )
  21. @router.post("/")
  22. @web_try()
  23. @sxtimeit
  24. def create_jm_job_info(item: schemas.JmJobInfoCreate, db: Session = Depends(get_db)):
  25. jm_job_info,nodes,edges = crud.create_jm_job_info(db, item)
  26. job_id = jm_job_info.id
  27. create_jm_job_node(db, nodes, edges, job_id)
  28. return jm_job_info.to_dict()
  29. def create_jm_job_node(db: Session, nodes, edges, job_id):
  30. uuid_node_id = {}
  31. if nodes is None or len(nodes) == 0:
  32. return
  33. for node in nodes:
  34. uuid = node['id']
  35. node_item = models.JmJobNode(**{
  36. 'job_id': job_id,
  37. 'homework_id': node['homework_id'],
  38. 'homework_name': node['homework_name'],
  39. 'start_point': 1,
  40. })
  41. node_item = crud.create_jm_job_node(db,node_item)
  42. node_id = node_item.id
  43. uuid_node_id.update({uuid:node_id})
  44. if nodes is None or len(nodes) == 0:
  45. return
  46. for edge in edges:
  47. edge_item = models.JmJobEdge(**{
  48. 'job_id': job_id,
  49. 'in_node_id': uuid_node_id[edge['source']],
  50. 'out_node_id': uuid_node_id[edge['target']]
  51. })
  52. edge = crud.create_jm_job_edge(db,edge_item)
  53. return
  54. @router.get("/")
  55. @web_try()
  56. @sxtimeit
  57. def get_jm_job_infos(db: Session = Depends(get_db)):
  58. res_list = []
  59. jm_job_list = crud.get_jm_job_infos(db)
  60. jm_job_ids = [job.id for job in jm_job_list]
  61. relations = crud.get_af_ids(db,jm_job_ids, 'job')
  62. af_to_datax = {relation.af_id:relation.se_id for relation in relations}
  63. af_job_runs = crud.get_airflow_runs_by_af_job_ids(db, af_to_datax.keys())
  64. res = {}
  65. for af_job_run in af_job_runs:
  66. tasks = list(af_job_run.details['tasks'].values()) if len(list(af_job_run.details['tasks'].values()))>0 else []
  67. if len(tasks) > 0:
  68. task = tasks[-1]
  69. task.pop('log',None)
  70. job_id = af_to_datax[int(af_job_run.job_id)]
  71. log = {
  72. "id": af_job_run.id,
  73. "job_id": job_id,
  74. "af_job_id": int(af_job_run.job_id),
  75. "run_id": af_job_run.run_id,
  76. "trigger_time": af_job_run.start_time,
  77. "trigger_result": 1 if task else 0,
  78. "execute_time": task['start_time'] if task else 0,
  79. "execute_result": 1 if task and task['status'] == 'success' else 0,
  80. "end_time": task['end_time'] if task else 0,
  81. }
  82. if job_id in res.keys():
  83. res[job_id].append(log)
  84. else:
  85. res.update({job_id: [log]})
  86. for jm_job in jm_job_list:
  87. history = res[jm_job.id] if jm_job.id in res.keys() else []
  88. jm_job_dict = jm_job.to_dict()
  89. jm_job_dict.update({'history':history[0:10]})
  90. res_list.append(jm_job_dict)
  91. return res_list
  92. @router.get("/info")
  93. @web_try()
  94. @sxtimeit
  95. def get_jm_job_info(jm_job_id: int, db: Session = Depends(get_db)):
  96. jm_job = crud.get_jm_job_info(db,jm_job_id)
  97. jm_job_dict = jm_job.to_dict()
  98. nodes = crud.get_one_job_nodes(db,jm_job_id)
  99. cron_type, cron_select_type, cron_expression = jm_job_dict['cron_type'], jm_job_dict['cron_select_type'], jm_job_dict['cron_expression']
  100. cron_expression_dict = {}
  101. if cron_type == 2:
  102. cron_expression_dict = parsing_cron_expression(cron_expression)
  103. cron_expression_dict.update({
  104. 'cron_select_type': cron_select_type,
  105. 'cron_expression': cron_expression
  106. })
  107. jm_job_dict.update({
  108. 'nodes': nodes,
  109. 'cron_expression_dict': cron_expression_dict
  110. })
  111. return jm_job_dict
  112. @router.put("/")
  113. @web_try()
  114. @sxtimeit
  115. def update_jm_job_info(item: schemas.JmJobInfoUpdate, db: Session = Depends(get_db)):
  116. jm_job_info,nodes,edges = crud.update_jm_job_info(db, item)
  117. job_id = jm_job_info.id
  118. crud.delete_job_node(db, job_id)
  119. job_id = jm_job_info.id
  120. create_jm_job_node(db, nodes, edges, job_id)
  121. return jm_job_info.to_dict()
  122. @router.delete("/")
  123. @web_try()
  124. @sxtimeit
  125. def delete_jm_job_info(jm_job_id: int, db: Session = Depends(get_db)):
  126. relation = crud.get_af_id(db, jm_job_id, 'job')
  127. send_delete('/jpt/af_job', relation.af_id)
  128. return crud.delete_jm_job_info(db,jm_job_id)
  129. @router.put("/status")
  130. @web_try()
  131. @sxtimeit
  132. def update_jm_job_status(item: schemas.JmJobInfoStatusUpdate, db: Session = Depends(get_db)):
  133. return crud.update_jm_job_status(db,item)
  134. @router.post("/execute/{jm_job_id}")
  135. @web_try()
  136. @sxtimeit
  137. def execute_jm_job(jm_job_id: int, db: Session = Depends(get_db)):
  138. jm_job = crud.get_jm_job_info(db,jm_job_id)
  139. if jm_job.status == 0:
  140. raise Exception('任务已被停用')
  141. relation = crud.get_af_id(db, jm_job_id, 'job')
  142. res = send_execute(relation.af_id)
  143. return res['data']
  144. @router.post("/cron_expression")
  145. @web_try()
  146. @sxtimeit
  147. def get_cron_expression(cron_expression: schemas.CronExpression):
  148. print(cron_expression)
  149. cron = joint_cron_expression(cron_expression)
  150. return cron
  151. @router.get("/cron_next_execute")
  152. @web_try()
  153. @sxtimeit
  154. def get_cron_next_execute(cron_expression: str):
  155. execute_list = run_get_next_time(cron_expression)
  156. return execute_list
  157. def run_get_next_time(cron_expression):
  158. now = datetime.datetime.now()
  159. cron_str = cron_expression.replace('?','*')
  160. cron = croniter.croniter(cron_str, now)
  161. execute_list = []
  162. for i in range(0, 5):
  163. next_time = cron.get_next(datetime.datetime).strftime("%Y-%m-%d %H:%M")
  164. execute_list.append(next_time)
  165. return execute_list