New task to use the new DAO method to delete the data by type

This commit is contained in:
Martyn Inglis
2017-05-23 13:40:36 +01:00
parent aaa0f763a1
commit a5c71ea907
2 changed files with 51 additions and 29 deletions

View File

@@ -12,10 +12,9 @@ from app import performance_platform_client
from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago
from app.dao.jobs_dao import dao_set_scheduled_jobs_to_pending, dao_get_jobs_older_than_limited_by from app.dao.jobs_dao import dao_set_scheduled_jobs_to_pending, dao_get_jobs_older_than_limited_by
from app.dao.notifications_dao import ( from app.dao.notifications_dao import (
delete_notifications_created_more_than_a_week_ago,
dao_timeout_notifications, dao_timeout_notifications,
is_delivery_slow_for_provider is_delivery_slow_for_provider,
) delete_notifications_created_more_than_a_week_ago_by_type)
from app.dao.statistics_dao import dao_timeout_job_statistics from app.dao.statistics_dao import dao_timeout_job_statistics
from app.dao.provider_details_dao import ( from app.dao.provider_details_dao import (
get_current_provider, get_current_provider,
@@ -61,42 +60,60 @@ def delete_verify_codes():
raise raise
@notify_celery.task(name="delete-successful-notifications") @notify_celery.task(name="delete-sms-notifications")
@statsd(namespace="tasks") @statsd(namespace="tasks")
def delete_successful_notifications(): def delete_sms_notifications_older_than_seven_days():
try: try:
start = datetime.utcnow() start = datetime.utcnow()
deleted = delete_notifications_created_more_than_a_week_ago('delivered') deleted = delete_notifications_created_more_than_a_week_ago_by_type('sms')
current_app.logger.info( current_app.logger.info(
"Delete job started {} finished {} deleted {} successful notifications".format( "Delete {} job started {} finished {} deleted {} sms notifications".format(
'sms',
start, start,
datetime.utcnow(), datetime.utcnow(),
deleted deleted
) )
) )
except SQLAlchemyError as e: except SQLAlchemyError as e:
current_app.logger.exception("Failed to delete successful notifications") current_app.logger.exception("Failed to delete sms notifications")
raise raise
@notify_celery.task(name="delete-failed-notifications") @notify_celery.task(name="delete-email-notifications")
@statsd(namespace="tasks") @statsd(namespace="tasks")
def delete_failed_notifications(): def delete_email_notifications_older_than_seven_days():
try: try:
start = datetime.utcnow() start = datetime.utcnow()
deleted = delete_notifications_created_more_than_a_week_ago('failed') deleted = delete_notifications_created_more_than_a_week_ago_by_type('email')
deleted += delete_notifications_created_more_than_a_week_ago('technical-failure')
deleted += delete_notifications_created_more_than_a_week_ago('temporary-failure')
deleted += delete_notifications_created_more_than_a_week_ago('permanent-failure')
current_app.logger.info( current_app.logger.info(
"Delete job started {} finished {} deleted {} failed notifications".format( "Delete {} job started {} finished {} deleted {} email notifications".format(
'email',
start, start,
datetime.utcnow(), datetime.utcnow(),
deleted deleted
) )
) )
except SQLAlchemyError as e: except SQLAlchemyError as e:
current_app.logger.exception("Failed to delete failed notifications") current_app.logger.exception("Failed to delete sms notifications")
raise
@notify_celery.task(name="delete-letter-notifications")
@statsd(namespace="tasks")
def delete_letter_notifications_older_than_seven_days():
try:
start = datetime.utcnow()
deleted = delete_notifications_created_more_than_a_week_ago_by_type('letter')
current_app.logger.info(
"Delete {} job started {} finished {} deleted {} letter notifications".format(
'letter',
start,
datetime.utcnow(),
deleted
)
)
except SQLAlchemyError as e:
current_app.logger.exception("Failed to delete sms notifications")
raise raise

View File

@@ -5,13 +5,13 @@ from functools import partial
from flask import current_app from flask import current_app
from freezegun import freeze_time from freezegun import freeze_time
from app.celery.scheduled_tasks import s3, timeout_job_statistics from app.celery.scheduled_tasks import s3, timeout_job_statistics, delete_sms_notifications_older_than_seven_days, \
delete_letter_notifications_older_than_seven_days, delete_email_notifications_older_than_seven_days
from app.celery import scheduled_tasks from app.celery import scheduled_tasks
from app.celery.scheduled_tasks import ( from app.celery.scheduled_tasks import (
delete_verify_codes, delete_verify_codes,
remove_csv_files, remove_csv_files,
delete_successful_notifications, delete_notifications_created_more_than_a_week_ago_by_type,
delete_failed_notifications,
delete_invitations, delete_invitations,
timeout_notifications, timeout_notifications,
run_scheduled_jobs, run_scheduled_jobs,
@@ -70,8 +70,7 @@ def prepare_current_provider(restore_provider_details):
def test_should_have_decorated_tasks_functions(): def test_should_have_decorated_tasks_functions():
assert delete_verify_codes.__wrapped__.__name__ == 'delete_verify_codes' assert delete_verify_codes.__wrapped__.__name__ == 'delete_verify_codes'
assert delete_successful_notifications.__wrapped__.__name__ == 'delete_successful_notifications' assert delete_notifications_created_more_than_a_week_ago_by_type.__wrapped__.__name__ == 'delete_notifications_created_more_than_a_week_ago_by_type' # noqa
assert delete_failed_notifications.__wrapped__.__name__ == 'delete_failed_notifications'
assert timeout_notifications.__wrapped__.__name__ == 'timeout_notifications' assert timeout_notifications.__wrapped__.__name__ == 'timeout_notifications'
assert delete_invitations.__wrapped__.__name__ == 'delete_invitations' assert delete_invitations.__wrapped__.__name__ == 'delete_invitations'
assert run_scheduled_jobs.__wrapped__.__name__ == 'run_scheduled_jobs' assert run_scheduled_jobs.__wrapped__.__name__ == 'run_scheduled_jobs'
@@ -81,16 +80,22 @@ def test_should_have_decorated_tasks_functions():
'switch_current_sms_provider_on_slow_delivery' 'switch_current_sms_provider_on_slow_delivery'
def test_should_call_delete_successful_notifications_more_than_week_in_task(notify_api, mocker): def test_should_call_delete_sms_notifications_more_than_week_in_task(notify_api, mocker):
mocked = mocker.patch('app.celery.scheduled_tasks.delete_notifications_created_more_than_a_week_ago') mocked = mocker.patch('app.celery.scheduled_tasks.delete_notifications_created_more_than_a_week_ago_by_type')
delete_successful_notifications() delete_sms_notifications_older_than_seven_days()
mocked.assert_called_once_with('delivered') mocked.assert_called_once_with('sms')
def test_should_call_delete_failed_notifications_more_than_week_in_task(notify_api, mocker): def test_should_call_delete_email_notifications_more_than_week_in_task(notify_api, mocker):
mocker.patch('app.celery.scheduled_tasks.delete_notifications_created_more_than_a_week_ago') mocked = mocker.patch('app.celery.scheduled_tasks.delete_notifications_created_more_than_a_week_ago_by_type')
delete_failed_notifications() delete_email_notifications_older_than_seven_days()
assert scheduled_tasks.delete_notifications_created_more_than_a_week_ago.call_count == 4 mocked.assert_called_once_with('email')
def test_should_call_delete_letter_notifications_more_than_week_in_task(notify_api, mocker):
mocked = mocker.patch('app.celery.scheduled_tasks.delete_notifications_created_more_than_a_week_ago_by_type')
delete_letter_notifications_older_than_seven_days()
mocked.assert_called_once_with('letter')
def test_should_call_delete_codes_on_delete_verify_codes_task(notify_api, mocker): def test_should_call_delete_codes_on_delete_verify_codes_task(notify_api, mocker):