jm_job_info.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 utils.sx_time import sxtimeit
  13. from utils.sx_web import web_try
  14. from fastapi_pagination import Page, add_pagination, paginate, Params
  15. from app import get_db
  16. router = APIRouter(
  17. prefix="/jpt/jm_job_info",
  18. tags=["jm_job_info-定时任务管理"],
  19. )
  20. @router.post("/")
  21. @web_try()
  22. @sxtimeit
  23. def create_jm_job_info(item: schemas.JmJobInfoCreate, db: Session = Depends(get_db)):
  24. jm_job_info,nodes,edges = crud.create_jm_job_info(db, item)
  25. job_id = jm_job_info.id
  26. create_jm_job_node(db, nodes, edges, job_id)
  27. return jm_job_info.to_dict()
  28. def create_jm_job_node(db: Session, nodes, edges, job_id):
  29. uuid_node_id = {}
  30. if nodes is None or len(nodes) == 0:
  31. return
  32. for node in nodes:
  33. uuid = node['id']
  34. node_item = models.JmJobNode(**{
  35. 'job_id': job_id,
  36. 'homework_id': node['homework_id'],
  37. 'homework_name': node['homework_name'],
  38. 'start_point': 1,
  39. })
  40. node_item = crud.create_jm_job_node(db,node_item)
  41. node_id = node_item.id
  42. uuid_node_id.update({uuid:node_id})
  43. if nodes is None or len(nodes) == 0:
  44. return
  45. for edge in edges:
  46. edge_item = models.JmJobEdge(**{
  47. 'job_id': job_id,
  48. 'in_node_id': uuid_node_id[edge['source']],
  49. 'out_node_id': uuid_node_id[edge['target']]
  50. })
  51. edge = crud.create_jm_job_edge(db,edge_item)
  52. return
  53. @router.get("/")
  54. @web_try()
  55. @sxtimeit
  56. def get_jm_job_infos(db: Session = Depends(get_db)):
  57. res_list = []
  58. jm_job_list = crud.get_jm_job_infos(db)
  59. for jm_job in jm_job_list:
  60. history = crud.get_one_job_historys(db, jm_job.id)
  61. jm_job_dict = jm_job.to_dict()
  62. jm_job_dict.update({'history':history[0:10]})
  63. res_list.append(jm_job_dict)
  64. return res_list
  65. @router.get("/info")
  66. @web_try()
  67. @sxtimeit
  68. def get_jm_job_info(jm_job_id: int, db: Session = Depends(get_db)):
  69. jm_job = crud.get_jm_job_info(db,jm_job_id)
  70. jm_job_dict = jm_job.to_dict()
  71. nodes = crud.get_one_job_nodes(db,jm_job_id)
  72. cron_type, cron_select_type, cron_expression = jm_job_dict['cron_type'], jm_job_dict['cron_select_type'], jm_job_dict['cron_expression']
  73. cron_expression_dict = {}
  74. if cron_type == 2:
  75. cron_expression_dict = parsing_cron_expression(cron_expression)
  76. cron_expression_dict.update({
  77. 'cron_select_type': cron_select_type,
  78. 'cron_expression': cron_expression
  79. })
  80. jm_job_dict.update({
  81. 'nodes': nodes,
  82. 'cron_expression_dict': cron_expression_dict
  83. })
  84. return jm_job_dict
  85. @router.put("/")
  86. @web_try()
  87. @sxtimeit
  88. def update_jm_job_info(item: schemas.JmJobInfoUpdate, db: Session = Depends(get_db)):
  89. jm_job_info,nodes,edges = crud.update_jm_job_info(db, item)
  90. job_id = jm_job_info.id
  91. crud.delete_job_node(db, job_id)
  92. job_id = jm_job_info.id
  93. create_jm_job_node(db, nodes, edges, job_id)
  94. return jm_job_info.to_dict()
  95. @router.delete("/")
  96. @web_try()
  97. @sxtimeit
  98. def delete_jm_job_info(jm_job_id: int, db: Session = Depends(get_db)):
  99. return crud.delete_jm_job_info(db,jm_job_id)
  100. @router.put("/status")
  101. @web_try()
  102. @sxtimeit
  103. def update_jm_job_status(item: schemas.JmJobInfoStatusUpdate, db: Session = Depends(get_db)):
  104. return crud.update_jm_job_status(db,item)
  105. @router.post("/execute/{jm_job_id}")
  106. @web_try()
  107. @sxtimeit
  108. def execute_jm_job(jm_job_id: int, db: Session = Depends(get_db)):
  109. jm_job = crud.get_jm_job_info(db,jm_job_id)
  110. if jm_job.status == 0:
  111. raise Exception('任务已被停用')
  112. # 进行api调用
  113. return jm_job
  114. @router.post("/cron_expression")
  115. @web_try()
  116. @sxtimeit
  117. def get_cron_expression(cron_expression: schemas.CronExpression):
  118. print(cron_expression)
  119. cron = joint_cron_expression(cron_expression)
  120. return cron
  121. @router.get("/cron_next_execute")
  122. @web_try()
  123. @sxtimeit
  124. def get_cron_next_execute(cron_expression: str):
  125. execute_list = run_get_next_time(cron_expression)
  126. return execute_list
  127. def run_get_next_time(cron_expression):
  128. now = datetime.datetime.now()
  129. cron_str = cron_expression.replace('?','*')
  130. cron = croniter.croniter(cron_str, now)
  131. execute_list = []
  132. for i in range(0, 5):
  133. next_time = cron.get_next(datetime.datetime).strftime("%Y-%m-%d %H:%M")
  134. execute_list.append(next_time)
  135. return execute_list