dag1_calculate_hpc_worker.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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,airflow_utils_for_redis
  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. schedule_interval="*/15 * * * *",
  19. default_args=args,
  20. tags=['igf-lims',]
  21. )
  22. def get_new_workers(**kwargs):
  23. try:
  24. if 'ti' not in kwargs:
  25. raise ValueError('ti not present in kwargs')
  26. ti = kwargs.get('ti')
  27. active_tasks = ti.xcom_pull(task_ids='fetch_active_jobs_from_hpc')
  28. active_tasks = active_tasks.decode()
  29. active_tasks = json.loads(active_tasks)
  30. queued_tasks = ti.xcom_pull(task_ids='fetch_queue_list_from_redis')
  31. worker_to_submit,unique_queue_list = \
  32. calculate_new_workers(
  33. queue_list=queued_tasks,
  34. active_jobs_dict=active_tasks)
  35. for key,value in worker_to_submit.items():
  36. ti.xcom_push(key=key,value=value)
  37. unique_queue_list = \
  38. [q for q in unique_queue_list if q.startswith('hpc')]
  39. return unique_queue_list
  40. except Exception as e:
  41. raise ValueError('Failed to get new workers, error: {0}'.format(e))
  42. with dag:
  43. fetch_queue_list_from_redis = \
  44. PythonOperator(
  45. task_id='fetch_queue_list_from_redis',
  46. dag=dag,
  47. python_callable=airflow_utils_for_redis,
  48. op_kwargs={"redis_conf_file":Variable.get('redis_conn_file')},
  49. queue='igf-lims'
  50. )
  51. check_hpc_queue = \
  52. SSHOperator(
  53. task_id='check_hpc_queue',
  54. ssh_hook=hpc_hook,
  55. dag=dag,
  56. command='source /etc/bashrc;qstat',
  57. queue='igf-lims'
  58. )
  59. fetch_active_jobs_from_hpc = \
  60. SSHOperator(
  61. task_id='fetch_active_jobs_from_hpc',
  62. ssh_hook=hpc_hook,
  63. dag=dag,
  64. command='source /etc/bashrc;bash /project/tgu/data2/airflow_test/github/igf-airflow-hpc/scripts/hpc/hpc_job_count_runner.sh ',
  65. do_xcom_push=True,
  66. queue='igf-lims'
  67. )
  68. calculate_new_worker_size_and_branch = \
  69. BranchPythonOperator(
  70. task_id='calculate_new_worker_size_and_branch',
  71. dag=dag,
  72. python_callable=get_new_workers,
  73. queue='igf-lims',
  74. )
  75. check_hpc_queue >> fetch_active_jobs_from_hpc
  76. calculate_new_worker_size_and_branch << [fetch_queue_list_from_redis,fetch_active_jobs_from_hpc]
  77. hpc_queue_list = Variable.get('hpc_queue_list')
  78. for q,data in hpc_queue_list.items():
  79. pbs_resource = data.get('pbs_resource')
  80. airflow_queue = data.get('airflow_queue')
  81. t = SSHOperator(
  82. task_id=q,
  83. ssh_hook=hpc_hook,
  84. dag=dag,
  85. queue='igf-lims',
  86. command="""
  87. {% if ti.xcom_pull(key=params.job_name,task_ids="calculate_new_worker_size_and_branch" ) > 1 %}
  88. 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 }}
  89. {% else %}
  90. 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 }}
  91. {% endif %}
  92. """,
  93. params={'pbs_resource':pbs_resource,'airflow_queue':airflow_queue,'job_name':q}
  94. )
  95. calculate_new_worker_size_and_branch >> t