dag1_calculate_hpc_worker.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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=None,
  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. return unique_queue_list
  38. except Exception as e:
  39. raise ValueError('Failed to get new workers, error: {0}'.format(e))
  40. with dag:
  41. fetch_queue_list_from_redis = \
  42. PythonOperator(
  43. task_id='fetch_queue_list_from_redis',
  44. dag=dag,
  45. python_callable=airflow_utils_for_redis,
  46. op_kwargs={"redis_conf_file":Variable.get('redis_conn_file')},
  47. queue='igf-lims'
  48. )
  49. check_hpc_queue = \
  50. SSHOperator(
  51. task_id='check_hpc_queue',
  52. ssh_hook=hpc_hook,
  53. dag=dag,
  54. command='source /etc/bashrc;qstat',
  55. queue='igf-lims'
  56. )
  57. fetch_active_jobs_from_hpc = \
  58. SSHOperator(
  59. task_id='fetch_active_jobs_from_hpc',
  60. ssh_hook=hpc_hook,
  61. dag=dag,
  62. command='source /etc/bashrc;bash /project/tgu/data2/airflow_test/github/igf-airflow-hpc/scripts/hpc/hpc_job_count_runner.sh ',
  63. do_xcom_push=True,
  64. queue='igf-lims'
  65. )
  66. calculate_new_worker_size_and_branch = \
  67. BranchPythonOperator(
  68. task_id='calculate_new_worker_size_and_branch',
  69. dag=dag,
  70. python_callable=get_new_workers,
  71. queue='igf-lims',
  72. )
  73. check_hpc_queue >> fetch_active_jobs_from_hpc
  74. calculate_new_worker_size_and_branch << [fetch_queue_list_from_redis,fetch_active_jobs_from_hpc]
  75. hpc_queue_list = Variable.get('hpc_queue_list')
  76. for q,data in hpc_queue_list.items():
  77. pbs_resource = data.get('pbs_resource')
  78. airflow_queue = data.get('airflow_queue')
  79. t = BashOperator(
  80. task_id=q,
  81. dag=dag,
  82. queue='igf-lims',
  83. bash_command='echo $pbs_resource $airflow_queue',
  84. envs={'pbs_resource':pbs_resource,'airflow_queue':airflow_queue}
  85. )
  86. calculate_new_worker_size_and_branch >> t