import os
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
# Get the directory path of the current file (your DAG file)
current_file_path = os.path.abspath(__file__)
# Extract the directory path
module_directory = os.path.dirname(current_file_path)
# Add the module directory to the sys.path
import sys
sys.path.append(module_directory)
# Import your Python module
from your_module import your_function
# Define default arguments
default_args = {
'owner': 'your_name',
'depends_on_past': False,
'start_date': datetime(2024, 1, 1),
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
# Instantiate the DAG object
dag = DAG(
'example_dag',
default_args=default_args,
description='Example DAG with dynamically determined module directory',
schedule_interval=timedelta(days=1),
)
# Define a PythonOperator task
python_task = PythonOperator(
task_id='execute_your_function',
python_callable=your_function,
provide_context=True, # Pass the context to your Python function if needed
dag=dag,
)
# Set the task execution order
python_task