-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
import os | ||
|
||
import pendulum | ||
from airflow.decorators import dag | ||
from airflow.operators.bash import BashOperator | ||
from airflow.operators.bash_operator import BashOperator | ||
|
||
AIRFLOW_HOME = os.getenv("AIRFLOW_HOME") | ||
LOG_DIR = "logs" | ||
logs_dir = f"{AIRFLOW_HOME}/{LOG_DIR}" | ||
|
||
|
||
@dag(start_date=pendulum.today("UTC").add(days=-1), schedule="@monthly", catchup=False) | ||
def cleanup_logs(): | ||
BashOperator( | ||
task_id="cleanup_logs", | ||
bash_command=f""" | ||
logs_dir="{logs_dir}" | ||
find "$logs_dir" -type d -mtime +30 -exec rm -r {{}} \; 2>/dev/null | ||
""", | ||
) | ||
|
||
|
||
cleanup_logs() |
This file contains 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 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 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 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,39 @@ | ||
import os | ||
from datetime import datetime, timezone | ||
|
||
from airflow import DAG | ||
from airflow.models import DagBag | ||
from freezegun import freeze_time | ||
from pytest import fixture | ||
|
||
|
||
@fixture | ||
def dag(): | ||
dagbag = DagBag(dag_folder="dags/", include_examples=False) | ||
assert dagbag.import_errors.get(f"dags/cleanup_logs.py") is None | ||
clean_dag = dagbag.get_dag(dag_id="cleanup_logs") | ||
return clean_dag | ||
|
||
|
||
def test_dag_loaded(dag: DAG): | ||
assert dag is not None | ||
assert len(dag.tasks) == 1 | ||
|
||
|
||
@freeze_time("2023-09-20") | ||
@fixture | ||
def old_temp_dir(tmpdir, tmp_path): | ||
logs_date = datetime.utcnow().astimezone(timezone.utc) | ||
log_path = tmpdir.join( | ||
f'logs/dag_id=test/run_id=test__{logs_date.strftime("%Y-%m-%dT%H:%M:%S.%f%z")}/task_id=test_task/attempt=1.log' | ||
) | ||
log_path.dirpath().ensure_dir() | ||
yield log_path | ||
|
||
|
||
@freeze_time("2023-09-20") | ||
def test_clean_up_command(dag, old_temp_dir, monkeypatch): | ||
monkeypatch.setenv("AIRFLOW_HOME", old_temp_dir) | ||
dag.clear() | ||
dag.test() | ||
assert not os.path.exists(old_temp_dir) |