job_info.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import time
  2. from app import models, schemas
  3. from app.services.datax import datax_create_job, datax_update_job, execute_job
  4. from app.utils.cron_utils import joint_cron_expression
  5. from sqlalchemy.orm import Session
  6. from sqlalchemy import func
  7. import app.crud as crud
  8. from configs.globals import g
  9. def create_job_info_services(db: Session, item: schemas.JobInfoCreate):
  10. create_time: int = int(time.time())
  11. item_dict = item.dict()
  12. name_item = db.query(models.JobInfo)\
  13. .filter(models.JobInfo.job_desc == func.binary(item.job_desc))\
  14. .filter(models.JobInfo.project_id == g.project_id)\
  15. .filter(models.JobInfo.delete_status == 1).first()
  16. if name_item:
  17. raise Exception('同步配置名称重复')
  18. # 定时任务对象转为cron表达式
  19. cron_expression_dict = item_dict.pop('cron_expression')
  20. cron_expression = joint_cron_expression(schemas.CronExpression(**cron_expression_dict))
  21. cron_select_type = cron_expression_dict["cron_select_type"]
  22. item_dict.update({
  23. 'cron_select_type': cron_select_type,
  24. 'job_cron': cron_expression,
  25. })
  26. # 分区信息拼接
  27. partition_info = item_dict.pop('partition_info') if "partition_info" in item_dict.keys() and item_dict['partition_info'] != '' else None
  28. partition_time = item_dict.pop('partition_time') if "partition_time" in item_dict.keys() and item_dict['partition_time'] != '' else None
  29. partition_num = item_dict.pop('partition_num') if "partition_num" in item_dict.keys() and item_dict['partition_num'] != '' else None
  30. partition_info_str = ''
  31. if partition_info is not None and partition_time is not None and partition_num is not None:
  32. partition_info_str += partition_info + ',' + str(partition_num) + ',' + partition_time
  33. elif partition_info is not None and (partition_time is None or partition_num is None):
  34. raise Exception('分区信息不完善')
  35. item_dict.update({
  36. 'partition_info': partition_info_str,
  37. })
  38. db_item = models.JobInfo(**item_dict, **{
  39. 'user_id': g.user_id,
  40. 'project_id': g.project_id,
  41. 'trigger_status': 0,
  42. 'create_time': create_time,
  43. 'update_time': create_time,
  44. 'delete_status': 1,
  45. })
  46. # 创建airflow端同步任务
  47. af_job = datax_create_job(db_item, db)
  48. # 创建本地同步任务
  49. db_item = crud.create_job_info(db, db_item)
  50. job_info = db_item.to_dict()
  51. crud.create_relation(db, db_item.id,'datax', af_job['id'])
  52. return job_info
  53. def update_job_info_services(db: Session, id: int, update_item: schemas.JobInfoUpdate):
  54. # 获取任务信息
  55. db_item = crud.get_job_info(db,id)
  56. name_item = db.query(models.JobInfo)\
  57. .filter(models.JobInfo.job_desc == func.binary(update_item.job_desc))\
  58. .filter(models.JobInfo.project_id == g.project_id)\
  59. .filter(models.JobInfo.delete_status == 1)\
  60. .filter(models.JobInfo.id != id).first()
  61. if name_item:
  62. raise Exception('同步配置名称重复')
  63. update_dict = update_item.dict(exclude_unset=True)
  64. # 定时任务对象转为cron表达式
  65. cron_expression_dict = update_dict.pop('cron_expression')
  66. cron_expression = joint_cron_expression(schemas.CronExpression(**cron_expression_dict))
  67. cron_select_type = cron_expression_dict["cron_select_type"]
  68. update_dict.update({
  69. 'cron_select_type': cron_select_type,
  70. 'job_cron': cron_expression,
  71. })
  72. # 分区信息拼接
  73. partition_info = update_dict.pop('partition_info') if "partition_info" in update_dict.keys() and update_dict['partition_info'] != '' else None
  74. partition_time = update_dict.pop('partition_time') if "partition_time" in update_dict.keys() and update_dict['partition_time'] != '' else None
  75. partition_num = update_dict.pop('partition_num') if "partition_num" in update_dict.keys() and update_dict['partition_num'] != '' else None
  76. partition_info_str = ''
  77. if partition_info is not None and partition_time is not None and partition_num is not None:
  78. partition_info_str += partition_info + ',' + str(partition_num) + ',' + partition_time
  79. elif partition_info is not None and (partition_time is None or partition_num is None):
  80. raise Exception('分区信息不完善')
  81. update_dict.update({
  82. 'partition_info': partition_info_str,
  83. })
  84. for k, v in update_dict.items():
  85. setattr(db_item, k, v)
  86. db_item.update_time = int(time.time())
  87. # 修改airflow端同步任务
  88. af_job = datax_update_job(db_item, db)
  89. crud.update_job_info(db,id,db_item)
  90. return db_item
  91. def execute_job_services(db: Session, job_id: int):
  92. relation = crud.get_af_id(db, job_id, 'datax')
  93. res = execute_job(relation.af_id)
  94. return res