--- title: Plugins keywords: fastai sidebar: home_sidebar nb_path: "nbs/plugin.pluginbase.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
# # hide
# class PluginSettings(Item):
#     def __init__(self, settings_dict):
#         self.settings_dict=settings_dict
        
#     def __getattr__(self, k):
#         if k in self.settings_dict:
#             return self.settings_dict[k]
        
#     @classmethod
#     def from_string(cls, s):
#         objs = json.loads(s)
#         cls(objs)
{% endraw %}

Creating a plugin

The memri pod uses a plugin system to add features to the backend memri backend. Plugins, can import your data (importers), change your data (indexers), or call other serivces. Users can define their own plugins to add new behaviour to their memri app. Let's use the following plugin as an example of how we can start plugins.

Memri plugins need to define at least 2 methods: .run() and .add_to_schema(). .run() defines the logic of the plugin. .add_to_schema() defines the schema for the plugin in the pod. Note that currently, add_to_schema requires all item to have all properties defined that are used in the plugin. In the future, we might replace add_to_schema, to be done automatically, based on a declarative schema defined in the plugin.

{% raw %}
MyPlugin()
MyPlugin (#None)
{% endraw %}
class MyItem(Item):
    properties = Item.properties + ["name", "age"]
    edges = Item.edges
    def __init__(self, name=None, age=None, **kwargs):
        super().__init__(**kwargs)
        self.name = name
        self.age = age

class MyPlugin(PluginBase):
    """"""
    properties = PluginBase.properties
    edges= PluginBase.edges

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def run(self, run, client):
        print("running")
        client.create(MyItem("some person", 20))

    def add_to_schema(self, client):
        client.add_to_schema(MyItem("my name", 10))
{% raw %}
from pymemri.pod.client import PodClient
client = PodClient()
{% endraw %} {% raw %}
assert client.add_to_schema(PluginRun("", "", "", "", ""))
{% endraw %} {% raw %}
 
{% endraw %} {% raw %}
# client.create(plugin)
run = PluginRun("pymemri", "pymemri.plugin.pluginbase", "MyPlugin", "my_config_string")
{% endraw %} {% raw %}
client.create(run)
True
{% endraw %} {% raw %}
run = client.get(run.id)
{% endraw %}

Running your plugin

Plugins can be started using the pymemri run_plugin CLI. To use the CLI, you can either pass your run arguments as parameters, or set them as environment variables. If both are set, the CLI will prefer the passed arguments.

{% raw %}
{% endraw %}

CLI

{% raw %}
{% endraw %} {% raw %}

run_plugin[source]

run_plugin(pod_full_address:Param object at 0x7f99922dd310>=None, plugin_run_id:Param object at 0x7f99922dd390>=None, database_key:Param object at 0x7f9990eba450>=None, owner_key:Param object at 0x7f9990fb6650>=None, container:Param object at 0x7f998f30a110>=None)

{% endraw %} {% raw %}
{% endraw %}

To start a plugin on your local machine, you can use the CLI. This will create a client for you, and run the code defined in <myplugin>.run()

{% raw %}
!run_plugin --pod_full_address=$DEFAULT_POD_ADDRESS --owner_key=$client.owner_key \
            --database_key=$client.database_key --container="pymemri" --plugin_run_id=$run.id
Used arguments passed to `run_plugin()` (ignoring environment)
pod_full_address=http://localhost:3030
plugin_run_id=33A294a95F9dE405DB7548ccf7AA6CA5
owner_key=0976404262528527457330449252813526842102910066371174763451992320
auth_json=None

running
{% endraw %} {% raw %}
# # hide
# class StartPlugin(Item):
#     properties = Item.properties + ["container", "targetItemId"]
#     edges = Item.edges
#     def __init__(self, container=None, targetItemId=None, **kwargs):
#         super().__init__(**kwargs)
#         self.container = container
#         self.targetItemId = targetItemId
{% endraw %} {% raw %}

run_plugin_from_pod[source]

run_plugin_from_pod(pod_full_address:"The pod full address"=None, database_key:"Database key of the pod"=None, owner_key:"Owner key of the pod"=None, container:"Pod container to run frod"=None, plugin_module:"Plugin module"=None, plugin_name:"Plugin class name"=None)

{% endraw %} {% raw %}
{% endraw %}

In production, we start plugins by making an API call to the pod, which in turn creates an environment for the plugin and starts it (currently on docker is supported). We can start this process using the CLI by provding --from_pod==True and providing a --container (the docker container used by the pod). Note that the provided docker container should be installed within the Pod environemnt (e.g. docker build -t pymemri . for this repo) in order to start it.

{% raw %}
!run_plugin_from_pod --pod_full_address=$DEFAULT_POD_ADDRESS --owner_key=$client.owner_key \
                     --database_key=$client.database_key --container="pymemri" \
                     --plugin_module="pymemri.plugin.pluginbase" --plugin_name="MyPlugin"
pod_full_address=http://localhost:3030
owner_key=0976404262528527457330449252813526842102910066371174763451992320

calling the `create` api on http://localhost:3030 to make your Pod start a plugin with id e6FD5D4Fbacf052A250A729EBc9e60db.
*Check the pod log/console for debug output.*
{% endraw %}

{% include note.html content='The data that was created earlier (PluginRun, plugin) should be in the pod in order for this to work' %}