Files
notifications-api/app/celery/broadcast_message_tasks.py
Leo Hemsted eca37d0853 add send_broadcast_message task
task takes a brodcast_message_id, and makes a post to the cbc-proxy
for now, hardcode the url to the notify stub. the stub requires template
as the admin/api get it, so use the marshmallow schema to json dump it.
Note - this also required us to tweak the BroadcastMessage.serialize
function so that it converts uuids in to ids - flask's jsonify function
does that for free but requests.post doesn't sadly.

if the request fails (either 4xx or 5xx) just raise an exception and let
it bubble up for now - in the future we'll add retry logic
2020-07-09 18:23:30 +01:00

36 lines
1.1 KiB
Python

import requests
from flask import current_app
from notifications_utils.statsd_decorators import statsd
from app import notify_celery
from app.schemas import template_schema
from app.dao.broadcast_message_dao import dao_get_broadcast_message_by_id
@notify_celery.task(name="send-broadcast-message")
@statsd(namespace="tasks")
def send_broadcast_message(broadcast_message_id, provider='stub-1'):
broadcast_message = dao_get_broadcast_message_by_id(broadcast_message_id)
current_app.logger.info(
f'sending broadcast_message {broadcast_message_id} '
f'status {broadcast_message.status} to {provider}'
)
payload = {
"template": template_schema.dump(broadcast_message.template).data,
"broadcast_message": broadcast_message.serialize(),
}
resp = requests.post(
f'{current_app.config["CBC_PROXY_URL"]}/broadcasts/{provider}',
json=payload
)
resp.raise_for_status()
current_app.logger.info(
f'broadcast_message {broadcast_message.id} '
f'status {broadcast_message.status} sent to {provider}'
)