mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
Merge pull request #1780 from alphagov/send-service-callback-if-sent_at_is-None
Send service callback if sent at is none
This commit is contained in:
@@ -9,6 +9,8 @@ from app.config import QueueNames
|
||||
from app.dao import notifications_dao
|
||||
from app.dao.notifications_dao import update_notification_status_by_id
|
||||
from app.delivery import send_to_providers
|
||||
from app.exceptions import NotificationTechnicalFailureException
|
||||
from app.models import NOTIFICATION_TECHNICAL_FAILURE
|
||||
|
||||
|
||||
@worker_process_shutdown.connect
|
||||
@@ -31,10 +33,10 @@ def deliver_sms(self, notification_id):
|
||||
)
|
||||
self.retry(queue=QueueNames.RETRY)
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.exception(
|
||||
"RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification_id),
|
||||
)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
message = "RETRY FAILED: Max retries reached. The task send_sms_to_provider failed for notification {}. " \
|
||||
"Notification has been updated to technical-failure".format(notification_id)
|
||||
update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE)
|
||||
raise NotificationTechnicalFailureException(message)
|
||||
|
||||
|
||||
@notify_celery.task(bind=True, name="deliver_email", max_retries=48, default_retry_delay=300)
|
||||
@@ -55,7 +57,8 @@ def deliver_email(self, notification_id):
|
||||
)
|
||||
self.retry(queue=QueueNames.RETRY)
|
||||
except self.MaxRetriesExceededError:
|
||||
current_app.logger.error(
|
||||
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification_id)
|
||||
)
|
||||
update_notification_status_by_id(notification_id, 'technical-failure')
|
||||
message = "RETRY FAILED: Max retries reached. " \
|
||||
"The task send_email_to_provider failed for notification {}. " \
|
||||
"Notification has been updated to technical-failure".format(notification_id)
|
||||
update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE)
|
||||
raise NotificationTechnicalFailureException(message)
|
||||
|
||||
@@ -16,6 +16,7 @@ from app.dao.services_dao import (
|
||||
dao_fetch_monthly_historical_stats_by_template
|
||||
)
|
||||
from app.dao.stats_template_usage_by_month_dao import insert_or_update_stats_for_template
|
||||
from app.exceptions import NotificationTechnicalFailureException
|
||||
from app.performance_platform import total_sent_notifications, processing_time
|
||||
from app import performance_platform_client, deskpro_client
|
||||
from app.dao.date_util import get_month_start_and_end_date_in_utc
|
||||
@@ -59,10 +60,15 @@ from app.celery.tasks import (
|
||||
process_job
|
||||
)
|
||||
from app.config import QueueNames, TaskNames
|
||||
from app.utils import convert_utc_to_bst
|
||||
from app.utils import (
|
||||
convert_utc_to_bst
|
||||
)
|
||||
from app.v2.errors import JobIncompleteError
|
||||
from app.dao.service_callback_api_dao import get_service_callback_api_for_service
|
||||
from app.celery.service_callback_tasks import send_delivery_status_to_service
|
||||
from app.celery.service_callback_tasks import (
|
||||
send_delivery_status_to_service,
|
||||
create_encrypted_callback_data,
|
||||
)
|
||||
import pytz
|
||||
|
||||
|
||||
@@ -196,17 +202,25 @@ def delete_invitations():
|
||||
@notify_celery.task(name='timeout-sending-notifications')
|
||||
@statsd(namespace="tasks")
|
||||
def timeout_notifications():
|
||||
notifications = dao_timeout_notifications(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD'))
|
||||
technical_failure_notifications, temporary_failure_notifications = \
|
||||
dao_timeout_notifications(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD'))
|
||||
|
||||
notifications = technical_failure_notifications + temporary_failure_notifications
|
||||
for notification in notifications:
|
||||
# queue callback task only if the service_callback_api exists
|
||||
service_callback_api = get_service_callback_api_for_service(service_id=notification.service_id)
|
||||
|
||||
if service_callback_api:
|
||||
send_delivery_status_to_service.apply_async([str(notification.id)], queue=QueueNames.CALLBACKS)
|
||||
encrypted_notification = create_encrypted_callback_data(notification, service_callback_api)
|
||||
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
|
||||
queue=QueueNames.CALLBACKS)
|
||||
|
||||
current_app.logger.info(
|
||||
"Timeout period reached for {} notifications, status has been updated.".format(len(notifications)))
|
||||
if technical_failure_notifications:
|
||||
message = "{} notifications have been updated to technical-failure because they " \
|
||||
"have timed out and are still in created.Notification ids: {}".format(
|
||||
len(technical_failure_notifications), [str(x.id) for x in technical_failure_notifications])
|
||||
raise NotificationTechnicalFailureException(message)
|
||||
|
||||
|
||||
@notify_celery.task(name='send-daily-performance-platform-stats')
|
||||
|
||||
@@ -62,7 +62,7 @@ def send_delivery_status_to_service(self, notification_id,
|
||||
response.raise_for_status()
|
||||
except RequestException as e:
|
||||
current_app.logger.warning(
|
||||
"send_delivery_status_to_service request failed for service_id: {} and url: {}. exc: {}".format(
|
||||
"send_delivery_status_to_service request failed for notification_id: {} and url: {}. exc: {}".format(
|
||||
notification_id,
|
||||
status_update['service_callback_api_url'],
|
||||
e
|
||||
@@ -119,7 +119,7 @@ def process_update_with_notification_id(self, notification_id):
|
||||
response.raise_for_status()
|
||||
except RequestException as e:
|
||||
current_app.logger.warning(
|
||||
"send_delivery_status_to_service request failed for service_id: {} and url: {}. exc: {}".format(
|
||||
"send_delivery_status_to_service request failed for notification_id: {} and url: {}. exc: {}".format(
|
||||
notification_id,
|
||||
service_callback_api.url,
|
||||
e
|
||||
@@ -141,3 +141,21 @@ def process_update_with_notification_id(self, notification_id):
|
||||
"""Retry: send_delivery_status_to_service has retried the max num of times
|
||||
for notification: {}""".format(notification_id)
|
||||
)
|
||||
|
||||
|
||||
def create_encrypted_callback_data(notification, service_callback_api):
|
||||
from app import DATETIME_FORMAT, encryption
|
||||
data = {
|
||||
"notification_id": str(notification.id),
|
||||
"notification_client_reference": notification.client_reference,
|
||||
"notification_to": notification.to,
|
||||
"notification_status": notification.status,
|
||||
"notification_created_at": notification.created_at.strftime(DATETIME_FORMAT),
|
||||
"notification_updated_at":
|
||||
notification.updated_at.strftime(DATETIME_FORMAT) if notification.updated_at else None,
|
||||
"notification_sent_at": notification.sent_at.strftime(DATETIME_FORMAT) if notification.sent_at else None,
|
||||
"notification_type": notification.notification_type,
|
||||
"service_callback_api_url": service_callback_api.url,
|
||||
"service_callback_api_bearer_token": service_callback_api.bearer_token,
|
||||
}
|
||||
return encryption.encrypt(data)
|
||||
|
||||
@@ -50,7 +50,7 @@ from app.dao.provider_details_dao import get_current_provider
|
||||
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
|
||||
from app.dao.services_dao import dao_fetch_service_by_id, fetch_todays_total_message_count
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.exceptions import DVLAException
|
||||
from app.exceptions import DVLAException, NotificationTechnicalFailureException
|
||||
from app.models import (
|
||||
DVLA_RESPONSE_STATUS_SENT,
|
||||
EMAIL_TYPE,
|
||||
@@ -371,8 +371,10 @@ def update_letter_notifications_to_error(self, notification_references):
|
||||
'updated_at': datetime.utcnow()
|
||||
}
|
||||
)
|
||||
|
||||
current_app.logger.debug("Updated {} letter notifications to technical-failure".format(updated_count))
|
||||
message = "Updated {} letter notifications to technical-failure with references {}".format(
|
||||
updated_count, notification_references
|
||||
)
|
||||
raise NotificationTechnicalFailureException(message=message)
|
||||
|
||||
|
||||
def handle_exception(task, notification, notification_id, exc):
|
||||
|
||||
@@ -375,14 +375,15 @@ def dao_timeout_notifications(timeout_period_in_seconds):
|
||||
timeout = functools.partial(_timeout_notifications, timeout_start=timeout_start, updated_at=updated_at)
|
||||
|
||||
# Notifications still in created status are marked with a technical-failure:
|
||||
updated_ids = timeout([NOTIFICATION_CREATED], NOTIFICATION_TECHNICAL_FAILURE)
|
||||
technical_failure_notifications = timeout([NOTIFICATION_CREATED], NOTIFICATION_TECHNICAL_FAILURE)
|
||||
|
||||
# Notifications still in sending or pending status are marked with a temporary-failure:
|
||||
updated_ids += timeout([NOTIFICATION_SENDING, NOTIFICATION_PENDING], NOTIFICATION_TEMPORARY_FAILURE)
|
||||
temporary_failure_notifications = timeout([NOTIFICATION_SENDING, NOTIFICATION_PENDING],
|
||||
NOTIFICATION_TEMPORARY_FAILURE)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return updated_ids
|
||||
return technical_failure_notifications, temporary_failure_notifications
|
||||
|
||||
|
||||
def get_total_sent_notifications_in_date_range(start_date, end_date, notification_type):
|
||||
|
||||
@@ -19,6 +19,7 @@ from app.dao.provider_details_dao import (
|
||||
)
|
||||
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.exceptions import NotificationTechnicalFailureException
|
||||
from app.models import (
|
||||
SMS_TYPE,
|
||||
KEY_TYPE_TEST,
|
||||
@@ -211,7 +212,7 @@ def get_html_email_options(service):
|
||||
def technical_failure(notification):
|
||||
notification.status = NOTIFICATION_TECHNICAL_FAILURE
|
||||
dao_update_notification(notification)
|
||||
current_app.logger.warn(
|
||||
raise NotificationTechnicalFailureException(
|
||||
"Send {} for notification id {} to provider is not allowed: service {} is inactive".format(
|
||||
notification.notification_type,
|
||||
notification.id,
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
class DVLAException(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
|
||||
class NotificationTechnicalFailureException(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
@@ -12,7 +12,10 @@ from app.dao import (
|
||||
)
|
||||
from app.dao.service_callback_api_dao import get_service_callback_api_for_service
|
||||
from app.notifications.process_client_response import validate_callback_data
|
||||
from app.celery.service_callback_tasks import send_delivery_status_to_service
|
||||
from app.celery.service_callback_tasks import (
|
||||
send_delivery_status_to_service,
|
||||
create_encrypted_callback_data,
|
||||
)
|
||||
from app.config import QueueNames
|
||||
|
||||
|
||||
@@ -76,7 +79,7 @@ def process_ses_response(ses_request):
|
||||
notification.sent_at
|
||||
)
|
||||
|
||||
_check_and_queue_callback_task(notification.id, notification.service_id)
|
||||
_check_and_queue_callback_task(notification)
|
||||
return
|
||||
|
||||
except KeyError:
|
||||
@@ -93,8 +96,10 @@ def remove_emails_from_bounce(bounce_dict):
|
||||
recip.pop('emailAddress')
|
||||
|
||||
|
||||
def _check_and_queue_callback_task(notification_id, service_id):
|
||||
def _check_and_queue_callback_task(notification):
|
||||
# queue callback task only if the service_callback_api exists
|
||||
service_callback_api = get_service_callback_api_for_service(service_id=service_id)
|
||||
service_callback_api = get_service_callback_api_for_service(service_id=notification.service_id)
|
||||
if service_callback_api:
|
||||
send_delivery_status_to_service.apply_async([str(notification_id)], queue=QueueNames.CALLBACKS)
|
||||
encrypted_notification = create_encrypted_callback_data(notification, service_callback_api)
|
||||
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
|
||||
queue=QueueNames.CALLBACKS)
|
||||
|
||||
@@ -8,11 +8,13 @@ from app.clients import ClientException
|
||||
from app.dao import notifications_dao
|
||||
from app.clients.sms.firetext import get_firetext_responses
|
||||
from app.clients.sms.mmg import get_mmg_responses
|
||||
from app.celery.service_callback_tasks import send_delivery_status_to_service
|
||||
from app.celery.service_callback_tasks import (
|
||||
send_delivery_status_to_service,
|
||||
create_encrypted_callback_data,
|
||||
)
|
||||
from app.config import QueueNames
|
||||
from app.dao.service_callback_api_dao import get_service_callback_api_for_service
|
||||
|
||||
|
||||
sms_response_mapper = {
|
||||
'MMG': get_mmg_responses,
|
||||
'Firetext': get_firetext_responses
|
||||
@@ -83,7 +85,9 @@ def _process_for_status(notification_status, client_name, reference):
|
||||
service_callback_api = get_service_callback_api_for_service(service_id=notification.service_id)
|
||||
|
||||
if service_callback_api:
|
||||
send_delivery_status_to_service.apply_async([str(notification.id)], queue=QueueNames.CALLBACKS)
|
||||
encrypted_notification = create_encrypted_callback_data(notification, service_callback_api)
|
||||
send_delivery_status_to_service.apply_async([str(notification.id), encrypted_notification],
|
||||
queue=QueueNames.CALLBACKS)
|
||||
|
||||
success = "{} callback succeeded. reference {} updated".format(client_name, reference)
|
||||
return success
|
||||
|
||||
Reference in New Issue
Block a user