Skip to content

Commit

Permalink
Add work-around for trigger looks ups.
Browse files Browse the repository at this point in the history
If id or uid is available, use that instead of trying to look up trigger by
"parameters" dictionary. This way we can avoid parameters dictionary lookup
related bugs in pymongo / mongoengine.
  • Loading branch information
Kami committed Jul 1, 2016
1 parent 9f128b7 commit 884ef04
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
3 changes: 3 additions & 0 deletions st2common/st2common/models/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def get_by_name(self, value):
def get_by_id(self, value):
return self.get(id=value, raise_exception=True)

def get_by_uid(self, value):
return self.get(uid=value, raise_exception=True)

def get_by_ref(self, value):
return self.get(ref=value, raise_exception=True)

Expand Down
4 changes: 4 additions & 0 deletions st2common/st2common/persistence/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def get_by_name(cls, value):
def get_by_id(cls, value):
return cls._get_impl().get_by_id(value)

@classmethod
def get_by_uid(cls, value):
return cls._get_impl().get_by_uid(value)

@classmethod
def get_by_ref(cls, value):
return cls._get_impl().get_by_ref(value)
Expand Down
38 changes: 38 additions & 0 deletions st2common/st2common/services/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
'add_trigger_models',

'get_trigger_db_by_ref',
'get_trigger_db_by_id',
'get_trigger_db_by_uid',
'get_trigger_db_given_type_and_params',
'get_trigger_type_db',

Expand Down Expand Up @@ -92,6 +94,42 @@ def get_trigger_db_given_type_and_params(type=None, parameters=None):
return None


def get_trigger_db_by_id(id):
"""
Returns the trigger object from db given a trigger id.
:param ref: Reference to the trigger db object.
:type ref: ``str``
:rtype: ``object``
"""
try:
return Trigger.get_by_id(id)
except StackStormDBObjectNotFoundError as e:
LOG.debug('Database lookup for id="%s" resulted in exception : %s.',
id, e, exc_info=True)

return None


def get_trigger_db_by_uid(uid):
"""
Returns the trigger object from db given a trigger uid.
:param ref: Reference to the trigger db object.
:type ref: ``str``
:rtype: ``object``
"""
try:
return Trigger.get_by_uid(uid)
except StackStormDBObjectNotFoundError as e:
LOG.debug('Database lookup for uid="%s" resulted in exception : %s.',
uid, e, exc_info=True)

return None


def get_trigger_db_by_ref(ref):
"""
Returns the trigger object from db given a string ref.
Expand Down
21 changes: 17 additions & 4 deletions st2reactor/st2reactor/container/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,23 @@ def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigg
if isinstance(trigger, six.string_types):
trigger_db = TriggerService.get_trigger_db_by_ref(trigger)
else:
type_ = trigger.get('type', None)
parameters = trigger.get('parameters', {})
trigger_db = TriggerService.get_trigger_db_given_type_and_params(type=type_,
parameters=parameters)
# If id / uid is available we try to look up Trigger by id. This way we can avoid bug in
# pymongo / mongoengine related to "parameters" dictionary lookups
id_ = trigger.get('id', None)
uid = trigger.get('uid', None)

# TODO: Remove parameters dictionary look up when we can confirm each trigger dictionary
# passed to this method always contains id or uid
if id_:
trigger_db = TriggerService.get_trigger_db_by_id(id=id)
elif uid:
trigger_db = TriggerService.get_trigger_db_by_uid(uid=uid)
else:
# Last resort - look it up by parameters
type_ = trigger.get('type', None)
parameters = trigger.get('parameters', {})
trigger_db = TriggerService.get_trigger_db_given_type_and_params(type=type_,
parameters=parameters)

if trigger_db is None:
LOG.debug('No trigger in db for %s', trigger)
Expand Down

0 comments on commit 884ef04

Please sign in to comment.