datax.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import time
  2. from app import crud, models
  3. from app.utils.send_util import *
  4. from app.utils.utils import get_cmd_parameter
  5. from sqlalchemy.orm import Session
  6. def datax_create_job(job_info: models.JobInfo, db: Session):
  7. af_task = datax_create_task(job_info)
  8. cron: str = job_info.job_cron
  9. cron = cron.replace('?','*')
  10. af_job = {
  11. "tasks": [af_task],
  12. "name": job_info.job_desc,
  13. "dependence": [],
  14. "cron": cron,
  15. "desc": job_info.job_desc,
  16. "route_strategy": job_info.executor_route_strategy,
  17. "block_strategy": job_info.executor_block_strategy,
  18. "executor_timeout": job_info.executor_timeout,
  19. "executor_fail_retry_count": job_info.executor_fail_retry_count,
  20. "trigger_status": job_info.trigger_status,
  21. "job_mode":1,
  22. "job_type": 0,
  23. "user_id": 0,
  24. }
  25. res = send_post('/af/af_job', af_job)
  26. af_job = res['data']
  27. send_submit(af_job['id'])
  28. return af_job
  29. def datax_create_task(job_info: models.JobInfo):
  30. cmd_parameter = get_cmd_parameter(job_info.jvm_param)
  31. partition_list = []
  32. if job_info.partition_info is not None and job_info.partition_info != '':
  33. partition_list = job_info.partition_info.split(',')
  34. first_begin_time = int(time.time())
  35. if job_info.inc_start_time is not None and job_info.inc_start_time != '':
  36. first_begin_time = job_info.inc_start_time
  37. last_key = 'lastTime'
  38. if job_info.last_time is not None and job_info.last_time != '':
  39. last_key = job_info.last_time
  40. current_key = 'currentTime'
  41. if job_info.current_time is not None and job_info.current_time != '':
  42. current_key = job_info.current_time
  43. envs = {
  44. "first_begin_time": first_begin_time,
  45. "last_key": last_key,
  46. "current_key": current_key,
  47. "partition_key": "partition",
  48. "partition_word": partition_list[0] if len(partition_list) > 0 else 'xujiayue',
  49. "partition_format": partition_list[2] if len(partition_list) > 0 else '%Y-%m-%d',
  50. "partition_diff": partition_list[1] if len(partition_list) > 0 else 0
  51. }
  52. af_task = {
  53. "name": job_info.job_desc,
  54. "file_urls": [],
  55. "script": job_info.job_json,
  56. "cmd": "",
  57. "cmd_parameters": cmd_parameter,
  58. "envs": envs,
  59. "run_image": "",
  60. "task_type": "datax",
  61. "user_id": 0,
  62. }
  63. res = send_post('/af/af_task', af_task)
  64. af_task = res['data']
  65. return af_task
  66. def datax_update_job(job_info: models.JobInfo, db: Session):
  67. relation = crud.get_af_id(db, job_info.id, 'datax')
  68. af_job_id = relation.af_id
  69. res = send_get("/af/af_job/getOnce",af_job_id)
  70. old_af_job = res['data']
  71. old_af_task = old_af_job['tasks'][0]
  72. af_task = datax_put_task(job_info,old_af_task)
  73. cron: str = job_info.job_cron
  74. cron = cron.replace('?','*')
  75. af_job = {
  76. "tasks": [af_task],
  77. "name": job_info.job_desc,
  78. "dependence": [],
  79. "cron": cron,
  80. "desc": job_info.job_desc,
  81. "route_strategy": job_info.executor_route_strategy,
  82. "block_strategy": job_info.executor_block_strategy,
  83. "executor_timeout": job_info.executor_timeout,
  84. "executor_fail_retry_count": job_info.executor_fail_retry_count,
  85. "trigger_status": job_info.trigger_status,
  86. }
  87. res = send_put('/af/af_job', old_af_job['id'], af_job)
  88. af_job = res['data']
  89. send_submit(af_job['id'])
  90. return af_job
  91. def datax_put_task(job_info: models.JobInfo,old_af_task):
  92. cmd_parameter = get_cmd_parameter(job_info.jvm_param)
  93. partition_list = []
  94. if job_info.partition_info is not None and job_info.partition_info != '':
  95. partition_list = job_info.partition_info.split(',')
  96. first_begin_time = int(time.time())
  97. if job_info.inc_start_time is not None and job_info.inc_start_time != '':
  98. first_begin_time = job_info.inc_start_time
  99. last_key = 'lastTime'
  100. if job_info.last_time is not None and job_info.last_time != '':
  101. last_key = job_info.last_time
  102. current_key = 'currentTime'
  103. if job_info.current_time is not None and job_info.current_time != '':
  104. current_key = job_info.current_time
  105. envs = {
  106. "first_begin_time": first_begin_time,
  107. "last_key": last_key,
  108. "current_key": current_key,
  109. "partition_key": "partition",
  110. "partition_word": partition_list[0] if len(partition_list) > 0 else 'xujiayue',
  111. "partition_format": partition_list[2] if len(partition_list) > 0 else '%Y-%m-%d',
  112. "partition_diff": partition_list[1] if len(partition_list) > 0 else 0
  113. }
  114. af_task = {
  115. "name": job_info.job_desc,
  116. "file_urls": [],
  117. "script": job_info.job_json,
  118. "cmd": "",
  119. "cmd_parameters": cmd_parameter,
  120. "envs": envs,
  121. "run_image": "",
  122. }
  123. res = send_put('/af/af_task', old_af_task['id'],af_task)
  124. af_task = res['data']
  125. return af_task
  126. def on_off_control(af_job_id: int,status: int):
  127. for i in range(0,11):
  128. parsed_res = get_job_last_parsed_time(af_job_id)
  129. last_parsed_time = parsed_res['data']['last_parsed_time']
  130. if last_parsed_time:
  131. send_pause(af_job_id,status)
  132. print(f"{af_job_id}<==状态修改成功==>{last_parsed_time}")
  133. break
  134. if i >= 10:
  135. raise Exception(f"{af_job_id}==>状态修改失败")
  136. time.sleep(2)
  137. def execute_job(af_job_id: int):
  138. res = get_job_last_parsed_time(af_job_id)
  139. current_time = res['data']['last_parsed_time'] if 'last_parsed_time' in res['data'].keys() else None
  140. send_submit(af_job_id)
  141. for i in range(0,21):
  142. parsed_res = get_job_last_parsed_time(af_job_id)
  143. last_parsed_time = parsed_res['data']['last_parsed_time']
  144. if last_parsed_time and last_parsed_time != current_time:
  145. res = send_execute(af_job_id)
  146. print(f"{af_job_id}<==任务执行成功==>{last_parsed_time}")
  147. return res
  148. if i >= 20:
  149. raise Exception(f"{af_job_id}==>文件正在转化中")
  150. time.sleep(2)