Skip to content

Commit 0ae2a62

Browse files
authored
Merge pull request #4630 from StackStorm/fix_non_fatal_error_log_level
Log non-fatal errors during service bootstrap phase under DEBUG instead of ERROR
2 parents baada18 + 39ae2d7 commit 0ae2a62

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Fixed
110110
#4615
111111
* Fix an issue with new line characters (``\n``) being converted to ``\r\n`` in remote shell
112112
command and script actions which use sudo. (bug fix) #4623
113+
* Update service bootstrap and ``st2-register-content`` script code so non-fatal errors are
114+
suppressed by default and only logged under ``DEBUG`` log level. (bug fix) #3933 #4626 #4630
113115

114116
2.10.4 - March 15, 2019
115117
-----------------------

st2common/st2common/services/triggers.py

+35-8
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def create_trigger_db(trigger_api):
245245
return trigger_db
246246

247247

248-
def create_or_update_trigger_db(trigger):
248+
def create_or_update_trigger_db(trigger, log_not_unique_error_as_debug=False):
249249
"""
250250
Create a new TriggerDB model if one doesn't exist yet or update existing
251251
one.
@@ -269,7 +269,8 @@ def create_or_update_trigger_db(trigger):
269269
if is_update:
270270
trigger_db.id = existing_trigger_db.id
271271

272-
trigger_db = Trigger.add_or_update(trigger_db)
272+
trigger_db = Trigger.add_or_update(trigger_db,
273+
log_not_unique_error_as_debug=log_not_unique_error_as_debug)
273274

274275
extra = {'trigger_db': trigger_db}
275276

@@ -331,13 +332,20 @@ def cleanup_trigger_db_for_rule(rule_db):
331332
Trigger.delete_if_unreferenced(existing_trigger_db)
332333

333334

334-
def create_trigger_type_db(trigger_type):
335+
def create_trigger_type_db(trigger_type, log_not_unique_error_as_debug=False):
335336
"""
336337
Creates a trigger type db object in the db given trigger_type definition as dict.
337338
338339
:param trigger_type: Trigger type model.
339340
:type trigger_type: ``dict``
340341
342+
:param log_not_unique_error_as_debug: True to lot NotUnique errors under debug instead of
343+
error log level. This is to be used in scenarios where
344+
failure is non-fatal (e.g. when services register
345+
internal trigger types which is an idempotent
346+
operation).
347+
:type log_not_unique_error_as_debug: ``bool``
348+
341349
:rtype: ``object``
342350
"""
343351
trigger_type_api = TriggerTypeAPI(**trigger_type)
@@ -349,13 +357,23 @@ def create_trigger_type_db(trigger_type):
349357
if not trigger_type_db:
350358
trigger_type_db = TriggerTypeAPI.to_model(trigger_type_api)
351359
LOG.debug('verified trigger and formulated TriggerDB=%s', trigger_type_db)
352-
trigger_type_db = TriggerType.add_or_update(trigger_type_db)
360+
trigger_type_db = TriggerType.add_or_update(trigger_type_db,
361+
log_not_unique_error_as_debug=log_not_unique_error_as_debug)
362+
353363
return trigger_type_db
354364

355365

356-
def create_shadow_trigger(trigger_type_db):
366+
def create_shadow_trigger(trigger_type_db, log_not_unique_error_as_debug=False):
357367
"""
358368
Create a shadow trigger for TriggerType with no parameters.
369+
370+
:param log_not_unique_error_as_debug: True to lot NotUnique errors under debug instead of
371+
error log level. This is to be used in scenarios where
372+
failure is non-fatal (e.g. when services register
373+
internal trigger types which is an idempotent
374+
operation).
375+
:type log_not_unique_error_as_debug: ``bool``
376+
359377
"""
360378
trigger_type_ref = trigger_type_db.get_reference().ref
361379

@@ -368,16 +386,24 @@ def create_shadow_trigger(trigger_type_db):
368386
'type': trigger_type_ref,
369387
'parameters': {}}
370388

371-
return create_or_update_trigger_db(trigger)
389+
return create_or_update_trigger_db(trigger,
390+
log_not_unique_error_as_debug=log_not_unique_error_as_debug)
372391

373392

374-
def create_or_update_trigger_type_db(trigger_type):
393+
def create_or_update_trigger_type_db(trigger_type, log_not_unique_error_as_debug=False):
375394
"""
376395
Create or update a trigger type db object in the db given trigger_type definition as dict.
377396
378397
:param trigger_type: Trigger type model.
379398
:type trigger_type: ``dict``
380399
400+
:param log_not_unique_error_as_debug: True to lot NotUnique errors under debug instead of
401+
error log level. This is to be used in scenarios where
402+
failure is non-fatal (e.g. when services register
403+
internal trigger types which is an idempotent
404+
operation).
405+
:type log_not_unique_error_as_debug: ``bool``
406+
381407
:rtype: ``object``
382408
"""
383409
assert isinstance(trigger_type, dict)
@@ -399,7 +425,8 @@ def create_or_update_trigger_type_db(trigger_type):
399425
trigger_type_api.id = existing_trigger_type_db.id
400426

401427
try:
402-
trigger_type_db = TriggerType.add_or_update(trigger_type_api)
428+
trigger_type_db = TriggerType.add_or_update(trigger_type_api,
429+
log_not_unique_error_as_debug=log_not_unique_error_as_debug)
403430
except StackStormDBObjectConflictError:
404431
# Operation is idempotent and trigger could have already been created by
405432
# another process. Ignore object already exists because it simply means

st2common/st2common/triggers.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
from __future__ import absolute_import
17+
1718
import six
1819

1920
from mongoengine import ValidationError
@@ -23,7 +24,8 @@
2324
from st2common import log as logging
2425
from st2common.constants.triggers import (INTERNAL_TRIGGER_TYPES, ACTION_SENSOR_TRIGGER)
2526
from st2common.exceptions.db import StackStormDBObjectConflictError
26-
from st2common.services.triggers import create_trigger_type_db, create_shadow_trigger
27+
from st2common.services.triggers import create_trigger_type_db
28+
from st2common.services.triggers import create_shadow_trigger
2729
from st2common.services.triggers import get_trigger_type_db
2830
from st2common.models.system.common import ResourceReference
2931

@@ -36,11 +38,12 @@
3638

3739
def _register_internal_trigger_type(trigger_definition):
3840
try:
39-
trigger_type_db = create_trigger_type_db(trigger_type=trigger_definition)
41+
trigger_type_db = create_trigger_type_db(trigger_type=trigger_definition,
42+
log_not_unique_error_as_debug=True)
4043
except (NotUniqueError, StackStormDBObjectConflictError):
4144
# We ignore conflict error since this operation is idempotent and race is not an issue
42-
LOG.debug('Internal trigger type "%s" already exists, ignoring...' %
43-
(trigger_definition['name']), exc_info=True)
45+
LOG.debug('Internal trigger type "%s" already exists, ignoring error...' %
46+
(trigger_definition['name']))
4447

4548
ref = ResourceReference.to_string_reference(name=trigger_definition['name'],
4649
pack=trigger_definition['pack'])
@@ -52,12 +55,13 @@ def _register_internal_trigger_type(trigger_definition):
5255
# trigger types with parameters do no require a shadow trigger.
5356
if trigger_type_db and not trigger_type_db.parameters_schema:
5457
try:
55-
trigger_db = create_shadow_trigger(trigger_type_db)
58+
trigger_db = create_shadow_trigger(trigger_type_db,
59+
log_not_unique_error_as_debug=True)
5660

5761
extra = {'trigger_db': trigger_db}
5862
LOG.audit('Trigger created for parameter-less internal TriggerType. Trigger.id=%s' %
5963
(trigger_db.id), extra=extra)
60-
except StackStormDBObjectConflictError:
64+
except (NotUniqueError, StackStormDBObjectConflictError):
6165
LOG.debug('Shadow trigger "%s" already exists. Ignoring.',
6266
trigger_type_db.get_reference().ref, exc_info=True)
6367

@@ -73,7 +77,11 @@ def register_internal_trigger_types():
7377
"""
7478
Register internal trigger types.
7579
76-
Note: This method blocks until all the trigger types have been registered.
80+
NOTE 1: This method blocks until all the trigger types have been registered.
81+
82+
NOTE 2: We log "NotUniqueError" errors under debug and not error. Those errors are not fatal
83+
because this operation is idempotent and NotUniqueError simply means internal trigger type
84+
has already been registered by some other service.
7785
"""
7886
action_sensor_enabled = cfg.CONF.action_sensor.enable
7987

0 commit comments

Comments
 (0)