2

I dont know how to continue with this task. Could you please help? I am creating a dag, where a parameter by user will be passed manually. This dag is supposed to take "pid" to kill the hanging query in the postgres database. I have written a code and I cant pass the paramater to test it via cli. I am using this command : airflow tasks test killer_dag get_idle_queries 20210802 -t '{"pid":"12345"}

and this is the code: killer_dag.py

from airflow.models import DAG
from plugins.platform.utils import skyflow_email_list
from dags.utils.utils import (kill_hanging_queries,)
from airflow.models.log import Log
from airflow.utils.db import create_session
with create_session() as session:
    results = session.query(Log.dttm, Log.dag_id, Log.execution_date,
                            Log.owner, Log.extra) \
        .filter(Log.dag_id == 'killer_dag', Log.event ==
                'trigger').order_by(Log.dttm.desc()).all()
killer_dag = DAG(
    dag_id="killer_dag",
    default_args={
        "owner": "Data Intelligence: Data Platform",
        "email": skyflow_email_list,
        "email_on_failure": True,
        "email_on_retry": False,
        "depends_on_past": False,
        "start_date": datetime(2021, 8, 1, 0, 0, 0),
        "retries": 10,
        "retry_delay": timedelta(minutes=1),
        "sla": timedelta(minutes=90),
    },
    schedule_interval=timedelta(days=1),
)
kill_hanging_queries(killer_dag) 

and

utils.py

import logging
from airflow.operators.python_operator import PythonOperator
from psycopg2.extras import RealDictCursor
from plugins.platform.kw_postgres_hook import KwPostgresHook
from airflow.models import DagRun
from airflow.providers.postgres.hooks.postgres import PostgresHook

def get_idle_queries(**kwargs):
    logging.info(f"STARTING TO FETCH THE PID")
    logging.info(kwargs)
    pid= kwargs["pid"]
    logging.info(pid)
    logging.info("received pid: ", pid)
    # return 'Whatever you return gets printed in the logs'
    analdb_hook = KwPostgresHook(postgres_conn_id="anal_db")
    analdb_conn = analdb_hook.get_conn()
    analdb_cur = analdb_conn.cursor(cursor_factory=RealDictCursor)
    get_idle_queries_query = """
        SELECT pg_terminate_backend('{pid}');
    """
    analdb_cur.execute(get_idle_queries_query)
    hanging_queries = analdb_cur.fetchall()
    logging.info(f"Listing info about hanging queries {hanging_queries}")  # NORO KODO STARTO
    for record in hanging_queries:
        query = record["terminate_q"]
        logging.info(f"Running query: {query}")
        analdb_cur.execute(query)
    analdb_conn.close()
def kill_hanging_queries(killer_dag):
    PythonOperator(
        task_id="get_idle_queries",
        python_callable=get_idle_queries,
        dag=killer_dag,
        provide_context=True
    ) ```

  • Hey, I was wondering if my answer below was helpful to you. – NicoE Aug 06 '21 at 15:40
  • Thank you very much for your help, it was very helpful:). Can you help with create_session - getting the user name to be logged when the dag is triggered too,please? I have created a new stackoverflow question with more info. – Radka Žmers Aug 11 '21 at 18:27
  • Glad to hear that! Please consider marking the answer as [accepted](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). I will take a look to that other question. – NicoE Aug 11 '21 at 19:01
  • done, thank you very very much! My new question can be found here: https://stackoverflow.com/questions/68747150/airflow-log-the-user-who-triggered-the-dag – Radka Žmers Aug 11 '21 at 19:18

1 Answers1

1

To pass the params from CLI the correct way is pretty much how you did it (unless you are really missing the closing ' as in your post above):

airflow tasks test killer_dag get_idle_queries 20210802 -t '{"pid":"12345"}'

So I think the problem in your code is related to how you are trying to access those params. In get_idle_queries you could access them doing kwargs["params"]["pid"]}, like this:

def get_idle_queries(**kwargs):
    logging.info(f"STARTING TO FETCH THE PID")
    logging.info(kwargs)
    pid = kwargs["params"]["pid"]

Let me know if that worked for you.

NicoE
  • 4,373
  • 3
  • 18
  • 33