Renamed some files and fixed a bug on deleting the failed notifications

This commit is contained in:
Martyn Inglis
2016-03-10 09:34:27 +00:00
parent c8a5366484
commit 976a4c06e3
7 changed files with 30 additions and 24 deletions

View File

@@ -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)
)

View File

@@ -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

View File

@@ -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()

View File

@@ -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'}
}
}

View File

@@ -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")

View File

@@ -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"

View File

@@ -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(