Implemented the REST endpoint to communicate with the new send-to-provider code

- this allows us to send a notification to a provider by means of an API call
- This is in addition to the celery code.
- idea is that we can use this method to help speed up throughput by generating API traffic by node/lambda etc to supplement the celery code in times of high load.
This commit is contained in:
Martyn Inglis
2016-09-22 14:01:25 +01:00
parent 59ab5da5d3
commit 37a2dc7925
5 changed files with 139 additions and 22 deletions

View File

@@ -70,6 +70,7 @@ def create_app(app_name=None):
from app.provider_details.rest import provider_details as provider_details_blueprint
from app.spec.rest import spec as spec_blueprint
from app.organisation.rest import organisation_blueprint
from app.delivery.rest import delivery_blueprint
application.register_blueprint(service_blueprint, url_prefix='/service')
application.register_blueprint(user_blueprint, url_prefix='/user')
@@ -78,6 +79,7 @@ def create_app(app_name=None):
application.register_blueprint(notifications_blueprint)
application.register_blueprint(job_blueprint)
application.register_blueprint(invite_blueprint)
application.register_blueprint(delivery_blueprint)
application.register_blueprint(accept_invite, url_prefix='/invite')
application.register_blueprint(template_statistics_blueprint)

View File

@@ -4,25 +4,33 @@ from app.delivery import send_to_providers
from app.models import EMAIL_TYPE
from app.celery import provider_tasks
from app.dao import notifications_dao
from flask import current_app
delivery = Blueprint('delivery', __name__)
delivery_blueprint = Blueprint('delivery', __name__)
from app.errors import register_errors
register_errors(delivery)
register_errors(delivery_blueprint)
@delivery.route('/deliver/notification/<uuid:notification_id>', methods=['POST'])
@delivery_blueprint.route('/deliver/notification/<uuid:notification_id>', methods=['POST'])
def send_notification_to_provider(notification_id):
notification = notifications_dao.get_notification_by_id(notification_id)
if not notification:
return jsonify(notification={"result": "error", "message": "No result found"}), 404
return jsonify({"result": "error", "message": "No result found"}), 404
if notification.notification_type == EMAIL_TYPE:
send_response(send_to_providers.send_email_response, provider_tasks.deliver_email, notification_id, 'send-email')
send_response(
send_to_providers.send_email_to_provider,
provider_tasks.deliver_email,
notification,
'send-email')
else:
send_response(send_to_providers.send_sms_response, provider_tasks.deliver_sms, notification_id, 'send-sms')
send_response(
send_to_providers.send_sms_to_provider,
provider_tasks.deliver_sms,
notification,
'send-sms')
return jsonify({}), 204
@@ -30,4 +38,9 @@ def send_response(send_call, task_call, notification, queue):
try:
send_call(notification)
except Exception as e:
current_app.logger.exception(
"Failed to send notification, retrying in celery. ID {} type {}".format(
notification.id,
notification.notification_type),
e)
task_call.apply_async((str(notification.id)), queue=queue)