Is there a way to trigger event in workflow like inquiry and consume it in rules?

rules

(jeff) #1

Hi there,

Just wondering if there is a way to trigger an event in workflow1, and such event could be inspected by rules in order to trigger workflow2? I know that this could be done by calling workflow2 directly in workflow1, but if we can trigger event we don’t need to know the workflow2. That will be asynchronous and the failure of workflow2 won’t impact workflow1.

In inquiry we can specify ‘route: route_name’, and we can define rules to catch the ‘trigger.route’ and consume that event once an inquiry is created. So could we do it the same way in our workflows?

I’ve looked thorough the sensor and it looks like the sensor can poll or accept passively from external system, then trigger event. But it seems that it could not be used for such scenario.

Thanks a lot.


(Tomaz Muraus) #2

Right now there is no way to directly dispatch an event (trigger) inside a workflow, but there are multiple ways to achieve the same end result.

The best one probably is to add a new action to the workflow which sends an event / trigger using a webhook - Webhooks — StackStorm 2.7.2 documentation.

For that, you can use the existing “core.http” action. Something along those lines:

   ---
    chain:
        ....
        -
            name: "send_trigger_via_webhook"
            ref: "core.http"
            parameters:
                url: "https://localhost/api/v1/webhooks/st2"
                method: "POST"
                headers: {"Content-Type": "application/json"}
                body: '{"trigger": "mypack.mytrigger", "payload": {"key1": "value1"}}"
            on-success: "..."
        ....

Then you can match on this trigger inside a rule.

Other options include adding a new Python action which dispatches a trigger using the same code which is used by sensors - st2/sensor_wrapper.py at master · StackStorm/st2 · GitHub (this requires a lot more work though).

And depending on your workflow composition, you might also be able to utilize “st2.generic.actiontrigger” trigger (Sensors and Triggers — StackStorm 2.7.2 documentation). This trigger is dispatched every time an action execution completes.


(jeff) #3

Thanks @kami for your kindly support. Using webhook is the simplest way.
The difference between webhook and sensor_wrapper is that using webhook we need to provide the x-auth-token in the header, which means we need to configure username/password of stackstorm in config.yaml, read them and call the auth API to get the token then put in the header, right?

In sensor_wrapper it seems that we only to use TriggerDispatcher and which doesn’t need any credential, I want to try both of them.


(Tomaz Muraus) #4

You are welcome.

That’s correct (and that’s the downside of that approach).

We do actually generate temporary auth token which is available to every action execution via “ST2_ACTION_AUTH_TOKEN” environment variable during the life-time of that execution (Actions — StackStorm 2.7.2 documentation).

Sadly though, that only applies to python, local and remote runner actions and not workflow actions.

For Mistral workflows, token is available via “{{ env()[’__actions’][‘st2.action’][‘st2_context’][‘auth_token’] }}”, but unlike “ST2_ACTION_AUTH_TOKEN” environment variable, that’s really an implementation detail and not something I would rely on.


(jeff) #5

Thanks. And event triggered from pack1 could be consumed by the rule in pack2 right? If yes could an event be consumed by multiple rules in different packs?
And for the second approach do we need to create and register a custom trigger?It seems that it can only be done in a sensor so we need to create a sensor as well?


(Tomaz Muraus) #6

Yes, it can be consumed / used by rules from multiple packs.

And triggers can be registered independently outside of sensors. Here is an example - st2/sample-trigger.yaml at master · StackStorm/st2 · GitHub (it uses the same format as trigger_types array in sensor metadata yaml file).


(jeff) #7

Thanks alot @kami, that really helps!


(Jacob Floyd) #8

As of StackStorm 2.9, there is a new action: core.inject_trigger

The PR that added it: https://github.com/StackStorm/st2/pull/4259
The E2E test for it: Add some end to end tests for core.inject_trigger by Kami · Pull Request #140 · StackStorm/st2tests · GitHub

That E2E test shows some nifty things I didn’t know were possible. It uses a trigger defined in the examples pack, just like any action, rule, sensor, etc would be defined. edit: and I see @kami mentioned this trigger above. However did I miss that? LOL

And the actionchain task that injects that trigger: