dag1_calculate_hpc_worker.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import json
  2. from airflow.models import DAG,Variable
  3. from airflow.operators.bash_operator import BashOperator
  4. from airflow.operators.python_operator import PythonOperator,BranchPythonOperator
  5. from airflow.contrib.operators.ssh_operator import SSHOperator
  6. from airflow.contrib.hooks.ssh_hook import SSHHook
  7. from airflow.utils.dates import days_ago
  8. from igf_airflow.check_celery_queue import fetch_queue_list_from_redis_server
  9. from igf_airflow.check_celery_queue import calculate_new_workers
  10. args = {
  11. 'owner':'airflow',
  12. 'start_date':days_ago(2),
  13. 'provide_context': True,
  14. }
  15. hpc_hook = SSHHook(ssh_conn_id='hpc_conn')
  16. dag = DAG(
  17. dag_id='dag1_calculate_hpc_worker',
  18. catchup=False,
  19. max_active_runs=1,
  20. schedule_interval="*/15 * * * *",
  21. default_args=args,
  22. tags=['igf-lims',]
  23. )
  24. def airflow_utils_for_redis(**kwargs):
  25. """
  26. A function for dag1, TO DO
  27. """
  28. try:
  29. if 'redis_conf_file' not in kwargs:
  30. raise ValueError('redis_conf_file info is not present in the kwargs')
  31. redis_conf_file = kwargs.get('redis_conf_file')
  32. json_data = dict()
  33. with open(redis_conf_file,'r') as jp:
  34. json_data = json.load(jp)
  35. if 'redis_db' not in json_data:
  36. raise ValueError('redis_db key not present in the conf file')
  37. url = json_data.get('redis_db')
  38. queue_list = fetch_queue_list_from_redis_server(url=url)
  39. return queue_list
  40. except Exception as e:
  41. raise ValueError('Failed to run, error:{0}'.format(e))
  42. def get_new_workers(**kwargs):
  43. try:
  44. if 'ti' not in kwargs:
  45. raise ValueError('ti not present in kwargs')
  46. ti = kwargs.get('ti')
  47. active_tasks = ti.xcom_pull(task_ids='fetch_active_jobs_from_hpc')
  48. active_tasks = active_tasks.decode()
  49. active_tasks = json.loads(active_tasks)
  50. queued_tasks = ti.xcom_pull(task_ids='fetch_queue_list_from_redis')
  51. worker_to_submit,unique_queue_list = \
  52. calculate_new_workers(
  53. queue_list=queued_tasks,
  54. active_jobs_dict=active_tasks,
  55. max_workers_per_queue=3,
  56. max_total_workers=10)
  57. for key,value in worker_to_submit.items():
  58. ti.xcom_push(key=key,value=value)
  59. unique_queue_list = \
  60. [q for q in unique_queue_list if q.startswith('hpc')]
  61. return unique_queue_list
  62. except Exception as e:
  63. raise ValueError('Failed to get new workers, error: {0}'.format(e))
  64. with dag:
  65. fetch_queue_list_from_redis = \
  66. PythonOperator(
  67. task_id='fetch_queue_list_from_redis',
  68. dag=dag,
  69. python_callable=airflow_utils_for_redis,
  70. op_kwargs={"redis_conf_file":Variable.get('redis_conn_file')},
  71. queue='igf-lims'
  72. )
  73. check_hpc_queue = \
  74. SSHOperator(
  75. task_id='check_hpc_queue',
  76. ssh_hook=hpc_hook,
  77. dag=dag,
  78. command='source /etc/bashrc;qstat',
  79. queue='igf-lims'
  80. )
  81. fetch_active_jobs_from_hpc = \
  82. SSHOperator(
  83. task_id='fetch_active_jobs_from_hpc',
  84. ssh_hook=hpc_hook,
  85. dag=dag,
  86. command='source /etc/bashrc;bash /project/tgu/data2/airflow_test/github/igf-airflow-hpc/scripts/hpc/hpc_job_count_runner.sh ',
  87. do_xcom_push=True,
  88. queue='igf-lims'
  89. )
  90. calculate_new_worker_size_and_branch = \
  91. BranchPythonOperator(
  92. task_id='calculate_new_worker_size_and_branch',
  93. dag=dag,
  94. python_callable=get_new_workers,
  95. queue='igf-lims',
  96. )
  97. check_hpc_queue >> fetch_active_jobs_from_hpc
  98. calculate_new_worker_size_and_branch << [fetch_queue_list_from_redis,fetch_active_jobs_from_hpc]
  99. hpc_queue_list = Variable.get('hpc_queue_list')
  100. for q,data in hpc_queue_list.items():
  101. pbs_resource = data.get('pbs_resource')
  102. airflow_queue = data.get('airflow_queue')
  103. t = SSHOperator(
  104. task_id=q,
  105. ssh_hook=hpc_hook,
  106. dag=dag,
  107. queue='igf-lims',
  108. command="""
  109. {% if ti.xcom_pull(key=params.job_name,task_ids="calculate_new_worker_size_and_branch" ) > 1 %}
  110. source /etc/bashrc;qsub -o /dev/null -e /dev/null -k n -m n -N {{ params.job_name }} -J 1-{{ ti.xcom_pull(key=params.job_name,task_ids="calculate_new_worker_size_and_branch" ) }} {{ params.pbs_resource }} -- /project/tgu/data2/airflow_test/github/igf-airflow-hpc/scripts/hpc/airflow_worker.sh {{ params.airflow_queue }} {{ params.job_name }}
  111. {% else %}
  112. source /etc/bashrc;qsub -o /dev/null -e /dev/null -k n -m n -N {{ params.job_name }} {{ params.pbs_resource }} -- /project/tgu/data2/airflow_test/github/igf-airflow-hpc/scripts/hpc/airflow_worker.sh {{ params.airflow_queue }} {{ params.job_name }}
  113. {% endif %}
  114. """,
  115. params={'pbs_resource':pbs_resource,'airflow_queue':airflow_queue,'job_name':q}
  116. )
  117. calculate_new_worker_size_and_branch >> t