Exemple #1
0
def register_request_email(app_name, app_email, app_url, recipient_list, token, subject, template, fail_silently=False, correlation_id=""):
    logger = Helpers().get_logger(__name__)

    logger.info(
        _("Worker started processing register_request_email task with parameters %(parameters)s {'correlationId':'%(correlationId)s'}") % {
            "parameters": json.dumps({}),
            "correlationId": correlation_id
        }
    )

    try:
        send_mail(
            subject,
            "",
            app_email,
            recipient_list,
            fail_silently=fail_silently,
            html_message=render_to_string(template, {
                "app_url": app_url,
                "token": token,
                "app_name": app_name,
                "subject": subject
            }))
        return {
            "status": "passed",
            "result": "{}"
        }
    except Exception as e:
        return {
            "status": "failed",
            "result": {
                "error": str(e)
            }
        }
Exemple #2
0
def ping(text="PONG", correlation_id=""):
    logger = Helpers().get_logger(__name__)

    logger.info(_("Worker started processing ping task with parameters %(parameters)s {'correlationId':'%(correlationId)s'}") % {
        "parameters": json.dumps({"text": text}),
        "correlationId": correlation_id
    })

    return {"status": "passed", "result": text}
def incident_update(incident_update_id, user_id, correlation_id=""):
    logger = Helpers().get_logger(__name__)

    logger.info(
        _("Worker started processing incident_update task with parameters %(parameters)s {'correlationId':'%(correlationId)s'}"
          ) % {
              "parameters": json.dumps({}),
              "correlationId": correlation_id
          })

    incident_update_notification_module = IncidentUpdateNotificationModule()
    subscriber_module = SubscriberModule()
    task_module = TaskModule()

    for subscriber in subscriber_module.get_iterator():
        notification = incident_update_notification_module.is_subscriber_notified(
            incident_update_id, subscriber.id)
        if notification:
            # Send notification.id to the queue again
            task_module.delay("notify_subscriber",
                              {"notification_id": notification.id}, user_id)

        else:
            # Create new notification and send to queue
            new_notification = incident_update_notification_module.insert_one({
                "status":
                "pending",
                "incident_update_id":
                incident_update_id,
                "subscriber_id":
                subscriber.id
            })
            if new_notification:
                # Send new_notification.id to the Queue
                task_module.delay("notify_subscriber",
                                  {"notification_id": new_notification.id},
                                  user_id)

    return {"status": "passed", "result": "{}", "notify_type": "passed"}
def verify_subscriber(subscriber_id, correlation_id=""):
    logger = Helpers().get_logger(__name__)

    logger.info(
        _("Worker started processing verify_subscriber task with parameters %(parameters)s {'correlationId':'%(correlationId)s'}"
          ) % {
              "parameters": json.dumps({}),
              "correlationId": correlation_id
          })

    subscriber_module = SubscriberModule()
    subscriber = subscriber_module.get_one_by_id(subscriber_id)

    if not subscriber:
        return {"status": "passed", "result": "{}"}

    if subscriber.type == SubscriberModule.EMAIL:
        result = __verify_email()
    elif subscriber.type == SubscriberModule.PHONE:
        result = __verify_phone()
    elif subscriber.type == SubscriberModule.ENDPOINT:
        result = __verify_endpoint()

    return result
def notify_subscriber(notification_id, correlation_id=""):
    logger = Helpers().get_logger(__name__)

    logger.info(
        _("Worker started processing notify_subscriber task with parameters %(parameters)s {'correlationId':'%(correlationId)s'}"
          ) % {
              "parameters": json.dumps({}),
              "correlationId": correlation_id
          })

    option_entity = OptionEntity()
    incident_update_notification_module = IncidentUpdateNotificationModule()

    app_name = option_entity.get_value_by_key("app_name")
    app_email = option_entity.get_value_by_key("app_email")
    app_url = option_entity.get_value_by_key("app_url")

    notification = incident_update_notification_module.get_one_by_id(
        notification_id)
    incident_update = notification["incident_update"]
    incident = notification["incident_update"].incident
    subscriber = notification["subscriber"]

    data = {}
    data["incident_uri"] = incident.uri
    data["incident_update_time"] = incident_update.datetime.strftime(
        "%b %d %Y %H:%M:%S")
    data["incident_type"] = incident_update.status.title()
    data["incident_update"] = markdown2.markdown(incident_update.message)

    if notification["status"] == "pending":
        # send the notification for the first time
        if subscriber.type == SubscriberModule.EMAIL:
            status = __deliver_email(
                app_name, app_email, app_url, [subscriber.email],
                _("%(app_name)s Incident Update: %(incident_name)s") % {
                    "app_name": app_name,
                    "incident_name": incident.name
                }, "mails/incident_update.html", data, False)
        elif subscriber.type == SubscriberModule.PHONE:
            status = __deliver_sms(
                app_name, subscriber.phone, "%s%s" %
                (app_url.strip("/"),
                 reverse("app.web.incidents", kwargs={'uri': incident.uri})))
        elif subscriber.type == SubscriberModule.ENDPOINT:
            status = __deliver_webhook(subscriber.endpoint,
                                       subscriber.auth_token, "{}")

        if status:
            # message sent
            incident_update_notification_module.update_one_by_id(
                notification["id"], {"status": "success"})

        else:
            # message failed
            incident_update_notification_module.update_one_by_id(
                notification["id"], {"status": "failed"})

    elif notification["status"] == "failed":
        # Retry to send the notification
        if subscriber.type == SubscriberModule.EMAIL:
            status = __deliver_email(
                app_name, app_email, app_url, [subscriber.email],
                _("%(app_name)s Incident Update: %(incident_name)s") % {
                    "app_name": app_name,
                    "incident_name": incident.name
                }, "mails/incident_update.html", data, False)
        elif subscriber.type == SubscriberModule.PHONE:
            status = __deliver_sms(
                app_name, subscriber.phone, "%s%s" %
                (app_url.strip("/"),
                 reverse("app.web.incidents", kwargs={'uri': incident.uri})))
        elif subscriber.type == SubscriberModule.ENDPOINT:
            status = __deliver_webhook(subscriber.endpoint,
                                       subscriber.auth_token, "{}")
        if status:
            # message sent again
            incident_update_notification_module.update_one_by_id(
                notification["id"], {"status": "success"})
    else:
        print("skip subscriber#%s!" % subscriber.id)

    return {"status": "passed", "result": "{}", "notify_type": "passed"}