-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Update Episcanner-Downloader DAG with Improved PSQL Connection and Schedule #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
59e3a1c
feat: Correct issues in episcanner-downloader DAG
esloch 8d7e6ad
chore: Update Episcanner-Downloader DAG
esloch 199f588
chore(CI): Update variables connections in the CI
esloch 78e603e
chore(dags): Add a task to remove the episcanner-downloader repositor…
esloch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import os | ||
| from datetime import datetime, timedelta | ||
|
|
||
| from airflow import DAG | ||
| from airflow.models import Variable | ||
| from airflow.operators.bash import BashOperator | ||
| from airflow.operators.python import PythonOperator | ||
| from dotenv import dotenv_values, set_key | ||
|
|
||
|
|
||
| def set_airflow_variables(): | ||
| """ | ||
| Set Airflow variables from environment and write them to the .env file. | ||
| """ | ||
| # Set Airflow variables from environment variables | ||
| PSQL_USER = os.environ.get('AIRFLOW_PSQL_USER_MAIN') | ||
| PSQL_PASSWORD = os.environ.get('AIRFLOW_PSQL_PASSWORD_MAIN') | ||
| PSQL_HOST = os.environ.get('AIRFLOW_PSQL_HOST_MAIN') | ||
| PSQL_PORT = os.environ.get('AIRFLOW_PSQL_PORT_MAIN') | ||
| PSQL_DB = os.environ.get('AIRFLOW_PSQL_DB_MAIN') | ||
|
|
||
| Variable.set('PSQL_USER', PSQL_USER) | ||
| Variable.set('PSQL_PASSWORD', PSQL_PASSWORD) | ||
| Variable.set('PSQL_HOST', PSQL_HOST) | ||
| Variable.set('PSQL_PORT', PSQL_PORT) | ||
| Variable.set('PSQL_DB', PSQL_DB) | ||
|
|
||
| # Write variables to .env file | ||
| dotenv_path = '/opt/airflow/episcanner-downloader/.env' | ||
| env_vars = dotenv_values(dotenv_path) | ||
| env_vars['PSQL_USER'] = PSQL_USER | ||
| env_vars['PSQL_PASSWORD'] = PSQL_PASSWORD | ||
| env_vars['PSQL_HOST'] = PSQL_HOST | ||
| env_vars['PSQL_PORT'] = PSQL_PORT | ||
| env_vars['PSQL_DB'] = PSQL_DB | ||
| for key, value in env_vars.items(): | ||
| set_key(dotenv_path, key, value) | ||
|
|
||
|
|
||
| default_args = { | ||
| 'owner': 'airflow', | ||
| 'depends_on_past': False, | ||
| 'start_date': datetime(2023, 5, 21), | ||
| 'retries': 1, | ||
| 'retry_delay': timedelta(minutes=5), | ||
| } | ||
|
|
||
| with DAG( | ||
| 'EPISCANNER_DOWNLOADER', | ||
| default_args=default_args, | ||
| schedule_interval='0 3 * * 0', # Every Sunday at 3 AM | ||
| catchup=False, | ||
| ) as dag: | ||
|
|
||
| # clone the repository from GitHub | ||
| clone_repository = BashOperator( | ||
| task_id='clone_repository', | ||
| bash_command='git clone --branch main --single-branch --depth 1 ' | ||
| 'https://github.com/AlertaDengue/episcanner-downloader.git ' | ||
| '/opt/airflow/episcanner-downloader', | ||
| dag=dag, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using the DAG context manager, it is not necessary to set the dag variable to the tasks |
||
| ) | ||
|
|
||
| # Set variables for Episcanner-PostgreSQL connection | ||
| set_connection_variables = PythonOperator( | ||
| task_id='set_connection_variables', | ||
| python_callable=set_airflow_variables, | ||
| dag=dag, | ||
| ) | ||
|
|
||
| # Install the Episcanner package using Poetry | ||
| install_episcanner = BashOperator( | ||
| task_id='install_episcanner', | ||
| bash_command='source /home/airflow/mambaforge/bin/activate episcanner-downloader && ' # NOQA E501 | ||
| 'cd /opt/airflow/episcanner-downloader && ' | ||
| 'poetry install', | ||
| dag=dag, | ||
| ) | ||
|
|
||
| # Download all data to the specified directory | ||
| episcanner_downloader = BashOperator( | ||
| task_id='episcanner_downloader', | ||
| bash_command='source /home/airflow/mambaforge/bin/activate episcanner-downloader && ' # NOQA E501 | ||
| 'cd /opt/airflow/episcanner-downloader &&' | ||
| 'python epi_scanner/downloader/export_data.py ' | ||
| '-s all -d dengue chikungunya -o /opt/airflow/episcanner_data', | ||
| dag=dag, | ||
| ) | ||
|
|
||
| # Remove the episcanner-downloader repository | ||
| remove_repository = BashOperator( | ||
| task_id='remove_repository', | ||
| bash_command='rm -rf /opt/airflow/episcanner-downloader', | ||
| dag=dag, | ||
| ) | ||
|
|
||
| ( | ||
| clone_repository | ||
| >> set_connection_variables | ||
| >> install_episcanner | ||
| >> episcanner_downloader | ||
| >> remove_repository | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using the DAG context manager, it is not necessary to set the dag variable to the tasks