Flatten email processing structure to individual jobs per email

BREAKING CHANGE: Replaced batch email processing with individual jobs per email

Changes:
- Remove sendEmailTask.ts - no longer needed as each email gets its own job
- Add processEmailJob.ts - handles individual email processing
- Update sendEmail() to automatically create individual job per email
- Add processImmediately option to sendEmail() for instant processing
- Add processJobById() utility to run specific jobs immediately
- Update job registration to use new individual job structure
- Update dev API routes to use new processImmediately pattern
- Fix all TypeScript compilation errors

Benefits:
- Better job queue visibility (one job per email)
- More granular control over individual email processing
- Easier job monitoring and failure tracking
- Maintains backward compatibility via processImmediately option
- Simpler job queue management

Migration:
- Replace sendEmailJob usage with sendEmail({ processImmediately: true })
- Individual emails now appear as separate jobs in queue
- Batch processing still available via processEmailsTask if needed

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-14 20:19:07 +02:00
parent 08f017abed
commit caa3686f1a
7 changed files with 164 additions and 290 deletions

View File

@@ -1,6 +1,6 @@
import { getPayload } from 'payload'
import config from '@payload-config'
import { sendEmail, processEmailById } from '@xtr-dev/payload-mailing'
import { sendEmail } from '@xtr-dev/payload-mailing'
export async function POST(request: Request) {
try {
@@ -55,35 +55,21 @@ export async function POST(request: Request) {
emailOptions.data.scheduledAt = scheduledAt ? new Date(scheduledAt) : new Date(Date.now() + 60000)
}
const result = await sendEmail(payload, emailOptions)
// Set processImmediately for "send now" type
const processImmediately = (type === 'send' && !scheduledAt)
emailOptions.processImmediately = processImmediately
// If it's "send now" (not scheduled), process the email immediately
if (type === 'send' && !scheduledAt) {
try {
await processEmailById(payload, String(result.id))
return Response.json({
success: true,
emailId: result.id,
message: 'Email sent successfully',
status: 'sent'
})
} catch (processError) {
// If immediate processing fails, return that it's queued
console.warn('Failed to process email immediately, left in queue:', processError)
return Response.json({
success: true,
emailId: result.id,
message: 'Email queued successfully (immediate processing failed)',
status: 'queued'
})
}
}
const result = await sendEmail(payload, emailOptions)
return Response.json({
success: true,
emailId: result.id,
message: scheduledAt ? 'Email scheduled successfully' : 'Email queued successfully',
status: scheduledAt ? 'scheduled' : 'queued'
message: processImmediately ? 'Email sent successfully' :
scheduledAt ? 'Email scheduled successfully' :
'Email queued successfully',
status: processImmediately ? 'sent' :
scheduledAt ? 'scheduled' :
'queued'
})
} catch (error) {
console.error('Test email error:', error)