Background service that synchronizes data between the database and Elasticsearch to maintain search index consistency.
The Sync Worker service is responsible for:
This service is part of the Thmanyah microservices architecture:
Example :Queue (Redis/BullMQ) โ Sync Worker โ Elasticsearch
The Sync Worker ensures that changes made in the CMS API are reflected in the search index used by the Discovery API.
# Install dependencies (from root)
pnpm install
# Start in development mode
pnpm --filter sync-worker dev
# Or from the app directory
cd apps/sync-worker
pnpm dev
Required environment variables (see root .env.example
):
# Database
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=password
DATABASE_NAME=thmanyah
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
# Elasticsearch
ELASTICSEARCH_URL=http://localhost:9200
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=changeme
ELASTICSEARCH_INDEX_NAME=programs
# Node environment
NODE_ENV=development
# Unit tests
pnpm --filter sync-worker test
# E2E tests
pnpm --filter sync-worker test:e2e
# Test coverage
pnpm --filter sync-worker test:cov
# Watch mode
pnpm --filter sync-worker test:watch
# Development
pnpm dev # Start in development mode
pnpm start # Start in production mode
pnpm start:dev # Start with watch mode
pnpm start:debug # Start in debug mode
pnpm start:prod # Start in production mode
# Testing
pnpm test # Run unit tests
pnpm test:watch # Run tests in watch mode
pnpm test:cov # Run tests with coverage
pnpm test:debug # Run tests in debug mode
pnpm test:e2e # Run e2e tests
# Building
pnpm build # Build the application
pnpm build:watch # Build in watch mode
# Linting
pnpm lint # Run ESLint
pnpm lint:fix # Fix ESLint issues
apps/sync-worker/
โโโ src/
โ โโโ app.module.ts # Main application module
โ โโโ main.ts # Application entry point
โ โโโ common/ # Shared utilities
โ โโโ sync-worker/ # Sync worker logic
โ โโโ sync-worker.service.ts
โ โโโ sync-worker.service.spec.ts
โโโ test/ # E2E tests
โโโ package.json
The service manages an Elasticsearch index with the following mapping:
Example :{
"mappings": {
"properties": {
"id": { "type": "keyword" },
"title": {
"type": "text",
"analyzer": "standard",
"fields": { "keyword": { "type": "keyword" } }
},
"description": { "type": "text", "analyzer": "standard" },
"type": { "type": "keyword" },
"language": { "type": "keyword" },
"tags": { "type": "keyword" },
"createdAt": { "type": "date" },
"updatedAt": { "type": "date" }
}
},
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
ELASTICSEARCH_URL=http://localhost:9200
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=changeme
ELASTICSEARCH_INDEX_NAME=programs
# Build image
docker build --build-arg TARGET_APP=sync-worker -t thmanyah/sync-worker .
# Run container
docker run --env-file .env thmanyah/sync-worker
# Build for production
pnpm --filter sync-worker build
# Start production server
pnpm --filter sync-worker start:prod
Jobs not being processed
Elasticsearch errors
Performance issues
The service provides detailed logging for:
This project is part of the Thmanyah backend system and is licensed under the MIT License.