Services
Core services provided by the workflow module.
OrchestratorService
The main service responsible for orchestrating workflow execution and state transitions.
Import
import { OrchestratorService } from 'nestjs-serverless-workflow/core';
Methods
transit(event)
Processes a workflow event and executes the appropriate state transition.
Signature
async transit(params: IWorkflowEvent): Promise<void>
Parameters
params: Workflow event object:topic: Event topic/nameurn: Unique resource name (entity identifier)payload?: Optional event payloadattempt: Retry attempt number
Returns
Promise that resolves when the transition is complete.
Example
@Injectable()
export class OrderService {
constructor(private orchestrator: OrchestratorService) {}
async processOrderEvent(orderId: string, event: string, data: any) {
await this.orchestrator.transit({
topic: event,
urn: orderId,
payload: data,
attempt: 0,
});
}
}
Behavior
- Loads the entity using the URN
- Finds the appropriate transition based on current state and event
- Validates transition conditions
- Executes the event handler
- Updates entity state
- Handles retries if configured
- Processes automatic transitions if applicable
Error Handling
- Throws
BadRequestExceptionif no workflow is found for the event - Throws
BadRequestExceptionif no valid transition is found - Updates entity to failed state on error
- Respects
UnretriableExceptionto prevent retries
Lifecycle
The service initializes routes on module initialization (onModuleInit):
- Discovers all workflow classes
- Extracts workflow definitions and handlers
- Resolves entity services and broker publishers
- Builds route map for event handling
StateRouterHelperFactory
Factory for creating router helpers that assist with state routing logic.
Import
import { StateRouterHelperFactory } from 'nestjs-serverless-workflow/core';
Methods
create(event, entityService, definition, logger)
Creates a new router helper instance.
Signature
create(
event: string,
entityService: IWorkflowEntity,
definition: IWorkflowDefinition,
logger: Logger
): StateRouterHelper
RouterService
Service responsible for routing workflow events to appropriate handlers.
Note
This service is used internally by OrchestratorService and typically doesn't need to be used directly.
SagaService
Service for managing distributed transactions using the Saga pattern.
Note
Saga support is planned for future releases. See the workflow definition interface for saga configuration options.
Usage Example
import { Module, Injectable } from '@nestjs/common';
import { WorkflowModule, OrchestratorService } from 'nestjs-serverless-workflow/core';
import { IWorkflowEvent } from 'nestjs-serverless-workflow/event-bus';
@Injectable()
export class WorkflowProcessor {
constructor(private orchestrator: OrchestratorService) {}
async processEvent(event: IWorkflowEvent) {
try {
await this.orchestrator.transit(event);
console.log('Event processed successfully');
} catch (error) {
console.error('Failed to process event:', error);
throw error;
}
}
}
@Module({
imports: [
WorkflowModule.register({
entities: [],
workflows: [],
brokers: [],
}),
],
providers: [WorkflowProcessor],
})
export class AppModule {}