Plugin interface for extending Stratix applications.

Plugins follow a lifecycle: initialize → start → stop

class DatabasePlugin implements Plugin {
readonly metadata: PluginMetadata = {
name: 'database',
description: 'PostgreSQL database plugin',
dependencies: ['logger']
};

private database?: Database;

async initialize(context: PluginContext): Promise<void> {
const config = context.getConfig<DatabaseConfig>();
this.database = new Database(config);

context.container.register('database', () => this.database, {
lifetime: ServiceLifetime.SINGLETON
});
}

async start(): Promise<void> {
await this.database?.connect();
}

async stop(): Promise<void> {
await this.database?.disconnect();
}

async healthCheck(): Promise<HealthCheckResult> {
return await this.database?.ping();
}
}
interface Plugin {
    metadata: PluginMetadata;
    initialize(context: PluginContext): Promise<void>;
    start(): Promise<void>;
    stop(): Promise<void>;
    healthCheck(): Promise<HealthCheckResult>;
}

Methods

  • Initializes the plugin.

    Called during application startup, after all dependencies are initialized. Register services in the container during this phase.

    Parameters

    Returns Promise<void>

    async initialize(context: PluginContext): Promise<void> {
    const database = new Database(context.getConfig());
    context.container.register('database', () => database);
    }
  • Starts the plugin.

    Called after all plugins are initialized, before the application starts. Connect to external resources during this phase.

    Returns Promise<void>

    async start(): Promise<void> {
    await this.database.connect();
    console.log('Database connected');
    }
  • Stops the plugin.

    Called during application shutdown, in reverse dependency order. Close connections and clean up resources during this phase.

    Returns Promise<void>

    async stop(): Promise<void> {
    await this.database.disconnect();
    console.log('Database disconnected');
    }
  • Performs a health check for the plugin.

    Returns Promise<HealthCheckResult>

    The health check result

    async healthCheck(): Promise<HealthCheckResult> {
    try {
    await this.database.ping();
    return { status: HealthStatus.UP };
    } catch (error) {
    return { status: HealthStatus.DOWN, message: error.message };
    }
    }

Properties

metadata: PluginMetadata

Plugin metadata (name, dependencies).