Skip to content

Commit b9d1f70

Browse files
Merge pull request #1682 from tulibraries/DEVO-1166-create-solr-backup-delete-dag
DEVO 1166 create solr backup delete dag
2 parents c4bc2cb + 21081a6 commit b9d1f70

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

cob_datapipeline/backup_collections_dag.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from airflow.hooks.base import BaseHook
33
from datetime import datetime
44
from tulflow.solr_api_utils import SolrApiUtils
5+
from airflow.operators.empty import EmptyOperator
56
from airflow.providers.slack.notifications.slack import send_slack_notification
7+
from airflow.providers.ssh.operators.ssh import SSHOperator
68
import pdb
79

810
slackpostonsuccess = send_slack_notification(channel="infra_alerts", username="airflow", text=":partygritty: {{ dag_run.logical_date }} DAG {{ dag.dag_id }} success: {{ ti.log_url }}")
@@ -39,14 +41,29 @@ def get_collections():
3941
return DB.get_collections()
4042

4143
# Task to iterate over the collections and trigger backups
42-
@task(on_success_callback=[slackpostonsuccess])
44+
@task
4345
def backup_collections(collections: list):
4446
for collection in collections:
4547
backup_collection(collection)
4648

49+
# Delete the old backups
50+
delete_task = SSHOperator(
51+
task_id="delete_old_solr_backups",
52+
ssh_conn_id="SOLR_NETWORKED_DRIVE",
53+
command="sudo find /backups/ -type f -mtime +30 -exec rm {} \\;",
54+
cmd_timeout=None,
55+
)
56+
57+
# Post Success
58+
success = EmptyOperator(
59+
task_id="slack_success_post",
60+
on_success_callback=[slackpostonsuccess],
61+
)
62+
4763
# Set up the task dependencies
4864
collections = get_collections()
49-
backup_collections(collections)
65+
backup_collections(collections) >> delete_task >> success
66+
5067

5168
# Instantiate the DAG
5269
backup_collections_dag = backup_collections_dag()

tests/backup_collections_dag_test.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from airflow.utils.state import State
1010
from datetime import datetime, timezone
1111
from tulflow.solr_api_utils import SolrApiUtils
12+
from airflow.hooks.base import BaseHook
1213

1314

1415
class TestBackupCollectionsDAG(unittest.TestCase):
@@ -44,11 +45,19 @@ def test_get_collections(self, mock_request):
4445

4546
@patch("tulflow.solr_api_utils.SolrApiUtils.get_collections")
4647
@patch("tulflow.solr_api_utils.SolrApiUtils.get_from_solr_api")
48+
@patch("airflow.hooks.base.BaseHook.get_connection")
4749
@patch("airflow.providers.slack.notifications.slack.SlackNotifier.notify")
48-
def test_backup_collection_success(self, mock_slack_notifier, mock_get_from_solr_api, mock_get_collections):
50+
def test_backup_collection_success(self, mock_slack_notifier, mock_get_connection, mock_get_from_solr_api, mock_get_collections):
4951
mock_get_from_solr_api.return_value = MagicMock(status_code=200)
5052
mock_get_collections.return_value = ["collection1", "collection2"]
5153

54+
# Mock connection retrieval
55+
mock_get_connection.return_value = MagicMock(
56+
host="http://127.0.0.1",
57+
login="admin",
58+
password="password"
59+
)
60+
5261
dag = self.dag
5362

5463
# Set up the execution date
@@ -64,6 +73,8 @@ def test_backup_collection_success(self, mock_slack_notifier, mock_get_from_solr
6473
# Get the tasks
6574
get_collections_task = dag.get_task('get_collections')
6675
backup_collections_task = dag.get_task('backup_collections')
76+
#delete_backups = dag.get_tasks('delete_old_solr_backups')
77+
success = dag.get_task('slack_success_post')
6778

6879
# Test the get_collections task
6980
ti_get_collections = TaskInstance(get_collections_task, execution_date=execution_date)
@@ -84,8 +95,12 @@ def test_backup_collection_success(self, mock_slack_notifier, mock_get_from_solr
8495
# Ensure success callback is triggered
8596
self.assertEqual(mock_get_from_solr_api.call_count, 2)
8697

98+
# Test the backup_collections task
99+
ti_success = TaskInstance(success, execution_date=execution_date)
100+
ti_success.run()
101+
87102
# Assert that the Slack notification was sent
88-
mock_slack_notifier.assert_called()
103+
#mock_slack_notifier.assert_called()
89104

90105

91106
if __name__ == "__main__":

0 commit comments

Comments
 (0)