A Node.js microservice designed to process webhook requests from Chrome extensions for YouTube and Instagram platform data. The service uses BullMQ for sequential job processing and PostgreSQL for data storage.
This microservice processes webhook requests through a series of workflows:
- Webhook Reception: Receives requests from Chrome extensions
- Queue Management: Uses BullMQ for sequential processing
- Multi-Workflow Processing: Processes data through 6 interconnected workflows
- Database Operations: Stores and retrieves data from PostgreSQL databases
- External API Integration: Fetches data from YouTube APIs
graph TD
A["Chrome Extension"] --> B["POST /api/chrome-extension"]
B --> C["WORKFLOW 1: Webhook Processing"]
C --> D["Rate Limiting & Validation"]
D --> E["BullMQ Queue"]
E --> F["Sequential Job Processor"]
F --> G{Platform Type?}
G -->|YouTube| H["WORKFLOW 2: Channel Validation"]
G -->|Instagram| I["Instagram User Processing"]
H --> J["Check Channel in Database"]
J --> K{Channel Exists?}
K -->|Yes| L["Return Channel Exists"]
K -->|No| M["Fetch from YouTube API"]
M --> N["π« VEVO Channel Check"]
N --> O{Contains 'VEVO'?}
O -->|Yes| P["βοΈ Skip Processing"]
O -->|No| Q{Channel Valid?}
Q -->|No| R["Mark as Deleted"]
Q -->|Yes| S["Update Channel Data"]
S --> T["WORKFLOW 3: Playlist Generation"]
T --> U["Generate Playlist IDs"]
U --> V["Update Channel with Playlists"]
V --> W["WORKFLOW 4: Video Processing"]
W --> X["Fetch Latest Videos"]
X --> Y{New Videos?}
Y -->|No| Z["Update Channel Status"]
Y -->|Yes| AA["Insert New Videos"]
AA --> BB["WORKFLOW 5: Video Details Processing"]
BB --> CC["Fetch Video Details API"]
CC --> DD["Validate Video Data"]
DD --> EE["Distribute to Workers"]
EE --> FF["WORKFLOW 6.0: Worker Group 1"]
EE --> GG["WORKFLOW 6.1: Worker Group 2"]
EE --> HH["WORKFLOW 6.2: Worker Group 3"]
EE --> II["WORKFLOW 6.3: Worker Group 4"]
EE --> JJ["WORKFLOW 6.4: Worker Group 5"]
FF --> KK["Update Video Details"]
GG --> KK
HH --> KK
II --> KK
JJ --> KK
KK --> LL["Update Video Statistics"]
LL --> MM["Processing Complete"]
I --> NN["Check Instagram User"]
NN --> OO{User Exists?}
OO -->|Yes| PP["Return User Exists"]
OO -->|No| QQ["Create New User"]
- Features
- Tech Stack
- Project Structure
- Installation
- Configuration
- API Endpoints
- Workflow Documentation
- Docker Deployment
- Environment Variables
- Database Schema
- Queue Management
- Logging
- Development
- Testing
- Contributing
- Multi-Platform Support: Handles YouTube and Instagram data processing
- Sequential Processing: Uses BullMQ for reliable job queue management
- π« VEVO Channel Filtering: Automatically filters out VEVO channels from processing
- Scalable Architecture: Microservice-based design with Docker support
- Rate Limiting: Built-in protection against abuse (1000 requests per minute)
- Comprehensive Logging: Structured logging with Winston
- Health Monitoring: Health check endpoints for service monitoring
- Error Handling: Robust error handling with retry mechanisms
- Database Pooling: Efficient PostgreSQL connection management
- Runtime: Node.js 18+
- Framework: Express.js
- Language: TypeScript
- Queue: BullMQ with Redis
- Database: PostgreSQL
- ORM: Native PostgreSQL queries with connection pooling
- Logging: Winston
- Containerization: Docker & Docker Compose
- Process Management: PM2 (via npm scripts)
Chrome-Extension-Micro-Service/
βββ src/
β βββ app.ts # Main application entry point
β βββ config/
β β βββ config.ts # Configuration management
β β βββ database.ts # Database connection setup
β βββ controllers/
β β βββ WebhookController.ts # Main webhook handler
β β βββ WorkerController.ts # Worker job controller
β β βββ YouTubeController.ts # YouTube-specific controller
β βββ interfaces/
β β βββ WebhookPayload.ts # Type definitions
β βββ models/
β β βββ Video.ts # Video data models
β βββ repositories/
β β βββ VideoDetailsRepository.ts # Video details data access
β β βββ VideoStatsRepository.ts # Video stats data access
β βββ routes/
β β βββ Webhook.routes.ts # Webhook routing
β β βββ youtube.routes.ts # YouTube routing
β βββ services/
β β βββ DatabaseService.ts # Database operations
β β βββ LoggerService.ts # Logging service
β β βββ QueueService.ts # BullMQ queue management
β β βββ YouTubeValidatorService.ts # YouTube data validation
β β βββ PlaylistGeneratorService.ts # Playlist ID generation
β β βββ VideoProcessorService.ts # Video processing logic
β β βββ VideoDetailsProcessorService.ts # Video details processing
β β βββ VideoWorkerService.ts # Individual video processing
β βββ types/
β βββ youtube.ts # YouTube type definitions
βββ docs/ # Documentation files
βββ logs/ # Application logs
βββ tests/ # Test files
βββ docker-compose.yml # Docker Compose configuration
βββ Dockerfile # Docker image configuration
βββ package.json # Node.js dependencies
βββ tsconfig.json # TypeScript configuration
- Node.js 18 or higher
- PostgreSQL databases (YouTube and Instagram)
- Redis server
- Docker (optional, for containerized deployment)
-
Clone the repository
git clone <repository-url> cd Chrome-Extension-Micro-Service
-
Install dependencies
npm install
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Build the project
npm run build
-
Start the development server
npm run dev
Create a .env file in the root directory:
# Server Configuration
PORT=4000
NODE_ENV=development
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
# YouTube API Configuration
YOUTUBE_FETCH_URL=https://your-youtube-api.com
YOUTUBE_API_KEY=your_youtube_api_key
# YouTube Database Configuration
YOUTUBE_DB_HOST=your_youtube_db_host
YOUTUBE_DB_PORT=5432
YOUTUBE_DB_USER=your_youtube_db_user
YOUTUBE_DB_PASSWORD=your_youtube_db_password
YOUTUBE_DB_NAME=your_youtube_db_name
YOUTUBE_DB_SSL=require
# Instagram Database Configuration
INSTAGRAM_DB_HOST=your_instagram_db_host
INSTAGRAM_DB_PORT=5432
INSTAGRAM_DB_USER=your_instagram_db_user
INSTAGRAM_DB_PASSWORD=your_instagram_db_password
INSTAGRAM_DB_NAME=your_instagram_db_name
INSTAGRAM_DB_SSL=requireProcesses webhook requests from Chrome extensions.
Request Body:
{
"platform": "youtube|instagram",
"username": "channel_username",
"channel_id": "UC1234567890",
"channel_username": "@channel_name",
"yt_channel_id": "UC1234567890"
}Response:
{
"success": true,
"message": "Request queued for processing",
"requestId": "req_1234567890_abc123",
"status": "queued"
}Health check endpoint for service monitoring.
Response:
{
"status": "ok",
"timestamp": "2024-01-15T10:30:00.000Z"
}Returns current queue statistics and job status.
Response:
{
"success": true,
"data": {
"waiting": 5,
"active": 1,
"completed": 100,
"failed": 2,
"delayed": 0,
"total": 108,
"recentJobs": [...],
"currentJob": {...}
}
}The service processes data through 6 interconnected workflows:
- Receives and validates webhook requests
- Queues requests for sequential processing
- Returns immediate response to client
- Validates YouTube channel existence
- Fetches channel data from YouTube API
- Updates channel details in database
- Generates playlist IDs for different content types
- Updates channel with generated playlist IDs
- Fetches latest videos from playlists
- Identifies new videos for processing
- Inserts new video records
- Fetches detailed video information
- Distributes videos across worker groups
- Coordinates parallel video processing
- Updates individual video details
- Processes video statistics
- Handles video-specific data operations
For detailed workflow diagrams, see the docs/workflows/ directory.
-
Start all services
docker-compose up -d
-
View logs
docker-compose logs -f
-
Stop services
docker-compose down
-
Build the image
docker build -t chrome-extension-service . -
Run the container
docker run -d \ --name webhook-service \ -p 4000:4000 \ --env-file .env \ chrome-extension-service
- channel_details: Channel information and metadata
- channel_stats: Channel statistics and metrics
- video_details: Video information and metadata
- video_stats: Video statistics and engagement data
- ig_users: Instagram user profiles and data
The service uses BullMQ for reliable job processing:
- Sequential Processing: Jobs are processed one at a time
- Retry Logic: Failed jobs are automatically retried with exponential backoff
- Job Persistence: Jobs are persisted in Redis for reliability
- Progress Tracking: Real-time job progress and status monitoring
{
concurrency: 1, // Sequential processing
attempts: 3, // Retry failed jobs 3 times
backoff: {
type: 'exponential',
delay: 2000 // Start with 2-second delay
},
removeOnComplete: 100, // Keep last 100 completed jobs
removeOnFail: 50 // Keep last 50 failed jobs
}The service uses Winston for structured logging:
- Error Logs: Stored in
logs/error.log - Workflow Logs: Stored in
logs/workflow.log - Console Output: Colored console logging for development
error: Error conditions and exceptionswarn: Warning conditionsinfo: General information and workflow stepsdebug: Detailed debugging information
npm run dev # Start development server with nodemon
npm run build # Build TypeScript to JavaScript
npm start # Start production server
npm test # Run test suite
npm run lint # Run ESLint code analysis- Make changes to TypeScript files in
src/ - The development server will automatically restart on changes
- Test your changes using the provided endpoints
- Run tests to ensure functionality
- Build and test the production version
Run the test suite:
npm testThe project uses Jest for testing with TypeScript support.
- Service health:
GET /health - Queue status:
GET /api/queue/status - Docker health checks included in container configuration
- Structured JSON logging for production
- Colored console logs for development
- Separate log files for errors and workflows
- Request tracking with unique request IDs
Monitor these key metrics:
- Queue processing rate
- Job success/failure rates
- Database connection health
- API response times
- Memory and CPU usage
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Use TypeScript for all new code
- Follow existing naming conventions
- Add appropriate error handling
- Include logging for important operations
- Write tests for new functionality
For support and questions:
- Check the documentation in the
docs/directory - Review the workflow diagrams for understanding the data flow
- Check the logs for detailed error information
- Ensure all environment variables are properly configured
-
Database Connection Errors
- Verify database credentials in
.env - Check database server accessibility
- Ensure SSL configuration is correct
- Verify database credentials in
-
Redis Connection Issues
- Verify Redis server is running
- Check Redis connection parameters
- Ensure Redis is accessible from the application
-
Queue Processing Issues
- Check Redis connectivity
- Monitor queue status endpoint
- Review worker logs for processing errors
-
API Integration Problems
- Verify external API endpoints are accessible
- Check API key validity
- Monitor rate limiting and quotas