Skip to content

Creating a Bridge App

The following commands require a session

Check the Quickstart guide on how to login.

Creating a Bridge App

Bridge applications can be created with the same kelvin app create command as Kelvin Apps

kelvin app create my-bridge --app-type=bridge

The apps' creation output:

[kelvin.sdk][2021-10-11 17:05:19][I] Creating new application "my-bridge"                                                                                                                                                                                 
[kelvin.sdk][2021-10-11 17:05:19][I] Retrieving the latest schema version                                                                                                                                                                                 
[kelvin.sdk][2021-10-11 17:05:21][R] Successfully created new application: "my-bridge".                   

Application Structure

The application tree has the following structure:

.                                                                                                                                                                                                                                                         
├── .dockerignore                                                                                                                                                                                                                                              
├── .gitignore                                                                                                                                                                                                                                         
├── app.yaml                                                                                                                                                                                                                                              
├── build/                                                                                                                                                                                                                                                 
├── datatype/                                                                                                                                                                                                                                             
├── my_bridge/                                                                                                                                                                                                                                                
    ├── __init__.py                                                                                                                                                                                                                                       
    └── my_app.py                                                                                                                                                                                                                                         
├── requirements.txt                                                                                                                                                                                                                                               
├── setup.py                                                                                                                                                                                                                         
└── wheels/            

Its source code will be included as:

    """MyBridge Bridge."""

    from __future__ import annotations

    import asyncio
    from asyncio import Queue

    import structlog

    from kelvin.sdk.app.bridge import Bridge, BridgeConfiguration, Configuration, MetricConfiguration
    from kelvin.sdk.datatype import Message

    logger = structlog.get_logger(__name__)


    class MyBridgeBridgeConfiguration(BridgeConfiguration):
        """
        MyBridge Bridge configuration.
        Bridge-specific configuration under "app -> configuration"
        """

    class MyBridgeMetricConfiguration(MetricConfiguration):
        """
        MyBridge metric configuration.
        Bridge-specific configuration for the metrics specified under "app -> metrics_map -> configuration"
        """


    class MyBridgeConfiguration(Configuration[MyBridgeBridgeConfiguration, MyBridgeMetricConfiguration]):
        """MyBridge global configuration."""


    class MyBridgeBridge(Bridge[MyBridgeConfiguration]):
        """MyBridge bridge."""

        async def init(self) -> None:
            """Initialise bridge resources prior to running."""

        async def stop(self) -> None:
            """Stop bridge."""

        async def writer(self, messages: Queue[Message]) -> None:
            """Process outbound messages."""

        async def reader(self, messages: Queue[Message]) -> None:
            """Process inbound messages."""

            # Access the configuration
            configuration = self.config.configuration

            # Access the metrics map
            metrics_map = self.config.metrics_map

            then = 0.0
            period = 10.0  # establish a default period for the loop

            loop = asyncio.get_event_loop()

            while True:
                now = loop.time()
                delay = max(period - (now - then), 0.0)
                await asyncio.sleep(delay)

                then = loop.time()
                for metric in metrics_map:
                    logger.info("processing", metric=metric)

                    # message = Message.parse_obj(...)
                    # await messages.put(message)

Structure in detail

Bridge Applications structure follows the same structure as Kelvin Applications.

Source code

  • The my-bridge directory hosts all source code and package defining files. This is the code that is going to be executed.

Configuration Files

  • The app.yaml is the configuration file where all the app details and configurations will be specified.

Setup files

  • The requirements.txt is a python-only file that allows the specification of python packages as dependencies.
  • The setup.py is a python-only file that allows the apps to also be interpreted as a python project and ultimately packaged as a dependency.