Files
notifications-api/app/celery/broadcast_message_tasks.py
Leo Hemsted bdf2253298 send broadcast events rather than messages
use the new endpoint from cbc proxy. create a new task that just
serializes the event and sends it across rather than sending a template
and the broadcast message.

some changes to serialize to make it json friendly etc. it also expects
sent_at and transmitted_finishes_at to always be set (we set them in the
code but don't enforce it n the DB right now), as they're required by
utils template. not sure whether we'll update db constraints to be more
strict or utils template to be more permissive just yet, wait until we
find out more about the requirements of the CBCs we integrate with.
2020-08-14 17:41:44 +01:00

62 lines
2.0 KiB
Python

import requests
from flask import current_app
from notifications_utils.statsd_decorators import statsd
from app import notify_celery
from app.dao.broadcast_message_dao import dao_get_broadcast_message_by_id, dao_get_broadcast_event_by_id
@notify_celery.task(name="send-broadcast-message")
@statsd(namespace="tasks")
def send_broadcast_message(broadcast_message_id, provider='stub-1'):
# imports of schemas from tasks have to happen within functions to prevent
# `AttributeError: 'DummySession' object has no attribute 'query'` errors in unrelated tests
from app.schemas import template_schema
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}'
)
@notify_celery.task(name="send-broadcast-event")
@statsd(namespace="tasks")
def send_broadcast_event(broadcast_event_id, provider='stub-1'):
broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)
current_app.logger.info(
f'sending broadcast_event {broadcast_event.reference} '
f'msgType {broadcast_event.message_type} to {provider}'
)
payload = broadcast_event.serialize()
resp = requests.post(
f'{current_app.config["CBC_PROXY_URL"]}/broadcasts/events/{provider}',
json=payload
)
resp.raise_for_status()
current_app.logger.info(
f'broadcast_event {broadcast_event.reference} '
f'msgType {broadcast_event.message_type} sent to {provider}'
)