dag.py 4.9 KB

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