Scheduled tasks to clean up the database

- tasks run hourly
- uses celery beat to schedule the tasks

4 new tasks
- delete verify codes (after 1 day)
- delete invitations (after 1 day)
- delete successful notifications  (after 1 day)
- delete failed notifications (after 7 days)

Delete methods in the DAO classes
This commit is contained in:
Martyn Inglis
2016-03-09 17:46:01 +00:00
parent fbfa176895
commit c8a5366484
11 changed files with 298 additions and 37 deletions

View File

@@ -10,9 +10,12 @@ from app.dao.notifications_dao import (
get_notification,
get_notification_for_job,
get_notifications_for_job,
dao_get_notification_statistics_for_service
dao_get_notification_statistics_for_service,
delete_successful_notifications_created_more_than_a_day_ago,
delete_failed_notifications_created_more_than_a_week_ago
)
from tests.app.conftest import sample_job
from tests.app.conftest import sample_notification
def test_should_be_able_to_get_statistics_for_a_service(sample_template):
@@ -489,3 +492,44 @@ def test_update_notification(sample_notification, sample_template):
dao_update_notification(sample_notification)
notification_from_db = Notification.query.get(sample_notification.id)
assert notification_from_db.status == 'failed'
def test_should_delete_sent_notifications_after_one_day(notify_db, notify_db_session):
created_at = datetime.utcnow() - timedelta(hours=24)
sample_notification(notify_db, notify_db_session, created_at=created_at)
sample_notification(notify_db, notify_db_session, created_at=created_at)
assert len(Notification.query.all()) == 2
delete_successful_notifications_created_more_than_a_day_ago()
assert len(Notification.query.all()) == 0
def test_should_delete_failed_notifications_after_seven_days(notify_db, notify_db_session):
created_at = datetime.utcnow() - timedelta(hours=24 * 7)
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
assert len(Notification.query.all()) == 2
delete_failed_notifications_created_more_than_a_week_ago()
assert len(Notification.query.all()) == 0
def test_should_not_delete_sent_notifications_before_one_day(notify_db, notify_db_session):
expired = datetime.utcnow() - timedelta(hours=24)
valid = datetime.utcnow() - timedelta(hours=23, minutes=59, seconds=59)
sample_notification(notify_db, notify_db_session, created_at=expired, to_field="expired")
sample_notification(notify_db, notify_db_session, created_at=valid, to_field="valid")
assert len(Notification.query.all()) == 2
delete_successful_notifications_created_more_than_a_day_ago()
assert len(Notification.query.all()) == 1
assert Notification.query.first().to == 'valid'
def test_should_not_delete_failed_notifications_before_seven_days(notify_db, notify_db_session):
expired = datetime.utcnow() - timedelta(hours=24 * 7)
valid = datetime.utcnow() - timedelta(hours=(24 * 6) + 23, minutes=59, seconds=59)
sample_notification(notify_db, notify_db_session, created_at=expired, status="failed", to_field="expired")
sample_notification(notify_db, notify_db_session, created_at=valid, status="failed", to_field="valid")
assert len(Notification.query.all()) == 2
delete_failed_notifications_created_more_than_a_week_ago()
assert len(Notification.query.all()) == 1
assert Notification.query.first().to == 'valid'