Saltar al contenido principal

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! 🚀