- TypeScript 99.3%
- JavaScript 0.5%
- Dockerfile 0.2%
chore(deps): update dependency @rollup/rollup-linux-arm64-musl to v4.54.0 See merge request logius/nldoc/worker/stations!513 |
||
|---|---|---|
| .husky | ||
| .vscode | ||
| __mocks__ | ||
| __test__ | ||
| integration | ||
| rabbitmq/etc | ||
| src | ||
| .editorconfig | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| .npmrc | ||
| .nvmrc | ||
| .prettierignore | ||
| CHANGELOG.md | ||
| CODEOWNERS | ||
| commitlint.config.cts | ||
| docker-compose.yaml | ||
| Dockerfile | ||
| esbuild.config.ts | ||
| eslint.config.mjs | ||
| LICENSE | ||
| package-lock.json | ||
| package.json | ||
| prettier.config.mjs | ||
| README.md | ||
| renovate.json | ||
| tsconfig.dist.json | ||
| tsconfig.json | ||
| vitest.config.ts | ||
NLdoc Stations
A TypeScript framework for building document processing stations within the NLdoc pipeline. Stations are modular processing units that handle documents, pages, and attributes through AMQP message queuing, enabling scalable and orchestrated document processing workflows.
Overview
NLdoc Stations provides the core infrastructure for creating processing stations that:
- Process documents through configurable input/output pipelines
- Coordinate with workers for delegated processing tasks
- Handle message queuing via RabbitMQ/AMQP
- Enable distributed document processing workflows
Features
- Modular Architecture: Build custom processing stations with configurable input/output types
- Message Queue Integration: RabbitMQ/AMQP support for reliable message processing
- Worker Coordination: Delegate processing tasks to external workers
- State Management: Track processing state across distributed components
- Docker Ready: Containerized deployment with Docker Compose support
- TypeScript: Full type safety and modern ES modules
Station Types
The framework supports various station configurations for different processing stages:
- Document Stations: Process entire documents (mimetype detection, metadata extraction)
- Page Stations: Handle individual document pages (content extraction, region detection)
- Aggregation Stations: Aggregate child-item processing results (e.g., wait for all pages before processing)
- Attribute Stations: Manage document and page attributes
- Output Stations: Generate final processing outputs
Station types are configured via the STATION_CONFIGURATION environment variable. See StationConfiguration for the complete schema.
Getting Started
Local Installation
-
Clone the repository:
git clone https://gitlab.com/logius/nldoc/worker/stations.git cd stations -
Install dependencies:
npm install -
Build the project:
npm run build
Running with Docker Compose (Recommended)
-
Ensure Docker and Docker Compose are installed (Installation Guide)
-
Start the services:
docker compose up -
Access the management interfaces:
- RabbitMQ Console:
http://localhost:15672/(guest/guest) - PostgreSQL:
localhost:5432(postgres/postgres)
- RabbitMQ Console:
-
Run integration tests:
npm run test -
Stop services:
docker compose down
Running Directly
To run a station directly:
npm run start:station
Or in development mode:
npm run dev
Configuration
Stations are configured via environment variables and JSON configuration. For complete configuration options, see the Environment Configuration Schema.
Key environment variables include:
# Station Configuration (JSON)
STATION_CONFIGURATION='{
"input": {"type": "document"},
"workerResult": "fileWorkerResult",
"output": {"type": "output", "attribute": {"type": "file", "name": "source"}}
}'
# General Configuration
ENVIRONMENT=development
npm_package_version=9.5.1
# AMQP Configuration
AMQP_HOST=localhost
AMQP_PORT=5672
AMQP_USER=guest
AMQP_PASS=guest
## Maximum size in bytes for result stream segments before splitting
AMQP_RESULTS_STREAM_MAX_SEGMENT_SIZE=1000000
# PostgreSQL Configuration
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=nldoc
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
## PostgreSQL schema (namespace for tables). Use to isolate data between environments or tenants
POSTGRES_SCHEMA=public
## SSL/TLS connection mode: (default=verify-full)
## - disable: Disable SSL connection
## - require: Require SSL connection
## - allow: Use SSL if available
## - prefer: Prefer SSL but allow non-SSL
## - verify-full: Require SSL with full certificate verification
POSTGRES_SSL=verify-full
## Path to CA certificate file for validating self-signed certificates.
## Only used when POSTGRES_SSL=verify-full.
POSTGRES_SSL_CA_PATH=/path/to/server-ca.crt
## Maximum number of connections in the pool
POSTGRES_POOL_SIZE=20
Architecture
AMQP Message Queue
The AMQP module provides a centralized manager-based architecture for message queue operations:
- AmqpManager: Core orchestrator managing connections, channels, exchanges, queues, and message flow
- MessageProcessor: Handles incoming message processing with validation and error handling
- MessageValidator: Validates message structure and content before processing
- MessageRetryHandler: Manages message requeuing and retry logic with exponential backoff
- ExchangeConfigRegistry: Maps station input/output types to exchange configurations
- ConnectionConfig: Builds connection URLs from environment variables
The manager establishes exchanges for each document type (documents, PDF, images, etc.), binds queues based on topic patterns, and coordinates worker communication through dedicated job/result exchanges.
Worker Coordination
Stations can delegate processing tasks to external workers through a worker management system:
- WorkerRegistry: Tracks available worker instances and their capabilities
- WorkerManager: Coordinates job distribution and worker health monitoring
- JobPublisher: Publishes jobs to worker exchanges
Workers communicate via dedicated AMQP exchanges for jobs, results, and health status, enabling horizontal scaling of processing tasks.
State Management
The state management layer handles document and page processing state:
- StateManager: Manages source object state, attributes, and worker job tracking
- SourceObjectManager: Provides storage abstraction
- LockMonitor: Implements distributed locking using proper-lockfile for concurrent processing safety
State is persisted to ensure fault tolerance and processing resumability across station restarts.
Development
Available Scripts
npm run dev- Start development server with hot reloadnpm run build- Build for productionnpm run test- Run tests with coveragenpm run lint- Run ESLintnpm run format- Format code with Prettiernpm run check- Type check without emitting files
Testing and Development
-
Install dependencies and run tests:
npm install npm run test -
Run integration tests (requires Docker Compose services):
docker compose up -d postgresql rabbitmq npx tsx integration/test-worker-attribute.ts
Deployment
Docker Images
Pre-built Docker images are available in our container registry.
Run the latest version:
docker run --rm registry.gitlab.com/logius/nldoc/worker/stations:latest
Documentation
An Example Architecture
flowchart TB
Document{Document} ===> station-document-mimetype[station-document-mimetype]
station-document-mimetype ===> |mimetype| Document
Document ===> |mimetype| station-document-metadata[station-document-metadata]
station-document-metadata ===> Document
Document ===> |mimetype, pageCount| station-document-pages[station-document-pages]
station-document-pages ===> Page{Page}
Page ===> |interpretedContent, regions| station-page-content[station-page-content]
station-page-content ===> |content| Page
Page ===> |content| station-pdf-aggregation-documentcontent[station-pdf-aggregation-documentcontent]
station-pdf-aggregation-documentcontent ===> |content| Document
Document ===> |content| station-document-output[station-document-output]
station-document-output ===> Output{Output}
Note: The station-pdf-aggregation-documentcontent station uses the aggregation pattern (type: 'pdf' with aggregation config) to wait for all page content before processing.
License
This project is licensed under the European Union Public License 1.2 - see LICENSE for details.