Developing an Action
In this guide, we will outline each step to developing a Action for the DataHub Actions Framework.
Developing a DataHub Action is a matter of extending the
Action base class in Python, installing your
Action to make it visible to the framework, and then configuring the framework to use the new Action.
Step 1: Defining an Action
To implement an Action, we'll need to extend the
Action base class and override the following functions:
create()- This function is invoked to instantiate the action, with a free-form configuration dictionary extracted from the Actions configuration file as input.
act()- This function is invoked when an Action is received. It should contain the core logic of the Action.
close()- This function is invoked when the framework has issued a shutdown of the pipeline. It should be used to cleanup any processes happening inside the Action.
Let's start by defining a new implementation of Action called
CustomAction. We'll keep it simple-- this Action will
print the configuration that is provided when it is created, and print any Events that it receives.
from datahub_actions.action.action import Action
from datahub_actions.event.event_envelope import EventEnvelope
from datahub_actions.pipeline.pipeline_context import PipelineContext
def create(cls, config_dict: dict, ctx: PipelineContext) -> "Action":
# Simply print the config_dict.
def __init__(self, ctx: PipelineContext):
self.ctx = ctx
def act(self, event: EventEnvelope) -> None:
# Do something super important.
# For now, just print. :)
def close(self) -> None:
Step 2: Installing the Action
Now that we've defined the Action, we need to make it visible to the framework by making it available in the Python runtime environment.
The easiest way to do this is to just place it in the same directory as your configuration file, in which case the module name is the same as the file
name - in this case it will be
Advanced: Installing as a Package
Alternatively, create a
setup.py file in the same directory as the new Action to convert it into a package that pip can understand.
from setuptools import find_packages, setup
# if you don't already have DataHub Actions installed, add it under install_requires
Next, install the package
pip install -e .
inside the module. (alt.
Once we have done this, our class will be referencable via
Step 3: Running the Action
Now that we've defined our Action, we can create an Action configuration file that refers to the new Action. We will need to provide the fully-qualified Python module & class name when doing so.
# Some sample configuration which should be printed on create.
Next, run the
datahub actions command as usual:
datahub actions -c custom_action.yaml
If all is well, your Action should now be receiving & printing Events.
(Optional) Step 4: Contributing the Action
If your Action is generally applicable, you can raise a PR to include it in the core Action library
provided by DataHub. All Actions will live under the
datahub_actions/plugin/action directory inside the
Once you've added your new Action there, make sure that you make it discoverable by updating the
setup.py file. This allows you to assign a globally unique name for you Action, so that people can use
it without defining the full module path.
Prerequisites to consideration for inclusion in the core Actions library include
- Testing Define unit tests for your Action
- Deduplication Confirm that no existing Action serves the same purpose, or can be easily extended to serve the same purpose