job.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os
  2. import stat
  3. import time
  4. from app.core.airflow.task import *
  5. from app.core.airflow.af_util import get_job_path
  6. from app.schemas import AirflowJob
  7. class AirflowJobSubmitter:
  8. @staticmethod
  9. def submit_dag(item: AirflowJob):
  10. m_compilers = {'python': PythonTaskCompiler,
  11. 'datax': DataXTaskCompiler,
  12. 'sparks': SparksTaskCompiler,
  13. 'java': JavaTaskCompiler}
  14. nodes = [m_compilers[task.task_type](item=task).translate(job_id=item.id, task_mode=item.job_mode or 1) for task
  15. in item.tasks if
  16. task.task_type != 'sparks']
  17. spark_nodes = [SparksTaskCompiler(item=task).translate(job_id=item.id, task_mode=item.job_mode or 1) for task in
  18. item.tasks if
  19. task.task_type == 'sparks']
  20. edges = []
  21. for edge in item.dependence:
  22. edges.append({"source_operator_name": f'op_{edge[0]}',
  23. "target_operator_name": f'op_{edge[1]}'})
  24. #
  25. # m_interval = {
  26. # "None": "None",
  27. # "@once": "@once",
  28. # "0 * * * *": "@hourly",
  29. # "0 0 * * *": "@daily",
  30. # "0 0 * * 0": "@weekly",
  31. # "0 0 1 * *": "@monthly",
  32. # "0 0 1 1 *": "@yearly"
  33. # }
  34. # print(f" image pull key is : {config.get('K8S', 'image_pull_key')}")
  35. parameters = {'nodes': nodes, 'spark_nodes': spark_nodes, 'edges': edges, 'dag_id': f'dag_{item.id}',
  36. 'user_name': item.user_id, 'job_id': item.id, 'trigger_status': bool(item.trigger_status),
  37. 'interval': item.cron if item.cron != 'None' else None,
  38. 'af_backend_uri': config.get('AF_BACKEND', 'uri'),
  39. 'image_pull_key': config.get('K8S', 'image_pull_key', fallback=None),
  40. 'enable_notify': True
  41. }
  42. AirflowJobSubmitter.generate_dag_on_airflow(parameters=parameters, save_path=get_job_path(job_id=item.id))
  43. @staticmethod
  44. def generate_dag_on_airflow(parameters, save_path):
  45. env = Environment(
  46. loader=PackageLoader('app.core.airflow'),
  47. autoescape=select_autoescape()
  48. )
  49. template = env.get_template("dag_template.py.jinja2")
  50. dag_content = template.render(parameters)
  51. print(f'finish build:{dag_content}')
  52. with open(save_path, "w") as fh:
  53. fh.write(dag_content)
  54. os.chmod(save_path, stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU)
  55. print(f'write dag to {save_path}')
  56. @staticmethod
  57. def auto_submit_data_transfer():
  58. job_id = 0
  59. user_id = 0
  60. spark_task_demo = SparksTaskCompiler(item=None)
  61. spark_nodes = [
  62. {
  63. "sub_nodes": [{
  64. "name": 'read_and_save',
  65. "id": 0,
  66. "image": spark_task_demo.default_image,
  67. "cmds": ['/bin/bash', '-c', spark_task_demo.cmd_str(name='spark_data_transfer')],
  68. "env": {"SCRIPT": spark_task_demo.render_spark_script(
  69. parameters={"hive_metastore_uris": config.get('HIVE_METASTORE', 'uris')},
  70. template_file="data_transfer_dag_template.py.jinja2")
  71. },
  72. }],
  73. "edges": [],
  74. "name": 'data_save',
  75. "desc": 'task for data saving',
  76. "id": 0,
  77. }
  78. ]
  79. parameters = {'nodes': [], 'spark_nodes': spark_nodes, 'edges': [], 'dag_id': f'dag_{job_id}',
  80. 'user_name': user_id, 'job_id': job_id, 'trigger_status': False,
  81. 'interval': None,
  82. 'af_backend_uri': config.get('AF_BACKEND', 'uri'),
  83. 'image_pull_key': config.get('K8S', 'image_pull_key', fallback=None),
  84. 'enable_notify': False
  85. }
  86. AirflowJobSubmitter.generate_dag_on_airflow(parameters=parameters, save_path=get_job_path(job_id=job_id))
  87. print('create data transfer job success!')
  88. @staticmethod
  89. def auto_submit_requirements_install():
  90. job_id = -1
  91. user_id = 0
  92. cmds = []
  93. if config.get('K8S', 'enable_kerberos', fallback=None) in ['true', "True", True]:
  94. principal = config.get('HIVE', 'principal', fallback=None)
  95. cmds.append( f"kinit -kt /workspace/conf/user.keytab {principal}")
  96. cmds.append("cd /workspace")
  97. cmds.append("./py37/bin/pip3 install {{ dag_run.conf.get('requirements_str','numpy') }} -i https://pypi.douban.com/simple/")
  98. cmds.append("zip -q -r /tmp/py37.zip py37")
  99. cmds.append("hdfs dfs -put -f /tmp/py37.zip {{dag_run.conf.get('target_path','/tmp/basic.zip')}} ")
  100. py_task_demo = PythonTaskCompiler(item=AirflowTask(id=0, task_type="python", user_id=user_id, cmd_parameters="",
  101. create_time=0, update_time=0, name="requirements_install",
  102. file_urls=[], script="", cmd=' && '.join(cmds), envs={},
  103. run_image=SparksTaskCompiler(item=None).default_image))
  104. parameters = {'nodes': [py_task_demo.translate(job_id=job_id, task_mode=1)], 'spark_nodes': [], 'edges': [],
  105. 'dag_id': f'dag_{job_id}',
  106. 'user_name': user_id, 'job_id': job_id, 'trigger_status': False,
  107. 'interval': None,
  108. 'af_backend_uri': config.get('AF_BACKEND', 'uri'),
  109. 'image_pull_key': config.get('K8S', 'image_pull_key', fallback=None),
  110. 'enable_notify': False
  111. }
  112. AirflowJobSubmitter.generate_dag_on_airflow(parameters=parameters, save_path=get_job_path(job_id=job_id))
  113. print('create requirements_install success!')