def trigger_link_test(provider):
    identifier = str(uuid.uuid4())
    formatted_seq_number = None
    if provider == BroadcastProvider.VODAFONE:
        sequence = Sequence('broadcast_provider_message_number_seq')
        sequential_number = db.session.connection().execute(sequence)
        formatted_seq_number = format_sequential_number(sequential_number)
    message = f"Sending a link test to CBC proxy for provider {provider} with ID {identifier}"
    current_app.logger.info(message)
    cbc_proxy_client.get_proxy(provider).send_link_test(
        identifier, formatted_seq_number)
Example #2
0
def send_broadcast_provider_message(broadcast_event_id, provider):
    broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)

    broadcast_provider_message = create_broadcast_provider_message(
        broadcast_event, provider)
    formatted_message_number = None
    if provider == BroadcastProvider.VODAFONE:
        formatted_message_number = format_sequential_number(
            broadcast_provider_message.message_number)

    current_app.logger.info(f'invoking cbc proxy to send '
                            f'broadcast_event {broadcast_event.reference} '
                            f'msgType {broadcast_event.message_type}')

    areas = [{
        "polygon": polygon
    } for polygon in broadcast_event.transmitted_areas["simple_polygons"]]

    cbc_proxy_provider_client = cbc_proxy_client.get_proxy(provider)

    if broadcast_event.message_type == BroadcastEventMessageType.ALERT:
        cbc_proxy_provider_client.create_and_send_broadcast(
            identifier=str(broadcast_provider_message.id),
            message_number=formatted_message_number,
            headline="GOV.UK Notify Broadcast",
            description=broadcast_event.transmitted_content['body'],
            areas=areas,
            sent=broadcast_event.sent_at_as_cap_datetime_string,
            expires=broadcast_event.
            transmitted_finishes_at_as_cap_datetime_string,
        )
    elif broadcast_event.message_type == BroadcastEventMessageType.UPDATE:
        cbc_proxy_provider_client.update_and_send_broadcast(
            identifier=str(broadcast_provider_message.id),
            message_number=formatted_message_number,
            headline="GOV.UK Notify Broadcast",
            description=broadcast_event.transmitted_content['body'],
            areas=areas,
            previous_provider_messages=broadcast_event.
            get_earlier_provider_messages(provider),
            sent=broadcast_event.sent_at_as_cap_datetime_string,
            expires=broadcast_event.
            transmitted_finishes_at_as_cap_datetime_string,
        )
    elif broadcast_event.message_type == BroadcastEventMessageType.CANCEL:
        cbc_proxy_provider_client.cancel_broadcast(
            identifier=str(broadcast_provider_message.id),
            message_number=formatted_message_number,
            previous_provider_messages=broadcast_event.
            get_earlier_provider_messages(provider),
            sent=broadcast_event.sent_at_as_cap_datetime_string,
        )
def send_broadcast_provider_message(self, broadcast_event_id, provider):
    if not current_app.config['CBC_PROXY_ENABLED']:
        current_app.logger.info(
            "CBC Proxy disabled, not sending broadcast_provider_message for "
            f"broadcast_event_id {broadcast_event_id} with provider {provider}"
        )
        return

    broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)

    check_provider_message_should_send(broadcast_event, provider)

    # the broadcast_provider_message may already exist if we retried previously
    broadcast_provider_message = broadcast_event.get_provider_message(provider)
    if broadcast_provider_message is None:
        broadcast_provider_message = create_broadcast_provider_message(
            broadcast_event, provider)

    formatted_message_number = None
    if provider == BroadcastProvider.VODAFONE:
        formatted_message_number = format_sequential_number(
            broadcast_provider_message.message_number)

    current_app.logger.info(f'invoking cbc proxy to send '
                            f'broadcast_event {broadcast_event.reference} '
                            f'msgType {broadcast_event.message_type}')

    areas = [{
        "polygon": polygon
    } for polygon in broadcast_event.transmitted_areas["simple_polygons"]]

    cbc_proxy_provider_client = cbc_proxy_client.get_proxy(provider)

    try:
        if broadcast_event.message_type == BroadcastEventMessageType.ALERT:
            cbc_proxy_provider_client.create_and_send_broadcast(
                identifier=str(broadcast_provider_message.id),
                message_number=formatted_message_number,
                headline="GOV.UK Notify Broadcast",
                description=broadcast_event.transmitted_content['body'],
                areas=areas,
                sent=broadcast_event.sent_at_as_cap_datetime_string,
                expires=broadcast_event.
                transmitted_finishes_at_as_cap_datetime_string,
                channel=broadcast_event.service.broadcast_channel)
        elif broadcast_event.message_type == BroadcastEventMessageType.UPDATE:
            cbc_proxy_provider_client.update_and_send_broadcast(
                identifier=str(broadcast_provider_message.id),
                message_number=formatted_message_number,
                headline="GOV.UK Notify Broadcast",
                description=broadcast_event.transmitted_content['body'],
                areas=areas,
                previous_provider_messages=broadcast_event.
                get_earlier_provider_messages(provider),
                sent=broadcast_event.sent_at_as_cap_datetime_string,
                expires=broadcast_event.
                transmitted_finishes_at_as_cap_datetime_string,
                # We think an alert update should always go out on the same channel that created the alert
                # We recognise there is a small risk with this code here that if the services channel was
                # changed between an alert being sent out and then updated, then something might go wrong
                # but we are relying on service channels changing almost never, and not mid incident
                # We may consider in the future, changing this such that we store the channel a broadcast was
                # sent on on the broadcast message itself and pick the value from there instead of the service
                channel=broadcast_event.service.broadcast_channel)
        elif broadcast_event.message_type == BroadcastEventMessageType.CANCEL:
            cbc_proxy_provider_client.cancel_broadcast(
                identifier=str(broadcast_provider_message.id),
                message_number=formatted_message_number,
                previous_provider_messages=broadcast_event.
                get_earlier_provider_messages(provider),
                sent=broadcast_event.sent_at_as_cap_datetime_string,
            )
    except CBCProxyRetryableException as exc:
        delay = get_retry_delay(self.request.retries)
        current_app.logger.exception(
            f'Retrying send_broadcast_provider_message for broadcast_event {broadcast_event_id} and '
            + f'provider {provider} in {delay} seconds')

        self.retry(
            exc=exc,
            countdown=delay,
            queue=QueueNames.BROADCASTS,
        )

    update_broadcast_provider_message_status(
        broadcast_provider_message, status=BroadcastProviderMessageStatus.ACK)
def send_canary_to_cbc_proxy():
    if current_app.config['CBC_PROXY_ENABLED']:
        identifier = str(uuid.uuid4())
        message = f"Sending a canary message to CBC proxy with ID {identifier}"
        current_app.logger.info(message)
        cbc_proxy_client.get_proxy('canary').send_canary(identifier)