SourcesHandler Microservice
Overview
The SourcesHandler microservice is responsible for processing and managing source-related data within the Publisher ecosystem. It handles campaign sources, product sources, publisher names, and experience types by processing messages from various collections and maintaining synchronized data across multiple storage systems.
Business Purpose
This service acts as a central hub for managing source data integrity and synchronization across the Publisher platform. It processes events related to campaigns, vendors, and configuration changes to maintain up-to-date source information in both Cosmos DB and S3 storage systems.
Architecture
Service Type
- Deployment: Kubernetes containerized microservice
- Trigger: HTTP-triggered Azure Function
- Runtime: Node.js
Key Components
- ProductSourcesHandler: Manages product-related source data
- CampaignSourcesHandler: Handles campaign source information
- PublisherNamesHandler: Processes publisher name data with override capabilities
- ExperienceTypesHandler: Manages experience type configurations
Data Flow
graph TD
A[HTTP Request] --> B[Main Handler]
B --> C[Message Processing]
C --> D[ProductSourcesHandler]
C --> E[CampaignSourcesHandler]
C --> F[PublisherNamesHandler]
C --> G[ExperienceTypesHandler]
D --> H[Cosmos DB - Vendors]
D --> I[Cosmos DB - Campaigns]
D --> J[S3 Storage]
E --> H
E --> I
E --> J
F --> H
F --> I
F --> J
G --> J
H --> K[System Events]
I --> K
J --> K
Dependencies
External Services
- Azure Cosmos DB: Publisher database for Vendors and Campaigns collections
- AWS S3: Storage for source data files
- Azure Event Hub: System events publishing
NPM Dependencies
@azure/cosmos: Cosmos DB clientaws-sdk: AWS services integrationasync: Asynchronous flow controllodash: Utility functionstitleize: String formattingidgen: ID generationfast-json-patch: JSON patching operations
Configuration
Environment-Specific Configs
config.js: Development configurationconfig.int.js: Integration environmentconfig.prod.js: Production environment
Key Configuration Parameters
- LogLevel: Logging verbosity (default: 5)
- S3 Bucket:
dataetlhandlerwith key prefixappcache/ - maxRetries: Maximum retry attempts (default: 5)
- Event Hub: Consumer group
sourceshandlerfordocumenteventhub
API Endpoints
POST /
Processes source-related messages in batch format.
Request Body: Array of message objects
[
{
"collection": "Campaigns|Vendors|configuration",
"action": "CREATE|UPDATE|DELETE",
"record": { /* record data */ },
"oldRecord": { /* previous record state */ }
}
]
Response: - Status: 200 OK - Body: Array of failed events (if any)
Message Processing Logic
Supported Collections
- Campaigns: Campaign-related source updates
- Vendors: Publisher/vendor source information
- configuration: Experience type configurations (id: 'etype')
Processing Rules
- Ignores messages without valid collection or record data
- Skips DELETE actions for most handlers (except ProductSourcesHandler)
- Filters out campaigns with status "review"
- Handles vendor ID overrides using sourceGroupVendorId
Special Features
- Source Group Override Names: Predefined mappings for specific source groups
- Retry Logic: Configurable retry attempts with detailed logging
- Parallel Processing: Multiple handlers process messages concurrently
- Error Handling: Failed events are collected and returned for retry
Data Storage
Cosmos DB Collections
- Vendors: Publisher/vendor information
- Campaigns: Campaign data and source associations
S3 Storage Structure
- Bucket:
dataetlhandler - Prefix:
appcache/ - Files: Various source data files including experience types
Monitoring and Logging
Application Insights
- Connection string configured for telemetry
- Custom logging levels for different environments
- Error tracking and performance monitoring
Event Publishing
- System events published to
publishersystemeventsEvent Hub - Failed event tracking and alerting
Error Handling
Retry Mechanism
- Maximum retry attempts: 5 (configurable)
- Retry attempt tracking via HTTP headers
- Progressive logging levels based on retry count
Failure Scenarios
- Database connection failures
- S3 storage access issues
- Invalid message formats
- Processing timeouts
Development
Local Setup
- Install dependencies:
npm install - Configure environment variables
- Set up Azure Cosmos DB and AWS S3 credentials
- Run locally using Azure Functions Core Tools
Testing
- Test file:
test.js - Manual testing capabilities for individual components
Build Process
- Webpack configuration for bundling
- Terser plugin for code minification
- Copy plugin for static assets
Deployment
Kubernetes Configuration
- Containerized deployment
- Health check endpoints:
/liveand/ready - Environment-specific function.json configurations
Environment Variables
- Azure Cosmos DB connection strings
- AWS S3 credentials
- Application Insights instrumentation key
- Event Hub connection strings
Related Services
This service integrates with: - DocumentEventHandler: Source of document change events - CacheSync: Downstream cache synchronization - ReportGenerator: Source data for reporting - Various Handler Services: Consumers of source data
Troubleshooting
Common Issues
- High Retry Rates: Check Cosmos DB and S3 connectivity
- Missing Source Data: Verify message format and collection names
- Performance Issues: Monitor batch sizes and processing times
- Configuration Errors: Validate environment-specific settings
Monitoring Points
- Failed event counts
- Processing latency
- Retry attempt frequencies
- Storage operation success rates