Files
picpeak/backend/src/services/emailService.js
T
paul e7ed7006fd fix: critical database connection pool exhaustion issues
- Disabled duplicate email service (emailService.js) that was creating redundant connections
- Increased connection pool size from 10 to 25 for production environment
- Extended session timeout cache from 5 to 30 minutes to reduce DB queries
- Added connection retry logic with exponential backoff for transient failures
- Fixed password validation to use retry wrapper and correct setting key
- Updated public settings and gallery middleware to handle connection failures gracefully

These changes address the "Connection terminated unexpectedly" errors in production by:
1. Reducing unnecessary database connections
2. Increasing available connection pool capacity
3. Implementing automatic retry for transient connection failures
4. Caching frequently accessed data for longer periods

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-20 20:51:44 +02:00

67 lines
1.8 KiB
JavaScript

const nodemailer = require('nodemailer');
const { db } = require('../database/db');
const { emailTemplates } = require('./emailTemplates');
const logger = require('../utils/logger');
// Create transporter
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: process.env.SMTP_SECURE === 'true',
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
}
});
async function sendEmail(to, type, data) {
try {
const template = emailTemplates[type](data);
const info = await transporter.sendMail({
from: process.env.EMAIL_FROM,
to: to,
subject: template.subject,
html: template.html,
text: template.text
});
logger.info(`Email sent: ${info.messageId}`);
return info;
} catch (error) {
logger.error('Error sending email:', error);
throw error;
}
}
// Process email queue
async function processEmailQueue() {
const pendingEmails = await db('email_queue')
.where('status', 'pending')
.where('retry_count', '<', 3)
.limit(10);
for (const email of pendingEmails) {
try {
const emailData = JSON.parse(email.email_data);
await sendEmail(email.recipient_email, email.email_type, emailData);
await db('email_queue').where('id', email.id).update({
status: 'sent',
sent_at: new Date()
});
} catch (error) {
await db('email_queue').where('id', email.id).update({
retry_count: email.retry_count + 1,
error_message: error.message
});
}
}
}
// Start email queue processor
// DISABLED: Using emailProcessor.js instead to prevent duplicate connections
// setInterval(processEmailQueue, 60000); // Process every minute
module.exports = { sendEmail, processEmailQueue };