dag.py 5.5 KB

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