# Buggy code
def register_internal_trigger_types():
    """
    Register internal trigger types.

    Note: This method blocks until all the trigger types have been registered.
    """
    action_sensor_enabled = cfg.CONF.action_sensor.enable

    registered_trigger_types_db = []

    for _, trigger_definitions in six.iteritems(INTERNAL_TRIGGER_TYPES):
        for trigger_definition in trigger_definitions:
            LOG.debug("Registering internal trigger: %s", trigger_definition["name"])

            is_action_trigger = (
                trigger_definition["name"] == ACTION_SENSOR_TRIGGER["name"]
            )
            if is_action_trigger and not action_sensor_enabled:
                continue
            try:
                trigger_type_db = _register_internal_trigger_type(
                    trigger_definition=trigger_definition
                )
            except Exception:
                LOG.exception(
                    "Failed registering internal trigger: %s.", trigger_definition
                )
                raise
            else:
                registered_trigger_types_db.append(trigger_type_db)

    return registered_trigger_types_db


# Traceback (NotUniqueError)
Registering content...[flags = --config-file /etc/st2/st2.conf --register-all]
2017-11-10 16:04:14,401 INFO [-] Connecting to database "st2" @ "127.0.0.1:27017" as user "stackstorm".
2017-11-10 16:04:20,355 ERROR [-] Conflict while trying to save in DB.
Traceback (most recent call last):
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/persistence/base.py", line 173, in add_or_update
model_object = cls._get_impl().add_or_update(model_object)
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/models/db/__init__.py", line 336, in add_or_update
instance.save()
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/mongoengine/document.py", line 364, in save
raise NotUniqueError(message % six.text_type(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error collection: st2.trigger_type_d_b index: uid_1 dup key: { : "trigger_type:core:st2.key_value_pair.update" })
2017-11-10 16:04:20,432 ERROR [-] Conflict while trying to save in DB.
Traceback (most recent call last):
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/persistence/base.py", line 173, in add_or_update
model_object = cls._get_impl().add_or_update(model_object)
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/models/db/__init__.py", line 336, in add_or_update
instance.save()
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/mongoengine/document.py", line 364, in save
raise NotUniqueError(message % six.text_type(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error collection: st2.trigger_d_b index: uid_1 dup key: { : "trigger:core:st2.key_value_pair.value_change:99914b932bd37a50b983c5e7c90ae93b" })
2017-11-10 16:04:20,451 ERROR [-] Conflict while trying to save in DB.
Traceback (most recent call last):
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/persistence/base.py", line 173, in add_or_update
model_object = cls._get_impl().add_or_update(model_object)
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2common/models/db/__init__.py", line 336, in add_or_update
instance.save()
File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/mongoengine/document.py", line 364, in save
raise NotUniqueError(message % six.text_type(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error collection: st2.trigger_type_d_b index: uid_1 dup key: { : "trigger_type:core:st2.key_value_pair.delete" })
2017-11-10 16:04:20,567 INFO [-] =========================================================
2017-11-10 16:04:20,568 INFO [-] ############## Registering triggers #####################
2017-11-10 16:04:20,568 INFO [-] =========================================================
2017-11-10 16:04:20,673 INFO [-] Registered 0 triggers.

# Fixed code
def register_internal_trigger_types():
    """
    Register internal trigger types.

    NOTE 1: This method blocks until all the trigger types have been registered.

    NOTE 2: We log "NotUniqueError" errors under debug and not error. Those errors are not fatal
    because this operation is idempotent and NotUniqueError simply means internal trigger type
    has already been registered by some other service.
    """
    action_sensor_enabled = cfg.CONF.action_sensor.enable

    registered_trigger_types_db = []

    for _, trigger_definitions in six.iteritems(INTERNAL_TRIGGER_TYPES):
        for trigger_definition in trigger_definitions:
            LOG.debug("Registering internal trigger: %s", trigger_definition["name"])

            is_action_trigger = (
                trigger_definition["name"] == ACTION_SENSOR_TRIGGER["name"]
            )
            if is_action_trigger and not action_sensor_enabled:
                continue
            try:
                trigger_type_db = _register_internal_trigger_type(
                    trigger_definition=trigger_definition
                )
            except Exception:
                LOG.exception(
                    "Failed registering internal trigger: %s.", trigger_definition
                )
                raise
            else:
                registered_trigger_types_db.append(trigger_type_db)

    return registered_trigger_types_db