Skip to main content

Redis & BullMQ Setup Guide

βœ… What's Already Done​

1. Redis Installed βœ…β€‹

  • βœ… Redis service added to docker-compose.yml
  • βœ… Redis container running on localhost:6379
  • βœ… Redis volume created for persistence
  • βœ… Health check configured
  • βœ… Connection tested successfully (PONG received)

2. Database Migration Complete βœ…β€‹

  • βœ… All template tables created:
    • document_template (with all enhanced fields)
    • location_template_config
    • printer_profile
    • template_variable
    • template_audit (new!)
  • βœ… All indexes created
  • βœ… Database types generated

3. BullMQ Installed βœ…β€‹

  • βœ… @nestjs/bull and bullmq packages installed

πŸš€ Next Steps: Configure BullMQ​

Step 1: Add Redis Configuration to Backend​

Add these environment variables to your backend .env file:

# Redis Configuration for BullMQ
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

Step 2: Update PdfModule for BullMQ​

Update /Users/luisrangel/devLR/rpa/flowpos-workspace/apps/backend/src/pdf/pdf.module.ts:

import { BullModule } from '@nestjs/bull';

@Module({
imports: [
BullModule.forRoot({
redis: {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379', 10),
password: process.env.REDIS_PASSWORD || undefined,
},
}),
BullModule.registerQueue({
name: 'preview-generation',
defaultJobOptions: {
attempts: 3,
backoff: {
type: 'exponential',
delay: 2000,
},
removeOnComplete: 100,
removeOnFail: 500,
},
}),
],
// ... rest of module config
})

Step 3: Create Preview Generation Processor​

Create file: apps/backend/src/pdf/infrastructure/queues/preview-generation.processor.ts

import { Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';
import { Injectable, Logger } from '@nestjs/common';
import { TemplateRepository } from '@/pdf/infrastructure/repositories/template.repository';

@Processor('preview-generation')
@Injectable()
export class PreviewGenerationProcessor {
private readonly logger = new Logger(PreviewGenerationProcessor.name);

constructor(
private readonly templateRepository: TemplateRepository,
) {}

@Process('generate-preview')
async handlePreviewGeneration(job: Job<{ templateId: string; documentType: string }>) {
const { templateId, documentType } = job.data;

try {
this.logger.log(`Starting preview generation for template ${templateId}`);

// Update status to processing
await this.templateRepository.update(templateId, {
previewStatus: 'processing',
});

// TODO: Implement actual preview generation
// 1. Get template
// 2. Generate sample data
// 3. Compile template
// 4. Generate PDF
// 5. Take screenshot
// 6. Upload to storage
// 7. Update template with preview URL

// For now, just mark as completed
await this.templateRepository.update(templateId, {
previewStatus: 'completed',
});

this.logger.log(`Preview generated successfully for template ${templateId}`);
} catch (error) {
this.logger.error(`Preview generation failed for template ${templateId}:`, error);

await this.templateRepository.update(templateId, {
previewStatus: 'failed',
});

throw error; // Let BullMQ handle retries
}
}
}

Step 4: Update UploadTemplateUseCase to Use Queue​

Update the TODO section in upload-template.use-case.ts:

import { InjectQueue } from '@nestjs/bull';
import { Queue } from 'bull';

export class UploadTemplateUseCase {
constructor(
// ... other dependencies
@InjectQueue('preview-generation') private readonly previewQueue: Queue,
) {}

async execute(dto: UploadTemplateDto, userContext: UserContext): Promise<DocumentTemplate> {
// ... existing code ...

// Replace the TODO comment with:
await this.previewQueue.add('generate-preview', {
templateId: template.id,
documentType: template.documentType,
});

// ... rest of code
}
}

Step 5: Register Processor in PdfModule​

import { PreviewGenerationProcessor } from '@/pdf/infrastructure/queues/preview-generation.processor';

@Module({
// ... imports above
providers: [
// ... existing providers
PreviewGenerationProcessor,
],
})

πŸ§ͺ Testing Redis & BullMQ​

Test Redis Connection​

# Test from command line
docker exec flowpos-workspace-redis-1 redis-cli ping
# Should return: PONG

# Test SET and GET
docker exec flowpos-workspace-redis-1 redis-cli SET test "Hello Redis"
docker exec flowpos-workspace-redis-1 redis-cli GET test
# Should return: "Hello Redis"

Test BullMQ Queue (After Implementation)​

# Upload a template (will trigger queue)
curl -X POST http://localhost:4000/pdf/templates/upload \
-H "Content-Type: application/json" \
-d '{
"name": "Test Template",
"documentType": "sale",
"templateFormat": "standard_a4",
"htmlTemplate": "<!DOCTYPE html><html><body>Test</body></html>",
"userId": "test-user"
}'

# Check template preview status
curl http://localhost:4000/pdf/templates/{templateId}
# Should show: "previewStatus": "processing" or "completed"

🐳 Docker Commands​

Start Redis​

docker-compose up -d redis

Stop Redis​

docker-compose stop redis

View Redis Logs​

docker-compose logs -f redis

Restart Redis​

docker-compose restart redis

Redis CLI Access​

docker exec -it flowpos-workspace-redis-1 redis-cli

πŸ“Š Redis Monitoring​

Check Redis Stats​

docker exec flowpos-workspace-redis-1 redis-cli INFO stats

Monitor Redis Commands​

docker exec flowpos-workspace-redis-1 redis-cli MONITOR

Check Memory Usage​

docker exec flowpos-workspace-redis-1 redis-cli INFO memory

βœ… Current Status​

βœ… Completed​

  • Redis container running on port 6379
  • Redis persistent storage configured
  • Health checks enabled
  • Database migration run successfully
  • All template tables created
  • BullMQ package installed

πŸ“ Next Steps​

  • Configure BullMQ in PdfModule
  • Create PreviewGenerationProcessor
  • Update UploadTemplateUseCase to use queue
  • Test preview generation workflow

πŸŽ‰ Summary​

Redis Status: βœ… Running (healthy)
Port: 6379
Storage: Persistent (redis_data volume)
Migration: βœ… Complete
Tables: βœ… All 5 template tables created

You're all set! Redis is running and ready for BullMQ integration! πŸš€