Class for handling dispatching of trigger.
| 27 | |
| 28 | |
| 29 | class TriggerDispatcherService(object): |
| 30 | """ |
| 31 | Class for handling dispatching of trigger. |
| 32 | """ |
| 33 | |
| 34 | def __init__(self, logger): |
| 35 | self._logger = logger |
| 36 | self._dispatcher = TriggerDispatcher(self._logger) |
| 37 | |
| 38 | def dispatch( |
| 39 | self, trigger, payload=None, trace_tag=None, throw_on_validation_error=False |
| 40 | ): |
| 41 | """ |
| 42 | Method which dispatches the trigger. |
| 43 | |
| 44 | :param trigger: Reference to the TriggerTypeDB (<pack>.<name>) or TriggerDB object. |
| 45 | :type trigger: ``str`` |
| 46 | |
| 47 | :param payload: Trigger payload. |
| 48 | :type payload: ``dict`` |
| 49 | |
| 50 | :param trace_tag: Tracer to track the triggerinstance. |
| 51 | :type trace_tags: ``str`` |
| 52 | |
| 53 | :param throw_on_validation_error: True to throw on validation error (if validate_payload is |
| 54 | True) instead of logging the error. |
| 55 | :type throw_on_validation_error: ``boolean`` |
| 56 | """ |
| 57 | # empty strings |
| 58 | trace_context = TraceContext(trace_tag=trace_tag) if trace_tag else None |
| 59 | self._logger.debug( |
| 60 | "Added trace_context %s to trigger %s.", trace_context, trigger |
| 61 | ) |
| 62 | return self.dispatch_with_context( |
| 63 | trigger, |
| 64 | payload=payload, |
| 65 | trace_context=trace_context, |
| 66 | throw_on_validation_error=throw_on_validation_error, |
| 67 | ) |
| 68 | |
| 69 | def dispatch_with_context( |
| 70 | self, trigger, payload=None, trace_context=None, throw_on_validation_error=False |
| 71 | ): |
| 72 | """ |
| 73 | Method which dispatches the trigger. |
| 74 | |
| 75 | :param trigger: Reference to the TriggerTypeDB (<pack>.<name>) or TriggerDB object. |
| 76 | :type trigger: ``str`` |
| 77 | |
| 78 | :param payload: Trigger payload. |
| 79 | :type payload: ``dict`` |
| 80 | |
| 81 | :param trace_context: Trace context to associate with Trigger. |
| 82 | :type trace_context: ``st2common.api.models.api.trace.TraceContext`` |
| 83 | |
| 84 | :param throw_on_validation_error: True to throw on validation error (if validate_payload is |
| 85 | True) instead of logging the error. |
| 86 | :type throw_on_validation_error: ``boolean`` |