Services Module
This file defines the Services
class, which is responsible for managing and running all services decorated with the @Service
decorator in the @sodacore/core
library. It handles the initialization, starting, and stopping of all registered services.
Features
- Service Discovery: Finds all registered services in the global
Registry
that are decorated as services. - Lifecycle Management: Provides methods to initialize, start, and stop all services.
- Logging: Logs the status and count of services during each lifecycle phase.
Usage
typescript
import Services from './module/services';
const services = new Services(config);
await services.init();
await services.start();
// ... later
await services.stop();
API
Properties
- services:
BaseService[]
List of all discovered and managed service instances.
Methods
async init(): Promise<void>
Initializes the services module:
- Discovers all registered services by filtering modules in the
Registry
with thetype
metadata set to'service'
. - Calls the
init()
method on each service instance. - Logs the number of initialized services.
async start(): Promise<void>
Starts all services by calling their start()
method and logs the number of started services.
async stop(): Promise<void>
Stops all services by calling their stop()
method and logs the number of stopped services.
How It Works
Service Discovery:
- Filters all modules in the
Registry
to find those with thetype
metadata set to'service'
.
- Filters all modules in the
Lifecycle:
init()
initializes all discovered services.start()
starts all initialized services.stop()
stops all running services.
Logging:
- Each lifecycle method logs the action and the number of affected services.
Notes
- Services must extend
BaseService
and be decorated with the@Service
decorator to be managed by this module. - The module ensures all services are properly initialized, started, and stopped in a consistent manner.