def replay_created_notifications():
    # if the notification has not be send after 1 hour, then try to resend.
    resend_created_notifications_older_than = (60 * 60)
    for notification_type in (EMAIL_TYPE, SMS_TYPE):
        notifications_to_resend = notifications_not_yet_sent(
            resend_created_notifications_older_than,
            notification_type
        )

        if len(notifications_to_resend) > 0:
            current_app.logger.info("Sending {} {} notifications "
                                    "to the delivery queue because the notification "
                                    "status was created.".format(len(notifications_to_resend), notification_type))

        for n in notifications_to_resend:
            send_notification_to_queue(notification=n, research_mode=n.service.research_mode)

    # if the letter has not be send after an hour, then create a zendesk ticket
    letters = letters_missing_from_sending_bucket(resend_created_notifications_older_than)

    if len(letters) > 0:
        msg = "{} letters were created over an hour ago, " \
              "but do not have an updated_at timestamp or billable units. " \
              "\n Creating app.celery.letters_pdf_tasks.create_letters tasks to upload letter to S3 " \
              "and update notifications for the following notification ids: " \
              "\n {}".format(len(letters), [x.id for x in letters])

        current_app.logger.info(msg)
        for letter in letters:
            get_pdf_for_templated_letter.apply_async([str(letter.id)], queue=QueueNames.CREATE_LETTERS_PDF)
Exemplo n.º 2
0
def process_letter_notification(*,
                                letter_data,
                                api_key,
                                service,
                                template,
                                template_with_content,
                                reply_to_text,
                                precompiled=False):
    if api_key.key_type == KEY_TYPE_TEAM:
        raise BadRequestError(
            message='Cannot send letters with a team api key', status_code=403)

    if not service.research_mode and service.restricted and api_key.key_type != KEY_TYPE_TEST:
        raise BadRequestError(
            message='Cannot send letters when service is in trial mode',
            status_code=403)

    if precompiled:
        return process_precompiled_letter_notifications(
            letter_data=letter_data,
            api_key=api_key,
            service=service,
            template=template,
            reply_to_text=reply_to_text)

    postage = validate_address(service, letter_data['personalisation'])

    test_key = api_key.key_type == KEY_TYPE_TEST

    status = NOTIFICATION_CREATED
    updated_at = None
    if test_key:
        # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up
        if current_app.config['NOTIFY_ENVIRONMENT'] in [
                'preview', 'development'
        ]:
            status = NOTIFICATION_SENDING
        # mark test letter as delivered and do not create a fake response later
        else:
            status = NOTIFICATION_DELIVERED
            updated_at = datetime.utcnow()

    queue = QueueNames.CREATE_LETTERS_PDF if not test_key else QueueNames.RESEARCH_MODE

    notification = create_letter_notification(letter_data=letter_data,
                                              service=service,
                                              template=template,
                                              api_key=api_key,
                                              status=status,
                                              reply_to_text=reply_to_text,
                                              updated_at=updated_at,
                                              postage=postage)

    get_pdf_for_templated_letter.apply_async([str(notification.id)],
                                             queue=queue)

    if test_key and current_app.config['NOTIFY_ENVIRONMENT'] in [
            'preview', 'development'
    ]:
        create_fake_letter_response_file.apply_async(
            (notification.reference, ), queue=queue)
    resp = create_response_for_post_notification(
        notification_id=notification.id,
        client_reference=notification.client_reference,
        template_id=notification.template_id,
        template_version=notification.template_version,
        notification_type=notification.notification_type,
        reply_to=reply_to_text,
        service_id=notification.service_id,
        template_with_content=template_with_content)
    return resp
Exemplo n.º 3
0
def replay_create_pdf_letters(notification_id):
    print("Create task to get_pdf_for_templated_letter for notification: {}".
          format(notification_id))
    get_pdf_for_templated_letter.apply_async(
        [str(notification_id)], queue=QueueNames.CREATE_LETTERS_PDF)