def add_or_update( cls, model_object, publish=True, dispatch_trigger=True, validate=True, log_not_unique_error_as_debug=False, ): # Late import to avoid very expensive in-direct import (~1 second) when this function # is not called / used from mongoengine import NotUniqueError pre_persist_id = model_object.id try: model_object = cls._get_impl().add_or_update(model_object, validate=True) except NotUniqueError as e: if log_not_unique_error_as_debug: LOG.debug("Conflict while trying to save in DB: %s.", six.text_type(e)) else: LOG.exception("Conflict while trying to save in DB.") # On a conflict determine the conflicting object and return its id in # the raised exception. conflict_object = cls._get_by_object(model_object) conflict_id = str(conflict_object.id) if conflict_object else None message = six.text_type(e) raise StackStormDBObjectConflictError(message=message, conflict_id=conflict_id, model_object=model_object) is_update = str(pre_persist_id) == str(model_object.id) # Publish internal event on the message bus if publish: try: if is_update: cls.publish_update(model_object) else: cls.publish_create(model_object) except: LOG.exception("Publish failed.") # Dispatch trigger if dispatch_trigger: try: if is_update: cls.dispatch_update_trigger(model_object) else: cls.dispatch_create_trigger(model_object) except: LOG.exception("Trigger dispatch failed.") return model_object
def add_or_update(cls, model_object, publish=True, dispatch_trigger=True, log_not_unique_error_as_debug=False): pre_persist_id = model_object.id try: model_object = cls._get_impl().add_or_update(model_object) except NotUniqueError as e: if log_not_unique_error_as_debug: LOG.debug('Conflict while trying to save in DB.', exc_info=True) else: LOG.exception('Conflict while trying to save in DB.') # On a conflict determine the conflicting object and return its id in # the raised exception. conflict_object = cls._get_by_object(model_object) conflict_id = str(conflict_object.id) if conflict_object else None message = str(e) raise StackStormDBObjectConflictError(message=message, conflict_id=conflict_id, model_object=model_object) is_update = str(pre_persist_id) == str(model_object.id) # Publish internal event on the message bus if publish: try: if is_update: cls.publish_update(model_object) else: cls.publish_create(model_object) except: LOG.exception('Publish failed.') # Dispatch trigger if dispatch_trigger: try: if is_update: cls.dispatch_update_trigger(model_object) else: cls.dispatch_create_trigger(model_object) except: LOG.exception('Trigger dispatch failed.') return model_object
def insert(cls, model_object, publish=True, dispatch_trigger=True, log_not_unique_error_as_debug=False): # Late import to avoid very expensive in-direct import (~1 second) when this function # is not called / used from mongoengine import NotUniqueError if model_object.id: raise ValueError('id for object %s was unexpected.' % model_object) try: model_object = cls._get_impl().insert(model_object) except NotUniqueError as e: if log_not_unique_error_as_debug: LOG.debug('Conflict while trying to save in DB: %s.', six.text_type(e)) else: LOG.exception('Conflict while trying to save in DB.') # On a conflict determine the conflicting object and return its id in # the raised exception. conflict_object = cls._get_by_object(model_object) conflict_id = str(conflict_object.id) if conflict_object else None message = six.text_type(e) raise StackStormDBObjectConflictError(message=message, conflict_id=conflict_id, model_object=model_object) # Publish internal event on the message bus if publish: try: cls.publish_create(model_object) except: LOG.exception('Publish failed.') # Dispatch trigger if dispatch_trigger: try: cls.dispatch_create_trigger(model_object) except: LOG.exception('Trigger dispatch failed.') return model_object
def add_or_update(cls, model_object, publish=True): pre_persist_id = model_object.id try: model_object = cls._get_impl().add_or_update(model_object) except NotUniqueError as e: LOG.exception('Conflict while trying to save in DB.') # On a conflict determine the conflicting object and return its id in # the raised exception. conflict_object = cls._get_by_object(model_object) conflict_id = str(conflict_object.id) if conflict_object else None raise StackStormDBObjectConflictError(str(e), conflict_id) try: if publish: if str(pre_persist_id) == str(model_object.id): cls.publish_update(model_object) else: cls.publish_create(model_object) except: LOG.exception('publish failed.') return model_object