Application
This file defines the Application
class, which serves as the main entry point for applications built with the @sodacore/core
library. It is responsible for initializing, starting, stopping, and managing all core modules and plugins, as well as handling graceful shutdowns.
Features
- Core Module Management: Initializes and manages all core modules (autowire, events, runner, threads, workers, services, scripts).
- Plugin Support: Allows plugins to be registered and initialized.
- Graceful Shutdown: Handles process signals to ensure a clean shutdown.
- Global Registry: Registers core modules, plugins, and providers in a global registry for easy access.
- Lifecycle Hooks: Provides hooks for initialization, starting, and stopping the application and its modules.
Usage
import Application from './module/application';
const app = new Application(config);
await app.start();
// Register a plugin
await app.use(myPlugin);
// Register a module
await app.register(MyModule);
// Stop the application (can also be triggered by SIGINT or beforeExit)
await app.stop();
API
Constructor
constructor(
protected config: IConfig,
)
- config: The configuration object for the application.
Methods
public async start(): Promise<void>
Initializes and starts all core modules and plugins. Registers built-in providers and dispatches lifecycle events.
public async stop(): Promise<void>
Stops all core modules and plugins, dispatches lifecycle events, and logs shutdown messages.
public async use(plugin: IPlugin): Promise<void>
Registers and installs a plugin, initializing its modules.
public async register(module: any): Promise<void>
Registers a class (not an instance) with the application via the autowire system.
Private Methods
private async init(): Promise<void>
Initializes all core modules and plugins. Called automatically by start()
.
private async handleStop(): Promise<void>
Handles graceful shutdown on process signals. Sets a shutdown flag, prevents immediate process exit, and calls stop()
before exiting.
Properties
- autowire: Instance of the autowire module.
- events: Instance of the events module.
- runner: Instance of the runner module.
- threads: Instance of the threads module.
- workers: Instance of the workers module.
- services: Instance of the services module.
- scripts: Instance of the scripts module.
- logger: Logger instance.
- plugins: Array of registered plugins.
Notes
- The application listens for
SIGINT
andbeforeExit
signals to trigger a graceful shutdown. - Core modules and providers are registered in the global
Registry
for easy access throughout the application. - Plugins must implement the
IPlugin
interface and can define their owninit
method. - The
handleStop
method prevents the process from exiting immediately by creating an interval, ensuring all shutdown logic completes before exit.