OpenFactoryApp#

class openfactory.apps.ofaapp.OpenFactoryApp(app_uuid, ksqlClient, bootstrap_servers=None, loglevel='INFO')[source]#

Bases: Asset

Generic OpenFactory application.

Inherits from Asset and extends it to represent an OpenFactory application with standard metadata, logging, and lifecycle management.

APPLICATION_VERSION#

Class attribute. Version string from APPLICATION_VERSION env var or ‘latest’.

Type:

str

APPLICATION_MANUFACTURER#

Class attribute. Manufacturer from APPLICATION_MANUFACTURER or ‘OpenFactory’.

Type:

str

APPLICATION_LICENSE#

Class attribute. License string from APPLICATION_LICENSE or ‘BSD-3-Clause license’.

Type:

str

logger#

Instance attribute. Prefixed logger instance configured with the app UUID.

Type:

logging.Logger

Example usage:
import time
from openfactory.apps import OpenFactoryApp
from openfactory.kafka import KSQLDBClient

class DemoApp(OpenFactoryApp):

    def main_loop(self):
        # For actual use case, add here your logic of the app
        print("I don't do anything useful in this example.")
        counter = 1
        while True:
            print(counter)
            counter += 1
            time.sleep(2)

    def app_event_loop_stopped(self):
        # Not absolutely required as it is already done by the `KSQLDBClient` class
        self.ksql.close()

app = DemoApp(
    app_uuid='DEMO-APP',
    ksqlClient=KSQLDBClient("http://localhost:8088"),
    bootstrap_servers="localhost:9092"
)
app.run()
__init__(app_uuid, ksqlClient, bootstrap_servers=None, loglevel='INFO')[source]#

Initializes the OpenFactory application.

Fetches the application’s UUID from either the argument or the environment variable APP_UUID. It also sets up attributes for version, manufacturer, and license, and registers signal handlers for termination signals.

Parameters:
  • app_uuid (str) – The UUID of the application (overrides the environment variable APP_UUID if provided).

  • ksqlClient (KSQLDBClient) – The KSQL client instance.

  • bootstrap_servers (str) – Kafka bootstrap server URL, default value is read from config.KAFKA_BROKER.

  • loglevel (str) – Logging level for the app (e.g., ‘INFO’, ‘DEBUG’). Defaults to ‘INFO’.

app_event_loop_stopped()[source]#

Called when main loop is stopped.

Return type:

None

main_loop()[source]#

Main loop of the OpenFactory App.

This method must be implemented by child classes to define the main application loop behavior. It will be responsible for managing the application’s lifecycle, handling events, and maintaining any necessary state while the application is running.

Return type:

None

Raises:

NotImplementedError – If this method is not implemented by a subclass.

run()[source]#

Runs the OpenFactory app.

This method initializes the app by displaying a welcome banner, adding an availability attribute, and then starts the main application loop by calling main_loop. If an exception occurs during the execution of the main loop, the error is caught, and the app is gracefully stopped.

The following steps are performed:

  1. Display the welcome banner.

  2. Add the avail attribute with ‘AVAILABLE’ value.

  3. Start the main loop.

  4. Catch any exceptions that occur and stop the app gracefully.

Raises:

Exception – If any exception occurs during the execution of the main loop, it is caught and logged, and the app is stopped.

Return type:

None

signal_handler(signum, frame)[source]#

Handles SIGINT and SIGTERM signals, gracefully stopping the application.

This method listens for termination signals, deregisters the asset from the system, and then stops the application’s event loop. It is typically used to handle clean shutdowns when the app receives signals like SIGINT or SIGTERM.

Return type:

None

Parameters:
  • signum (int) – The signal number that was received (e.g., SIGINT, SIGTERM).

  • frame (Optional) – The current stack frame when the signal was received.

welcome_banner()[source]#

Welcome banner printed to stdout.

Can be redefined by children

Return type:

None