2017-05-19 10:16:34 +01:00
|
|
|
import json
|
|
|
|
|
|
2017-05-15 11:12:31 +01:00
|
|
|
from functools import wraps
|
2017-04-25 14:56:16 +01:00
|
|
|
|
|
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
|
|
|
|
jsonify,
|
|
|
|
|
request,
|
2017-05-15 11:12:31 +01:00
|
|
|
current_app
|
2017-04-25 14:56:16 +01:00
|
|
|
)
|
|
|
|
|
|
2019-03-25 15:30:48 +00:00
|
|
|
from app.celery.tasks import update_letter_notifications_statuses, record_daily_sorted_counts
|
2017-05-15 11:12:31 +01:00
|
|
|
from app.v2.errors import register_errors
|
|
|
|
|
from app.notifications.utils import autoconfirm_subscription
|
|
|
|
|
from app.schema_validation import validate
|
2017-07-19 13:50:29 +01:00
|
|
|
from app.config import QueueNames
|
2017-04-25 14:56:16 +01:00
|
|
|
|
|
|
|
|
letter_callback_blueprint = Blueprint('notifications_letter_callback', __name__)
|
2017-05-15 11:12:31 +01:00
|
|
|
register_errors(letter_callback_blueprint)
|
2017-05-09 14:20:56 +01:00
|
|
|
|
|
|
|
|
|
2017-05-15 11:12:31 +01:00
|
|
|
dvla_sns_callback_schema = {
|
|
|
|
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
|
|
|
|
"description": "sns callback received on s3 update",
|
|
|
|
|
"type": "object",
|
|
|
|
|
"title": "dvla internal sns callback",
|
|
|
|
|
"properties": {
|
|
|
|
|
"Type": {"enum": ["Notification", "SubscriptionConfirmation"]},
|
|
|
|
|
"MessageId": {"type": "string"},
|
|
|
|
|
"Message": {"type": ["string", "object"]}
|
|
|
|
|
},
|
|
|
|
|
"required": ["Type", "MessageId", "Message"]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_schema(schema):
|
|
|
|
|
def decorator(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
|
def wrapper(*args, **kw):
|
2017-05-16 10:29:27 +01:00
|
|
|
validate(request.get_json(force=True), schema)
|
2017-05-15 11:12:31 +01:00
|
|
|
return f(*args, **kw)
|
|
|
|
|
return wrapper
|
|
|
|
|
return decorator
|
2017-04-25 14:56:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@letter_callback_blueprint.route('/notifications/letter/dvla', methods=['POST'])
|
2017-05-15 11:12:31 +01:00
|
|
|
@validate_schema(dvla_sns_callback_schema)
|
2017-04-25 14:56:16 +01:00
|
|
|
def process_letter_response():
|
2017-05-16 10:29:27 +01:00
|
|
|
req_json = request.get_json(force=True)
|
As Notify matures we probably need less logging, especially to report happy path events.
This PR is a proposal to reduce the average messages we see for a single notification from about 7 messages to 2.
Messaging would change to something like this:
February 2nd 2018, 15:39:05.885 Full delivery response from Firetext for notification: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
{'status': ['0'], 'reference': ['8eda51d5-cd82-4569-bfc9-d5570cdf2126'], 'time': ['2018-02-02 15:39:01'], 'code': ['000']}
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:57.727 SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to provider firetext at 2018-02-02 15:38:56.716814
February 2nd 2018, 15:38:56.727 Starting sending SMS 8eda51d5-cd82-4569-bfc9-d5570cdf2126 to provider at 2018-02-02 15:38:56.408181
February 2nd 2018, 15:38:56.727 Firetext request for 8eda51d5-cd82-4569-bfc9-d5570cdf2126 finished in 0.30376038211397827
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 sent to the priority-tasks queue for delivery
To somthing like this:
February 2nd 2018, 15:39:05.885 Firetext callback return status of 0 for reference: 8eda51d5-cd82-4569-bfc9-d5570cdf2126
February 2nd 2018, 15:38:49.449 sms 8eda51d5-cd82-4569-bfc9-d5570cdf2126 created at 2018-02-02 15:38:48.439113
2018-02-02 15:55:25 +00:00
|
|
|
current_app.logger.debug('Received SNS callback: {}'.format(req_json))
|
2017-05-15 11:12:31 +01:00
|
|
|
if not autoconfirm_subscription(req_json):
|
|
|
|
|
# The callback should have one record for an S3 Put Event.
|
2017-05-19 10:16:34 +01:00
|
|
|
message = json.loads(req_json['Message'])
|
|
|
|
|
filename = message['Records'][0]['s3']['object']['key']
|
2017-05-15 11:12:31 +01:00
|
|
|
current_app.logger.info('Received file from DVLA: {}'.format(filename))
|
2018-01-12 16:10:09 +00:00
|
|
|
|
2018-01-15 14:09:46 +00:00
|
|
|
if filename.lower().endswith('rs.txt') or filename.lower().endswith('rsp.txt'):
|
|
|
|
|
current_app.logger.info('DVLA callback: Calling task to update letter notifications')
|
2018-01-12 15:10:42 +00:00
|
|
|
update_letter_notifications_statuses.apply_async([filename], queue=QueueNames.NOTIFY)
|
2019-03-25 15:30:48 +00:00
|
|
|
record_daily_sorted_counts.apply_async([filename], queue=QueueNames.NOTIFY)
|
2017-05-15 11:12:31 +01:00
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
|
result="success", message="DVLA callback succeeded"
|
|
|
|
|
), 200
|