diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 82e1d040c..9d019fd84 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -12,7 +12,7 @@ from app.dao.notifications_dao import ( ) from app.dao.jobs_dao import dao_update_job, dao_get_job_by_id from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago -from app.dao.invited_user_dao import delete_invitations_older_created_more_than_a_day_ago +from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago from app.models import ( Notification, TEMPLATE_TYPE_EMAIL, @@ -77,7 +77,7 @@ def delete_failed_notifications(): def delete_invitations(): try: start = datetime.utcnow() - deleted = delete_invitations_older_created_more_than_a_day_ago() + deleted = delete_invitations_created_more_than_two_days_ago() current_app.logger.info( "Delete job started {} finished {} deleted {} invitations".format(start, datetime.utcnow(), deleted) ) diff --git a/app/dao/invited_user_dao.py b/app/dao/invited_user_dao.py index 9ede607be..62fb0d25d 100644 --- a/app/dao/invited_user_dao.py +++ b/app/dao/invited_user_dao.py @@ -21,9 +21,9 @@ def get_invited_users_for_service(service_id): return InvitedUser.query.filter_by(service_id=service_id).all() -def delete_invitations_older_created_more_than_a_day_ago(): +def delete_invitations_created_more_than_two_days_ago(): deleted = db.session.query(InvitedUser).filter( - InvitedUser.created_at <= datetime.utcnow() - timedelta(days=1) + InvitedUser.created_at <= datetime.utcnow() - timedelta(days=2) ).delete() db.session.commit() return deleted diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 14ac46412..378316b80 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -89,7 +89,7 @@ def get_notifications_for_service(service_id, page=1): def delete_successful_notifications_created_more_than_a_day_ago(): deleted = db.session.query(Notification).filter( - Notification.created_at < datetime.utcnow() - timedelta(hours=24), + Notification.created_at < datetime.utcnow() - timedelta(days=1), Notification.status == 'sent' ).delete() db.session.commit() @@ -98,7 +98,7 @@ def delete_successful_notifications_created_more_than_a_day_ago(): def delete_failed_notifications_created_more_than_a_week_ago(): deleted = db.session.query(Notification).filter( - Notification.created_at < datetime.utcnow() - timedelta(hours=24 * 7), + Notification.created_at < datetime.utcnow() - timedelta(days=7), Notification.status == 'failed' ).delete() db.session.commit() diff --git a/config.py b/config.py index 5c6fd72eb..c2b6ce984 100644 --- a/config.py +++ b/config.py @@ -40,22 +40,22 @@ class Config(object): CELERYBEAT_SCHEDULE = { 'delete-verify-codes': { 'task': 'delete-verify-codes', - 'schedule': timedelta(hours=1), + 'schedule': timedelta(minutes=63), 'options': {'queue': 'periodic'} }, 'delete-invitations': { 'task': 'delete-invitations', - 'schedule': timedelta(hours=1), + 'schedule': timedelta(minutes=66), 'options': {'queue': 'periodic'} }, 'delete-failed-notifications': { 'task': 'delete-failed-notifications', - 'schedule': timedelta(hours=1), + 'schedule': timedelta(minutes=60), 'options': {'queue': 'periodic'} }, 'delete-successful-notifications': { 'task': 'delete-successful-notifications', - 'schedule': timedelta(hours=1), + 'schedule': timedelta(minutes=31), 'options': {'queue': 'periodic'} } } diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index cccf2476b..54fa72998 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -51,9 +51,9 @@ def test_should_call_delete_codes_on_delete_verify_codes_task(notify_api, mocker def test_should_call_delete_invotations_on_delete_invitations_task(notify_api, mocker): - mocker.patch('app.celery.tasks.delete_invitations_older_created_more_than_a_day_ago') + mocker.patch('app.celery.tasks.delete_invitations_created_more_than_two_days_ago') delete_invitations() - assert tasks.delete_invitations_older_created_more_than_a_day_ago.call_count == 1 + assert tasks.delete_invitations_created_more_than_two_days_ago.call_count == 1 @freeze_time("2016-01-01 11:09:00.061258") diff --git a/tests/app/dao/test_invited_user_dao.py b/tests/app/dao/test_invited_user_dao.py index 06c568c61..0a3772ed7 100644 --- a/tests/app/dao/test_invited_user_dao.py +++ b/tests/app/dao/test_invited_user_dao.py @@ -9,7 +9,7 @@ from app.dao.invited_user_dao import ( get_invited_user, get_invited_users_for_service, get_invited_user_by_id, - delete_invitations_older_created_more_than_a_day_ago + delete_invitations_created_more_than_two_days_ago ) @@ -91,23 +91,23 @@ def test_save_invited_user_sets_status_to_cancelled(notify_db, notify_db_session def test_should_delete_all_invitations_more_than_one_day_old( sample_user, sample_service): - make_invitation(sample_user, sample_service, age=timedelta(hours=24)) - make_invitation(sample_user, sample_service, age=timedelta(hours=24)) + make_invitation(sample_user, sample_service, age=timedelta(hours=48)) + make_invitation(sample_user, sample_service, age=timedelta(hours=48)) assert len(InvitedUser.query.all()) == 2 - delete_invitations_older_created_more_than_a_day_ago() + delete_invitations_created_more_than_two_days_ago() assert len(InvitedUser.query.all()) == 0 -def test_should_not_delete_invitations_less_than_one_day_old( +def test_should_not_delete_invitations_less_than_two_days_old( sample_user, sample_service): - make_invitation(sample_user, sample_service, age=timedelta(hours=23, minutes=59, seconds=59), + make_invitation(sample_user, sample_service, age=timedelta(hours=47, minutes=59, seconds=59), email_address="valid@2.com") - make_invitation(sample_user, sample_service, age=timedelta(hours=24), + make_invitation(sample_user, sample_service, age=timedelta(hours=48), email_address="expired@1.com") assert len(InvitedUser.query.all()) == 2 - delete_invitations_older_created_more_than_a_day_ago() + delete_invitations_created_more_than_two_days_ago() assert len(InvitedUser.query.all()) == 1 assert InvitedUser.query.first().email_address == "valid@2.com" diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 510068494..d7bbbae09 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -1,3 +1,4 @@ +from datetime import datetime import uuid import app.celery.tasks from tests import create_authorization_header @@ -126,11 +127,16 @@ def test_get_all_notifications_for_job_in_order(notify_api, notify_db, notify_db with notify_api.test_client() as client: main_job = sample_job(notify_db, notify_db_session, service=sample_service) another_job = sample_job(notify_db, notify_db_session, service=sample_service) - from time import sleep - notification_1 = sample_notification(notify_db, notify_db_session, job=main_job, to_field="1") - notification_2 = sample_notification(notify_db, notify_db_session, job=main_job, to_field="2") - notification_3 = sample_notification(notify_db, notify_db_session, job=main_job, to_field="3") + notification_1 = sample_notification( + notify_db, notify_db_session, job=main_job, to_field="1", created_at=datetime.utcnow() + ) + notification_2 = sample_notification( + notify_db, notify_db_session, job=main_job, to_field="2", created_at=datetime.utcnow() + ) + notification_3 = sample_notification( + notify_db, notify_db_session, job=main_job, to_field="3", created_at=datetime.utcnow() + ) sample_notification(notify_db, notify_db_session, job=another_job) auth_header = create_authorization_header(