-
-
Notifications
You must be signed in to change notification settings - Fork 758
Add option to specify when to emit st2.generic.actiontrigger #4315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,6 @@ | |
|
||
LOG = logging.getLogger(__name__) | ||
|
||
ACTION_SENSOR_ENABLED = cfg.CONF.action_sensor.enable | ||
# XXX: Fix this nasty positional dependency. | ||
ACTION_TRIGGER_TYPE = INTERNAL_TRIGGER_TYPES['action'][0] | ||
NOTIFY_TRIGGER_TYPE = INTERNAL_TRIGGER_TYPES['action'][1] | ||
|
@@ -79,22 +78,18 @@ def process(self, execution_db): | |
extra = {'execution': execution_db} | ||
LOG.debug('Processing action execution "%s".', execution_id, extra=extra) | ||
|
||
if execution_db.status not in LIVEACTION_COMPLETED_STATES: | ||
msg = 'Skip action execution "%s" because state "%s" is not in a completed state.' | ||
LOG.debug(msg % (str(execution_db.id), execution_db.status), extra=extra) | ||
return | ||
|
||
# Get the corresponding liveaction record. | ||
liveaction_db = LiveAction.get_by_id(execution_db.liveaction['id']) | ||
|
||
# If the action execution is executed under an orquesta workflow, policies for the | ||
# action execution will be applied by the workflow engine. A policy may affect the | ||
# final state of the action execution thereby impacting the state of the workflow. | ||
if not workflow_service.is_action_execution_under_workflow_context(execution_db): | ||
policy_service.apply_post_run_policies(liveaction_db) | ||
if execution_db.status in LIVEACTION_COMPLETED_STATES: | ||
# If the action execution is executed under an orquesta workflow, policies for the | ||
# action execution will be applied by the workflow engine. A policy may affect the | ||
# final state of the action execution thereby impacting the state of the workflow. | ||
if not workflow_service.is_action_execution_under_workflow_context(execution_db): | ||
policy_service.apply_post_run_policies(liveaction_db) | ||
|
||
if liveaction_db.notify is not None: | ||
self._post_notify_triggers(liveaction_db=liveaction_db, execution_db=execution_db) | ||
if liveaction_db.notify is not None: | ||
self._post_notify_triggers(liveaction_db=liveaction_db, execution_db=execution_db) | ||
|
||
self._post_generic_trigger(liveaction_db=liveaction_db, execution_db=execution_db) | ||
|
||
|
@@ -231,11 +226,25 @@ def _get_trace_context(self, execution_id): | |
return None | ||
|
||
def _post_generic_trigger(self, liveaction_db=None, execution_db=None): | ||
if not ACTION_SENSOR_ENABLED: | ||
if not cfg.CONF.action_sensor.enable: | ||
LOG.debug('Action trigger is disabled, skipping trigger dispatch...') | ||
return | ||
|
||
execution_id = str(execution_db.id) | ||
extra = {'execution': execution_db} | ||
|
||
target_statuses = cfg.CONF.action_sensor.emit_when | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small style thing - I would do Besides that, LGTM 👍 |
||
if not target_statuses: | ||
if execution_db.status not in LIVEACTION_COMPLETED_STATES: | ||
msg = 'Skip action execution "%s" because state "%s" is not in a completed state.' | ||
LOG.debug(msg % (execution_id, execution_db.status), extra=extra) | ||
return | ||
else: | ||
if execution_db.status not in target_statuses: | ||
msg = 'Skip action execution "%s" because state "%s" is not in %s' | ||
LOG.debug(msg % (execution_id, execution_db.status, target_statuses), extra=extra) | ||
return | ||
|
||
payload = {'execution_id': execution_id, | ||
'status': liveaction_db.status, | ||
'start_timestamp': str(liveaction_db.start_timestamp), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -388,6 +388,10 @@ def register_opts(ignore_errors=False): | |
cfg.BoolOpt( | ||
'enable', default=True, | ||
help='Whether to enable or disable the ability to post a trigger on action.'), | ||
cfg.ListOpt( | ||
'emit_when', default=[], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For explicitness sake, you could perhaps default this option to This way you could also simple the code here a bit - https://github.com/StackStorm/st2/pull/4315/files#diff-70b51c2693ac263742fc28ad30ebc932R234. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If we specify the list here explicitly, then we need to follow
Anyways that test case still needs to be there because users might have chance to specify a null or a blank list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true, but I don't think that should be a big deal because any change to that constant would also mean user would need to update and restart all the services so the change would be picked up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will merge this into master and open a small PR with changelog entry and this change here which will allow us to slightly simplify the if statement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And another reason why I prefer to use a default value here is because it's more explicit. If this option contain no default value here, user needs to dig through the code and documentation to figure out what a default value of |
||
help='List of execution statuses that a trigger will be emitted. ' | ||
'If not specified, it defaults to action completion.'), | ||
] | ||
|
||
do_register_opts(action_sensor_opts, group='action_sensor') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker, but I found previous code a bit easier to read / follow ("return early" vs "nested if", one level less of nesting).
Was there any particular reason why you changed it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is inevitable since:
apply_post_run_policies
->_post_notify_triggers
->_post_generic_trigger
)_post_generic_trigger
regardless of the status. So If we want to do a "early return" then I need to move that call beforeif execution_db.status not in LIVEACTION_COMPLETED_STATES:
line but that will break the order (_post_generic_trigger
will be called before_post_notify_triggers
)