Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
179 lines (154 loc) · 5.58 KB
/
Copy pathworker.js
File metadata and controls
179 lines (154 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const { PrismaClient } = require('@prisma/client');
const { Pool } = require('pg');
const { PrismaPg } = require('@prisma/adapter-pg');
const nodemailer = require('nodemailer');
const { Resend } = require('resend');
// Need to load .env.local because worker runs in root
require('dotenv').config({ path: '.env.local' });
let connectionString = process.env.DATABASE_URL;
if (connectionString.includes('sslmode=require') && !connectionString.includes('uselibpqcompat')) {
connectionString = connectionString.replace('sslmode=require', 'sslmode=require&uselibpqcompat=true');
}
const pool = new Pool({ connectionString });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
const POLL_INTERVAL = 30 * 1000; // 30 seconds
const BATCH_SIZE = 20;
async function processQueue() {
console.log(`[Worker] Checking queue at ${new Date().toISOString()}`);
try {
// Find pending emails
const emails = await prisma.emailQueue.findMany({
where: {
status: 'pending',
OR: [
{ scheduledFor: null },
{ scheduledFor: { lte: new Date() } }
]
},
take: BATCH_SIZE,
include: {
user: true, // Need user details for SMTP config
}
});
if (emails.length === 0) {
return 0; // Nothing to process
}
console.log(`[Worker] Found ${emails.length} pending emails. Processing...`);
for (const email of emails) {
try {
const user = email.user;
let success = false;
let errorMsg = null;
let attachments = [];
if (email.attachmentPath) {
const fs = require('fs');
const path = require('path');
const paths = email.attachmentPath.split(',');
for (const p of paths) {
const cleanPath = p.trim();
if (fs.existsSync(cleanPath)) {
const buffer = fs.readFileSync(cleanPath);
attachments.push({
filename: path.basename(cleanPath),
content: buffer,
path: cleanPath
});
}
}
}
if (user.emailProvider === 'resend' && user.resendKey) {
const resend = new Resend(user.resendKey);
const payload = {
from: `${user.senderName || 'Relay'} <${user.resendFrom || 'noreply@yourdomain.com'}>`,
to: email.toEmail,
subject: email.subject,
html: email.content,
};
if (attachments.length > 0) payload.attachments = attachments.map(a => ({ filename: a.filename, content: a.content }));
const { data, error } = await resend.emails.send(payload);
if (error) {
errorMsg = error.message;
} else {
success = true;
}
} else if (user.emailProvider === 'smtp' && user.smtpHost) {
const transporter = nodemailer.createTransport({
host: user.smtpHost,
port: user.smtpPort || 587,
secure: user.smtpPort === 465,
auth: {
user: user.smtpUser,
pass: user.smtpPass,
},
});
const mailOptions = {
from: `"${user.senderName || 'Relay'}" <${user.smtpUser}>`,
to: email.toEmail,
subject: email.subject,
html: email.content,
};
if (attachments.length > 0) mailOptions.attachments = attachments.map(a => ({ filename: a.filename, path: a.path }));
await transporter.sendMail(mailOptions);
success = true;
} else {
errorMsg = "No valid email provider configured for user.";
}
if (success) {
await prisma.emailQueue.update({
where: { id: email.id },
data: { status: 'sent', sentAt: new Date() }
});
await prisma.systemLog.create({
data: {
userId: user.id,
action: 'EMAIL_SENT',
details: JSON.stringify({ emailId: email.id, to: email.toEmail, subject: email.subject })
}
});
console.log(`[Worker] Sent email to ${email.toEmail}`);
} else {
throw new Error(errorMsg || "Unknown sending error");
}
} catch (err) {
console.error(`[Worker] Failed to send email to ${email.toEmail}:`, err.message);
await prisma.emailQueue.update({
where: { id: email.id },
data: { status: 'failed' }
});
await prisma.systemLog.create({
data: {
userId: email.userId,
action: 'EMAIL_FAILED',
details: JSON.stringify({ emailId: email.id, to: email.toEmail, error: err.message })
}
});
}
}
return emails.length;
} catch (error) {
console.error(`[Worker] Fatal error checking queue:`, error);
return 0;
}
}
async function start() {
const isOnce = process.argv.includes('--once');
if (isOnce) {
console.log('[Worker] Running in local --once mode. Will exit when the queue is completely empty.');
while (true) {
const processedCount = await processQueue();
if (processedCount === 0) {
console.log('[Worker] Queue is empty. Exiting safely.');
process.exit(0);
}
// Brief pause between batches
await new Promise(res => setTimeout(res, 1000));
}
} else {
// Start polling
console.log('[Worker] Started Relay Background Worker in continuous polling mode...');
setInterval(processQueue, POLL_INTERVAL);
processQueue(); // Run immediately on start
}
}
start();