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.
This commit is contained in:
Leo Hemsted
2020-08-04 19:21:22 +01:00
parent 1c48e2efb2
commit bdf2253298
7 changed files with 138 additions and 44 deletions

View File

@@ -14,7 +14,7 @@ from app.dao.broadcast_message_dao import (
from app.dao.services_dao import dao_fetch_service_by_id
from app.errors import register_errors, InvalidRequest
from app.models import BroadcastMessage, BroadcastStatusType, BroadcastEvent, BroadcastEventMessageType
from app.celery.broadcast_message_tasks import send_broadcast_message
from app.celery.broadcast_message_tasks import send_broadcast_event
from app.broadcast_message.broadcast_message_schema import (
create_broadcast_message_schema,
update_broadcast_message_schema,
@@ -196,7 +196,7 @@ def _create_broadcast_event(broadcast_message):
# save to the DB
dao_create_broadcast_message(event)
send_broadcast_message.apply_async(
kwargs={'broadcast_message_id': str(broadcast_message.id)},
send_broadcast_event.apply_async(
kwargs={'broadcast_event_id': str(event.id)},
queue=QueueNames.NOTIFY
)

View File

@@ -4,7 +4,7 @@ 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
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")
@@ -35,3 +35,27 @@ def send_broadcast_message(broadcast_message_id, provider='stub-1'):
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}'
)

View File

@@ -24,6 +24,10 @@ def dao_get_broadcast_message_by_id(broadcast_message_id):
return BroadcastMessage.query.get(broadcast_message_id)
def dao_get_broadcast_event_by_id(broadcast_event_id):
return BroadcastEvent.query.get(broadcast_event_id)
def dao_get_broadcast_messages_for_service(service_id):
return BroadcastMessage.query.filter(
BroadcastMessage.service_id == service_id

View File

@@ -2338,20 +2338,19 @@ class BroadcastEvent(db.Model):
def get_earlier_message_references(self):
from app.dao.broadcast_message_dao import get_earlier_events_for_broadcast_event
return [event.reference for event in get_earlier_events_for_broadcast_event(self)]
return [event.reference for event in get_earlier_events_for_broadcast_event(self.id)]
def serialize(self):
return {
'id': self.id,
'id': str(self.id),
'service_id': self.service_id,
'reference': self.reference,
'service_id': str(self.service_id),
'previous_event_references': self.get_earlier_message_references(),
'broadcast_message_id': self.broadcast_message_id,
'sent_at': self.sent_at,
'broadcast_message_id': str(self.broadcast_message_id),
# sent_at is required by BroadcastMessageTemplate.from_broadcast_event
'sent_at': self.sent_at.strftime(DATETIME_FORMAT),
'message_type': self.message_type,
'transmitted_content': self.transmitted_content,
@@ -2359,6 +2358,7 @@ class BroadcastEvent(db.Model):
'transmitted_sender': self.transmitted_sender,
'transmitted_starts_at': get_dt_string_or_none(self.transmitted_starts_at),
'transmitted_finishes_at': get_dt_string_or_none(self.transmitted_finishes_at),
# transmitted_finishes_at is required by BroadcastMessageTemplate.from_broadcast_event
'transmitted_finishes_at': self.transmitted_finishes_at.strftime(DATETIME_FORMAT),
}