Skip to main content
Custom modules in the iii Engine allow developers to extend the core functionality of the system. A module acts as a container for logic that can register functions, triggers, and integrate with external systems.

Introduction

Modules are dynamically loaded and configured, often utilizing an Adapter pattern to allow for swappable backend implementations (e.g., swapping an in-memory event bus for a Redis-backed one). The engine provides a trait-based system where modules implement the CoreModule trait for lifecycle management and the ConfigurableModule trait for handling configuration and adapter injection.

Module Architecture

The module system is built around two primary traits: CoreModule and ConfigurableModule.

Core Traits

Lifecycle Flow

The following diagram illustrates the lifecycle of a module from creation to initialization.

Implementing a Configurable Module

Developing a custom module typically involves defining an adapter interface, implementing specific adapters, and then wrapping them in a module structure.

Step 1: Define the Adapter Trait

Define an async_trait that specifies the behavior your module’s backend must implement. This allows users to switch implementations via configuration.
Why async_trait? Rust’s async traits require this macro to handle the complexity of async function pointers.

Step 2: Implement Adapter Registration

To make adapters discoverable by the configuration system, you must define a registration struct and use the inventory crate.
Purpose: This registration system allows the engine to discover and instantiate adapters dynamically based on configuration.

Step 3: Create Adapter Factories

Define factory functions that instantiate your specific adapter implementations (e.g., InMemory or Logging).

Step 4: Implement Adapter Logic

Create the actual adapter implementations.
Simple in-memory implementation for development and testing.
Wrapper adapter that logs all events while delegating to another adapter.

Step 5: Implement the Module Logic

The module struct holds the Engine reference and the injected Adapter.

Registering Functions

Modules expose functionality to the engine (and thus to workers) by registering functions. This is typically done in the initialize method or register_functions.

Registration Request Structure

When registering a function, you must provide a RegisterFunctionRequest.
When using the #[service] macro with #[function] attributes, request_format and response_format are auto-generated as standard JSON Schema from your Rust types (via schemars). Your input/output types must derive JsonSchema. Manual schema specification is only needed for custom module registration.

Example Registration

Handling Function Invocations

To handle invocations, the module (or a specific handler struct) must implement the FunctionHandler trait.

Registering Triggers

Modules can also act as sources of events by registering TriggerTypes. This allows the engine to route external events (like Cron ticks or HTTP requests) to specific functions.

Trigger Architecture

Implementation

To support triggers, a module implements TriggerRegistrator.
Then, register the trigger type during initialization:

Configuration

Modules are configured via iii-config.yaml or JSON passed during initialization. The ConfigurableModule trait maps this configuration to a Rust struct.

Configuration Struct

Usage in Config File

Nested Adapters: The logging adapter wraps the in-memory adapter, creating a decorator pattern for cross-cutting concerns.

Complete Example

Here’s a complete custom module implementation:

examples/custom_event_module.rs

iii-config.yaml

Best Practices

Always use adapters for external integrations to allow swapping implementations.
Handle cleanup in the module’s drop implementation or provide shutdown hooks.
Always define request and response formats for functions to enable validation and documentation.
Use the tracing crate for structured logging with context.

Next Steps

Core Modules

Explore built-in core modules

Adapters

Learn more about the adapter pattern