dag.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from asyncio import current_task
  2. import time
  3. from app import crud, models
  4. from app.utils.send_util import *
  5. from app.utils.utils import get_cmd_parameter
  6. from sqlalchemy.orm import Session
  7. from app.common.hive import hiveDs
  8. from configs.settings import DefaultOption, config
  9. database_name = config.get('HIVE', 'DATABASE_NAME')
  10. def dag_create_job(dag_uuid:str,dag_script: str,db: Session):
  11. af_task = dag_create_task(dag_uuid, dag_script, db)
  12. af_job = {
  13. "tasks": [af_task],
  14. "name": "调试任务",
  15. "dependence": [],
  16. "cron": "None",
  17. "desc": "调试任务",
  18. "route_strategy": "",
  19. "block_strategy": "",
  20. "executor_timeout": 0,
  21. "executor_fail_retry_count": 0,
  22. "trigger_status": 1,
  23. "job_mode":2,
  24. "job_type": 0,
  25. "user_id": 0,
  26. }
  27. res = send_post('/af/af_job', af_job)
  28. af_job = res['data']
  29. crud.create_debug_relation(db,dag_uuid,'debug',af_job['id'])
  30. return af_job
  31. def dag_create_task(dag_uuid:str,dag_script: str,db: Session):
  32. envs = {}
  33. requirements_relation = crud.get_requirements_relation(db, dag_uuid)
  34. if requirements_relation:
  35. requirements = crud.get_requirements_status(db, dag_uuid)
  36. if requirements.status != 2:
  37. raise Exception('依赖未安装成功,不可执行')
  38. envs.update({'requirement_package_path': f'jpt/requirements/dag_{dag_uuid.lower()})'})
  39. af_task = {
  40. "name": "调试作业",
  41. "file_urls": [],
  42. "script": dag_script,
  43. "cmd": "",
  44. "cmd_parameters": "",
  45. "envs": envs,
  46. "run_image": "",
  47. "task_type": "sparks",
  48. "user_id": 0,
  49. }
  50. res = send_post('/af/af_task', af_task)
  51. af_task = res['data']
  52. return af_task
  53. def dag_update_job(dag_uuid:str,dag_script: str, db: Session):
  54. relation = crud.get_dag_af_id(db, dag_uuid, 'debug')
  55. af_job_id = relation.af_id
  56. res = send_get("/af/af_job/getOnce",af_job_id)
  57. old_af_job = res['data']
  58. old_af_task = old_af_job['tasks'][0]
  59. af_task = dag_put_task(dag_script,old_af_task)
  60. af_job = {
  61. "tasks": [af_task],
  62. "name": "调试任务",
  63. "dependence": [],
  64. "cron": "None",
  65. "desc": "调试任务",
  66. "route_strategy": "",
  67. "block_strategy": "",
  68. "executor_timeout": 0,
  69. "executor_fail_retry_count": 0,
  70. "trigger_status": 1,
  71. }
  72. res = send_put('/af/af_job', old_af_job['id'], af_job)
  73. af_job = res['data']
  74. return af_job
  75. def dag_put_task(dag_uuid:str,dag_script: str,db: Session,old_af_task):
  76. envs = {}
  77. requirements_relation = crud.get_requirements_relation(db, dag_uuid)
  78. if requirements_relation:
  79. requirements = crud.get_requirements_status(db, dag_uuid)
  80. if requirements.status != 2:
  81. raise Exception('依赖未安装成功,不可执行')
  82. envs.update({'requirement_package_path': f'jpt/requirements/dag_{dag_uuid.lower()})'})
  83. af_task = {
  84. "name": "调试作业",
  85. "file_urls": [],
  86. "script": dag_script,
  87. "cmd": "",
  88. "cmd_parameters": "",
  89. "envs": envs,
  90. "run_image": "",
  91. "task_type": "sparks",
  92. }
  93. res = send_put('/af/af_task', old_af_task['id'],af_task)
  94. af_task = res['data']
  95. return af_task
  96. def dag_job_submit(dag_uuid:str,dag_script: str,db: Session):
  97. job_relation = crud.get_dag_af_id(db,dag_uuid,'debug')
  98. af_job = None
  99. if job_relation is None:
  100. af_job = dag_create_job(dag_uuid, dag_script, db)
  101. else:
  102. af_job = dag_update_job(dag_uuid, dag_script, db)
  103. res = get_job_last_parsed_time(af_job['id'])
  104. current_time = res['data']['last_parsed_time'] if 'last_parsed_time' in res['data'].keys() else None
  105. send_submit(af_job['id'])
  106. for i in range(0,21):
  107. time.sleep(2)
  108. res = get_job_last_parsed_time(af_job['id'])
  109. last_parsed_time = res['data']['last_parsed_time']
  110. if last_parsed_time and current_time != last_parsed_time:
  111. send_pause(af_job['id'],1)
  112. send_execute(af_job['id'])
  113. print(f"{af_job['id']}<==执行成功==>{last_parsed_time}")
  114. break
  115. if i >= 20:
  116. raise Exception(f"{af_job['id']}==>执行失败")
  117. return af_job
  118. def get_tmp_table_name(dag_uuid: str, node_id: str, out_pin: str, db: Session):
  119. relation = crud.get_dag_af_id(db,dag_uuid, 'debug')
  120. job_id = relation.af_id
  121. af_job_run = crud.get_airflow_run_once_debug_mode(db,job_id)
  122. tasks = af_job_run.details['tasks'] if len(af_job_run.details['tasks'])>0 else {}
  123. task_id = None
  124. for task in tasks:
  125. t_id = task.split('_')[0]
  126. n_id = task.split('_')[1]
  127. if n_id == node_id:
  128. task_id = t_id
  129. break
  130. if task_id:
  131. table_name = f'job{job_id}_task{task_id}_subnode{node_id}_output{out_pin}_tmp'
  132. t_list = hiveDs.list_tables()
  133. table_name = table_name.lower()
  134. if table_name not in t_list:
  135. raise Exception('该节点不存在中间结果')
  136. return table_name
  137. else:
  138. raise Exception('该节点不存在中间结果')
  139. def get_transfer_table_name(project_id: str, user_id: str, name: str, ):
  140. current_time = int(time.time())
  141. return f'{database_name}.project{project_id.lower()}_user{user_id.lower()}_{name.lower()}_{current_time}'