dag.py 4.3 KB

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