def test_should_not_delete_failed_notifications_before_seven_days(notify_db, notify_db_session): should_delete = datetime.utcnow() - timedelta(days=8) do_not_delete = datetime.utcnow() - timedelta(days=7) sample_notification(notify_db, notify_db_session, created_at=should_delete, status="failed", to_field="should_delete") sample_notification(notify_db, notify_db_session, created_at=do_not_delete, status="failed", to_field="do_not_delete") assert len(Notification.query.all()) == 2 delete_notifications_created_more_than_a_week_ago('failed') assert len(Notification.query.all()) == 1 assert Notification.query.first().to == 'do_not_delete'
def test_should_not_delete_notification_history(notify_db, notify_db_session): with freeze_time('2016-01-01 12:00'): notification = sample_notification(notify_db, notify_db_session, created_at=datetime.utcnow(), status="failed") notification_id = notification.id assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1 delete_notifications_created_more_than_a_week_ago('failed') assert Notification.query.count() == 0 assert NotificationHistory.query.count() == 1 assert NotificationHistory.query.one().id == notification_id
def delete_failed_notifications(): try: start = datetime.utcnow() deleted = delete_notifications_created_more_than_a_week_ago('failed') 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( "Delete job started {} finished {} deleted {} failed notifications".format( start, datetime.utcnow(), deleted ) ) except SQLAlchemyError as e: current_app.logger.exception("Failed to delete failed notifications") raise
def test_should_delete_notifications_after_seven_days(notify_db, notify_db_session): assert len(Notification.query.all()) == 0 # create one notification a day between 1st and 10th from 11:00 to 19:00 for i in range(1, 11): past_date = '2016-01-{0:02d} {0:02d}:00:00.000000'.format(i) with freeze_time(past_date): sample_notification(notify_db, notify_db_session, created_at=datetime.utcnow(), status="failed") all_notifications = Notification.query.all() assert len(all_notifications) == 10 # Records from before 3rd should be deleted delete_notifications_created_more_than_a_week_ago('failed') remaining_notifications = Notification.query.all() assert len(remaining_notifications) == 8 for notification in remaining_notifications: assert notification.created_at.date() >= date(2016, 1, 3)
def delete_successful_notifications(): try: start = datetime.utcnow() deleted = delete_notifications_created_more_than_a_week_ago('delivered') current_app.logger.info( "Delete job started {} finished {} deleted {} successful notifications".format( start, datetime.utcnow(), deleted ) ) except SQLAlchemyError as e: current_app.logger.exception("Failed to delete successful notifications") raise