2017-02-13 15:05:39 +00:00
|
|
|
from datetime import (
|
|
|
|
|
datetime,
|
|
|
|
|
timedelta
|
|
|
|
|
)
|
2016-06-20 13:33:53 +01:00
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
|
|
2016-09-07 15:36:59 +01:00
|
|
|
from app.aws import s3
|
2016-06-20 13:33:53 +01:00
|
|
|
from app import notify_celery
|
2017-08-23 15:04:37 +01:00
|
|
|
from app.performance_platform import total_sent_notifications
|
2017-01-27 12:30:56 +00:00
|
|
|
from app import performance_platform_client
|
2017-08-10 16:37:30 +01:00
|
|
|
from app.dao.date_util import get_month_start_and_end_date_in_utc
|
2017-06-02 14:28:52 +01:00
|
|
|
from app.dao.inbound_sms_dao import delete_inbound_sms_created_more_than_a_week_ago
|
2016-06-20 13:33:53 +01:00
|
|
|
from app.dao.invited_user_dao import delete_invitations_created_more_than_two_days_ago
|
2017-07-24 15:13:18 +01:00
|
|
|
from app.dao.jobs_dao import (
|
2017-08-22 09:55:47 +01:00
|
|
|
dao_get_letter_jobs_by_status,
|
2017-07-24 15:13:18 +01:00
|
|
|
dao_set_scheduled_jobs_to_pending,
|
|
|
|
|
dao_get_jobs_older_than_limited_by
|
|
|
|
|
)
|
|
|
|
|
from app.dao.monthly_billing_dao import (
|
2017-08-10 16:37:30 +01:00
|
|
|
get_service_ids_that_need_billing_populated,
|
|
|
|
|
create_or_update_monthly_billing
|
2017-07-24 15:13:18 +01:00
|
|
|
)
|
2017-01-27 12:30:56 +00:00
|
|
|
from app.dao.notifications_dao import (
|
2017-02-13 15:05:39 +00:00
|
|
|
dao_timeout_notifications,
|
2017-05-23 13:40:36 +01:00
|
|
|
is_delivery_slow_for_provider,
|
2017-05-24 13:21:22 +01:00
|
|
|
delete_notifications_created_more_than_a_week_ago_by_type,
|
2017-05-22 15:07:16 +01:00
|
|
|
dao_get_scheduled_notifications,
|
|
|
|
|
set_scheduled_notification_to_processed)
|
2017-05-12 12:19:56 +01:00
|
|
|
from app.dao.statistics_dao import dao_timeout_job_statistics
|
2017-02-13 15:05:39 +00:00
|
|
|
from app.dao.provider_details_dao import (
|
|
|
|
|
get_current_provider,
|
|
|
|
|
dao_toggle_sms_provider
|
2017-01-27 12:30:56 +00:00
|
|
|
)
|
2016-06-20 13:33:53 +01:00
|
|
|
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
|
2017-08-22 09:55:47 +01:00
|
|
|
from app.models import LETTER_TYPE, JOB_STATUS_READY_TO_SEND
|
2017-05-16 13:47:22 +01:00
|
|
|
from app.notifications.process_notifications import send_notification_to_queue
|
2016-08-05 10:44:43 +01:00
|
|
|
from app.statsd_decorators import statsd
|
2016-08-24 17:03:56 +01:00
|
|
|
from app.celery.tasks import process_job
|
2017-08-22 15:49:56 +01:00
|
|
|
from app.config import QueueNames, TaskNames
|
2017-08-10 16:37:30 +01:00
|
|
|
from app.utils import convert_utc_to_bst
|
2016-08-24 17:03:56 +01:00
|
|
|
|
|
|
|
|
|
2016-09-07 15:36:59 +01:00
|
|
|
@notify_celery.task(name="remove_csv_files")
|
|
|
|
|
@statsd(namespace="tasks")
|
2017-06-06 16:02:01 +01:00
|
|
|
def remove_csv_files(job_types):
|
|
|
|
|
jobs = dao_get_jobs_older_than_limited_by(job_types=job_types)
|
2016-09-07 15:36:59 +01:00
|
|
|
for job in jobs:
|
|
|
|
|
s3.remove_job_from_s3(job.service_id, job.id)
|
|
|
|
|
current_app.logger.info("Job ID {} has been removed from s3.".format(job.id))
|
|
|
|
|
|
|
|
|
|
|
2016-08-24 17:03:56 +01:00
|
|
|
@notify_celery.task(name="run-scheduled-jobs")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def run_scheduled_jobs():
|
|
|
|
|
try:
|
2016-10-07 12:28:42 +01:00
|
|
|
for job in dao_set_scheduled_jobs_to_pending():
|
2017-05-25 10:51:49 +01:00
|
|
|
process_job.apply_async([str(job.id)], queue=QueueNames.JOBS)
|
2016-10-07 12:55:48 +01:00
|
|
|
current_app.logger.info("Job ID {} added to process job queue".format(job.id))
|
2017-05-22 16:30:45 +01:00
|
|
|
except SQLAlchemyError:
|
2016-10-17 17:44:17 +01:00
|
|
|
current_app.logger.exception("Failed to run scheduled jobs")
|
2016-08-24 17:03:56 +01:00
|
|
|
raise
|
2016-06-20 13:33:53 +01:00
|
|
|
|
|
|
|
|
|
2017-05-16 13:47:22 +01:00
|
|
|
@notify_celery.task(name='send-scheduled-notifications')
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def send_scheduled_notifications():
|
|
|
|
|
try:
|
2017-05-17 12:59:00 +01:00
|
|
|
scheduled_notifications = dao_get_scheduled_notifications()
|
|
|
|
|
for notification in scheduled_notifications:
|
2017-05-16 13:47:22 +01:00
|
|
|
send_notification_to_queue(notification, notification.service.research_mode)
|
2017-05-22 15:07:16 +01:00
|
|
|
set_scheduled_notification_to_processed(notification.id)
|
2017-05-17 12:59:00 +01:00
|
|
|
current_app.logger.info(
|
2017-05-22 16:30:45 +01:00
|
|
|
"Sent {} scheduled notifications to the provider queue".format(len(scheduled_notifications)))
|
|
|
|
|
except SQLAlchemyError:
|
2017-05-16 13:47:22 +01:00
|
|
|
current_app.logger.exception("Failed to send scheduled notifications")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2016-06-20 13:33:53 +01:00
|
|
|
@notify_celery.task(name="delete-verify-codes")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def delete_verify_codes():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_codes_older_created_more_than_a_day_ago()
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} verify codes".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
2016-10-17 17:44:17 +01:00
|
|
|
current_app.logger.exception("Failed to delete verify codes")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2017-05-23 13:40:36 +01:00
|
|
|
@notify_celery.task(name="delete-sms-notifications")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def delete_sms_notifications_older_than_seven_days():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago_by_type('sms')
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete {} job started {} finished {} deleted {} sms notifications".format(
|
|
|
|
|
'sms',
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to delete sms notifications")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-email-notifications")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2017-05-23 13:40:36 +01:00
|
|
|
def delete_email_notifications_older_than_seven_days():
|
2016-06-20 13:33:53 +01:00
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2017-05-23 13:40:36 +01:00
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago_by_type('email')
|
2016-06-20 13:33:53 +01:00
|
|
|
current_app.logger.info(
|
2017-05-23 13:40:36 +01:00
|
|
|
"Delete {} job started {} finished {} deleted {} email notifications".format(
|
|
|
|
|
'email',
|
2016-06-20 13:33:53 +01:00
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
2017-05-23 13:40:36 +01:00
|
|
|
current_app.logger.exception("Failed to delete sms notifications")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
2017-05-23 13:40:36 +01:00
|
|
|
@notify_celery.task(name="delete-letter-notifications")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2017-05-23 13:40:36 +01:00
|
|
|
def delete_letter_notifications_older_than_seven_days():
|
2016-06-20 13:33:53 +01:00
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
2017-05-23 13:40:36 +01:00
|
|
|
deleted = delete_notifications_created_more_than_a_week_ago_by_type('letter')
|
2016-06-20 13:33:53 +01:00
|
|
|
current_app.logger.info(
|
2017-05-23 13:40:36 +01:00
|
|
|
"Delete {} job started {} finished {} deleted {} letter notifications".format(
|
|
|
|
|
'letter',
|
2016-06-20 13:33:53 +01:00
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
2017-05-23 13:40:36 +01:00
|
|
|
current_app.logger.exception("Failed to delete sms notifications")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-invitations")
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def delete_invitations():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_invitations_created_more_than_two_days_ago()
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete job started {} finished {} deleted {} invitations".format(start, datetime.utcnow(), deleted)
|
|
|
|
|
)
|
2016-08-24 17:03:56 +01:00
|
|
|
except SQLAlchemyError as e:
|
2016-10-17 17:44:17 +01:00
|
|
|
current_app.logger.exception("Failed to delete invitations")
|
2016-06-20 13:33:53 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='timeout-sending-notifications')
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="tasks")
|
2016-06-20 13:33:53 +01:00
|
|
|
def timeout_notifications():
|
2016-09-14 10:38:34 +01:00
|
|
|
updated = dao_timeout_notifications(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD'))
|
|
|
|
|
if updated:
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Timeout period reached for {} notifications, status has been updated.".format(updated))
|
2017-01-27 12:30:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-daily-performance-platform-stats')
|
|
|
|
|
@statsd(namespace="tasks")
|
2017-01-30 18:24:18 +00:00
|
|
|
def send_daily_performance_platform_stats():
|
|
|
|
|
if performance_platform_client.active:
|
2017-08-23 15:04:37 +01:00
|
|
|
count_dict = total_sent_notifications.get_total_sent_notifications_yesterday()
|
2017-01-30 18:24:18 +00:00
|
|
|
email_sent_count = count_dict.get('email').get('count')
|
|
|
|
|
sms_sent_count = count_dict.get('sms').get('count')
|
|
|
|
|
start_date = count_dict.get('start_date')
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Attempting to update performance platform for date {} with email count {} and sms count {}"
|
|
|
|
|
.format(start_date, email_sent_count, sms_sent_count)
|
|
|
|
|
)
|
|
|
|
|
|
2017-08-23 15:04:37 +01:00
|
|
|
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
2017-01-30 18:24:18 +00:00
|
|
|
start_date,
|
|
|
|
|
'sms',
|
|
|
|
|
sms_sent_count,
|
|
|
|
|
'day'
|
|
|
|
|
)
|
|
|
|
|
|
2017-08-23 15:04:37 +01:00
|
|
|
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
2017-01-30 18:24:18 +00:00
|
|
|
start_date,
|
|
|
|
|
'email',
|
|
|
|
|
email_sent_count,
|
|
|
|
|
'day'
|
|
|
|
|
)
|
2017-02-13 15:05:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='switch-current-sms-provider-on-slow-delivery')
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def switch_current_sms_provider_on_slow_delivery():
|
|
|
|
|
"""
|
|
|
|
|
Switch providers if there are at least two slow delivery notifications (more than four minutes)
|
|
|
|
|
in the last ten minutes. Search from the time we last switched to the current provider.
|
|
|
|
|
"""
|
|
|
|
|
functional_test_provider_service_id = current_app.config.get('FUNCTIONAL_TEST_PROVIDER_SERVICE_ID')
|
|
|
|
|
functional_test_provider_template_id = current_app.config.get('FUNCTIONAL_TEST_PROVIDER_SMS_TEMPLATE_ID')
|
|
|
|
|
|
|
|
|
|
if functional_test_provider_service_id and functional_test_provider_template_id:
|
|
|
|
|
current_provider = get_current_provider('sms')
|
2017-02-24 13:41:32 +00:00
|
|
|
slow_delivery_notifications = is_delivery_slow_for_provider(
|
2017-02-13 15:05:39 +00:00
|
|
|
provider=current_provider.identifier,
|
|
|
|
|
threshold=2,
|
2017-02-24 13:41:32 +00:00
|
|
|
sent_at=max(datetime.utcnow() - timedelta(minutes=10), current_provider.updated_at),
|
2017-02-13 15:05:39 +00:00
|
|
|
delivery_time=timedelta(minutes=4),
|
|
|
|
|
service_id=functional_test_provider_service_id,
|
|
|
|
|
template_id=functional_test_provider_template_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if slow_delivery_notifications:
|
|
|
|
|
current_app.logger.warning(
|
2017-02-24 13:41:32 +00:00
|
|
|
'Slow delivery notifications detected for provider {}'.format(
|
|
|
|
|
current_provider.identifier
|
2017-02-13 15:05:39 +00:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
dao_toggle_sms_provider(current_provider.identifier)
|
2017-05-11 15:22:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='timeout-job-statistics')
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def timeout_job_statistics():
|
|
|
|
|
updated = dao_timeout_job_statistics(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD'))
|
|
|
|
|
if updated:
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Timeout period reached for {} job statistics, failure count has been updated.".format(updated))
|
2017-06-02 14:28:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete-inbound-sms")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def delete_inbound_sms_older_than_seven_days():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
deleted = delete_inbound_sms_created_more_than_a_week_ago()
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete inbound sms job started {} finished {} deleted {} inbound sms notifications".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
deleted
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to delete inbound sms notifications")
|
|
|
|
|
raise
|
2017-06-07 16:31:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="remove_transformed_dvla_files")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def remove_transformed_dvla_files():
|
|
|
|
|
jobs = dao_get_jobs_older_than_limited_by(job_types=[LETTER_TYPE])
|
|
|
|
|
for job in jobs:
|
|
|
|
|
s3.remove_transformed_dvla_file(job.id)
|
|
|
|
|
current_app.logger.info("Transformed dvla file for job {} has been removed from s3.".format(job.id))
|
2017-06-12 15:55:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="delete_dvla_response_files")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def delete_dvla_response_files_older_than_seven_days():
|
|
|
|
|
try:
|
|
|
|
|
start = datetime.utcnow()
|
|
|
|
|
bucket_objects = s3.get_s3_bucket_objects(
|
|
|
|
|
current_app.config['DVLA_RESPONSE_BUCKET_NAME'],
|
|
|
|
|
'root/dispatch'
|
|
|
|
|
)
|
|
|
|
|
older_than_seven_days = s3.filter_s3_bucket_objects_within_date_range(bucket_objects)
|
|
|
|
|
|
|
|
|
|
for f in older_than_seven_days:
|
|
|
|
|
s3.remove_s3_object(current_app.config['DVLA_RESPONSE_BUCKET_NAME'], f['Key'])
|
|
|
|
|
|
|
|
|
|
current_app.logger.info(
|
|
|
|
|
"Delete dvla response files started {} finished {} deleted {} files".format(
|
|
|
|
|
start,
|
|
|
|
|
datetime.utcnow(),
|
|
|
|
|
len(older_than_seven_days)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.exception("Failed to delete dvla response files")
|
|
|
|
|
raise
|
2017-07-24 15:13:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="populate_monthly_billing")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def populate_monthly_billing():
|
|
|
|
|
# for every service with billable units this month update billing totals for yesterday
|
|
|
|
|
# this will overwrite the existing amount.
|
|
|
|
|
yesterday = datetime.utcnow() - timedelta(days=1)
|
2017-08-10 16:37:30 +01:00
|
|
|
yesterday_in_bst = convert_utc_to_bst(yesterday)
|
|
|
|
|
start_date, end_date = get_month_start_and_end_date_in_utc(yesterday_in_bst)
|
|
|
|
|
services = get_service_ids_that_need_billing_populated(start_date=start_date, end_date=end_date)
|
|
|
|
|
[create_or_update_monthly_billing(service_id=s.service_id, billing_month=end_date) for s in services]
|
2017-08-22 09:55:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="run-letter-jobs")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def run_letter_jobs():
|
2017-08-22 14:23:35 +01:00
|
|
|
job_ids = [job.id for job in dao_get_letter_jobs_by_status(JOB_STATUS_READY_TO_SEND)]
|
2017-08-22 09:55:47 +01:00
|
|
|
notify_celery.send_task(
|
2017-08-22 15:49:56 +01:00
|
|
|
name=TaskNames.DVLA_FILES,
|
2017-08-22 14:23:35 +01:00
|
|
|
args=(job_ids),
|
2017-08-22 09:55:47 +01:00
|
|
|
queue=QueueNames.PROCESS_FTP
|
|
|
|
|
)
|
2017-08-22 14:23:35 +01:00
|
|
|
current_app.logger.info("Queued {} ready letter job ids onto {}".format(len(job_ids), QueueNames.PROCESS_FTP))
|