| 1 |
============================ |
| 2 |
How to write a Custom Action |
| 3 |
============================ |
| 4 |
|
| 5 |
1. Write a class inherited from mistral.actions.Action |
| 6 |
|
| 7 |
.. code-block:: python |
| 8 |
|
| 9 |
from mistral_lib import actions |
| 10 |
|
| 11 |
class RunnerAction(actions.Action): |
| 12 |
def __init__(self, param): |
| 13 |
# store the incoming params |
| 14 |
self.param = param |
| 15 |
|
| 16 |
def run(self, context): |
| 17 |
|
| 18 |
# Actions can be returned in a manner of ways. The simplest is |
| 19 |
# return {'status': 0} |
| 20 |
# or using a Result object. The Result has an optional parameter data |
| 21 |
# that can be used to transfer information |
| 22 |
return actions.Result() |
| 23 |
# Failed executions can also be returned using a workflow Result object |
| 24 |
# that contains an non empty error parameter such as: |
| 25 |
# return actions.Result(error="error text") |
| 26 |
|
| 27 |
|
| 28 |
2. Publish the class in a namespace (in your ``setup.cfg``) |
| 29 |
|
| 30 |
|
| 31 |
.. code-block:: ini |
| 32 |
|
| 33 |
[entry_points] |
| 34 |
mistral.actions = |
| 35 |
example.runner = my.mistral_plugins.somefile:RunnerAction |
| 36 |
|
| 37 |
3. Reinstall your library package if it was installed in system |
| 38 |
(not in virtualenv). |
| 39 |
|
| 40 |
4. Run db-sync tool to ensure your actions are in Mistral's database |
| 41 |
|
| 42 |
.. code-block:: console |
| 43 |
|
| 44 |
$ mistral-db-manage --config-file <path-to-config> populate |
| 45 |
|
| 46 |
5. Now you can call the action ``example.runner`` |
| 47 |
|
| 48 |
.. code-block:: yaml |
| 49 |
|
| 50 |
my_workflow: |
| 51 |
tasks: |
| 52 |
my_action_task: |
| 53 |
action: example.runner |
| 54 |
input: |
| 55 |
param: avalue_to_pass_in |