mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 08:13:23 +00:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8406bca718 | ||
| 59ce8c031a | |||
| 08ba814da0 | |||
| f303eda652 | |||
|
|
8e1128f1e8 | ||
| c62a364d9c | |||
|
|
50ce181893 | ||
| 8b2af8164a | |||
| 3d7ddb8c97 | |||
|
|
2c0f202518 | ||
| 3f177cfeb5 | |||
| e364dd2c58 | |||
|
|
aa5a03b5b0 | ||
| 8ee3ff5a7d | |||
|
|
2220d83288 | ||
| 2f46dde532 | |||
| 02a9334bf4 | |||
|
|
de1ae636de | ||
| ae38653466 | |||
| fe8c4d194e | |||
| 0198821ff3 |
@@ -123,123 +123,6 @@ export default buildConfig({
|
||||
retryDelay: 60000, // 1 minute for dev
|
||||
queue: 'default',
|
||||
|
||||
// Example: Collection overrides for customization
|
||||
// Uncomment and modify as needed for your use case
|
||||
/*
|
||||
collections: {
|
||||
templates: {
|
||||
// Custom access controls - restrict who can manage templates
|
||||
access: {
|
||||
read: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin' || user.permissions?.includes('mailing:read')
|
||||
},
|
||||
create: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin' || user.permissions?.includes('mailing:create')
|
||||
},
|
||||
update: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin' || user.permissions?.includes('mailing:update')
|
||||
},
|
||||
delete: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin'
|
||||
},
|
||||
},
|
||||
// Custom admin UI settings
|
||||
admin: {
|
||||
group: 'Marketing',
|
||||
description: 'Email templates with enhanced security and categorization'
|
||||
},
|
||||
// Add custom fields to templates
|
||||
fields: [
|
||||
// Default plugin fields are automatically included
|
||||
{
|
||||
name: 'category',
|
||||
type: 'select',
|
||||
options: [
|
||||
{ label: 'Marketing', value: 'marketing' },
|
||||
{ label: 'Transactional', value: 'transactional' },
|
||||
{ label: 'System Notifications', value: 'system' }
|
||||
],
|
||||
defaultValue: 'transactional',
|
||||
admin: {
|
||||
position: 'sidebar',
|
||||
description: 'Template category for organization'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'tags',
|
||||
type: 'text',
|
||||
hasMany: true,
|
||||
admin: {
|
||||
position: 'sidebar',
|
||||
description: 'Tags for easy template filtering'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'isActive',
|
||||
type: 'checkbox',
|
||||
defaultValue: true,
|
||||
admin: {
|
||||
position: 'sidebar',
|
||||
description: 'Only active templates can be used'
|
||||
}
|
||||
}
|
||||
],
|
||||
// Custom validation hooks
|
||||
hooks: {
|
||||
beforeChange: [
|
||||
({ data, req }) => {
|
||||
// Example: Only admins can create system templates
|
||||
if (data.category === 'system' && req.user?.role !== 'admin') {
|
||||
throw new Error('Only administrators can create system notification templates')
|
||||
}
|
||||
|
||||
// Example: Auto-generate slug if not provided
|
||||
if (!data.slug && data.name) {
|
||||
data.slug = data.name.toLowerCase()
|
||||
.replace(/[^a-z0-9]/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^-|-$/g, '')
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
emails: {
|
||||
// Restrict access to emails collection
|
||||
access: {
|
||||
read: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin' || user.permissions?.includes('mailing:read')
|
||||
},
|
||||
create: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin' || user.permissions?.includes('mailing:create')
|
||||
},
|
||||
update: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin' || user.permissions?.includes('mailing:update')
|
||||
},
|
||||
delete: ({ req: { user } }) => {
|
||||
if (!user) return false
|
||||
return user.role === 'admin'
|
||||
},
|
||||
},
|
||||
// Custom admin configuration for emails
|
||||
admin: {
|
||||
group: 'Marketing',
|
||||
description: 'Email delivery tracking and management',
|
||||
defaultColumns: ['subject', 'to', 'status', 'priority', 'scheduledAt'],
|
||||
}
|
||||
}
|
||||
},
|
||||
*/
|
||||
|
||||
// Optional: Custom rich text editor configuration
|
||||
// Comment out to use default lexical editor
|
||||
richTextEditor: lexicalEditor({
|
||||
@@ -256,12 +139,6 @@ export default buildConfig({
|
||||
// etc.
|
||||
],
|
||||
}),
|
||||
|
||||
|
||||
// Called after mailing plugin is fully initialized
|
||||
onReady: async (payload) => {
|
||||
await seedUser(payload)
|
||||
},
|
||||
}),
|
||||
],
|
||||
secret: process.env.PAYLOAD_SECRET || 'test-secret_key',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-mailing",
|
||||
"version": "0.4.8",
|
||||
"version": "0.4.15",
|
||||
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { CollectionConfig } from 'payload'
|
||||
import { findExistingJobs, ensureEmailJob, updateEmailJobRelationship } from '../utils/jobScheduler.js'
|
||||
import { createContextLogger } from '../utils/logger.js'
|
||||
import { resolveID } from '../utils/helpers.js'
|
||||
|
||||
const Emails: CollectionConfig = {
|
||||
slug: 'emails',
|
||||
@@ -9,6 +11,26 @@ const Emails: CollectionConfig = {
|
||||
group: 'Mailing',
|
||||
description: 'Email delivery and status tracking',
|
||||
},
|
||||
defaultPopulate: {
|
||||
template: true,
|
||||
to: true,
|
||||
cc: true,
|
||||
bcc: true,
|
||||
from: true,
|
||||
replyTo: true,
|
||||
jobs: true,
|
||||
status: true,
|
||||
attempts: true,
|
||||
lastAttemptAt: true,
|
||||
error: true,
|
||||
priority: true,
|
||||
scheduledAt: true,
|
||||
sentAt: true,
|
||||
variables: true,
|
||||
html: true,
|
||||
text: true,
|
||||
createdAt: true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'template',
|
||||
@@ -176,9 +198,10 @@ const Emails: CollectionConfig = {
|
||||
readOnly: true,
|
||||
},
|
||||
filterOptions: ({ id }) => {
|
||||
const emailId = resolveID({ id })
|
||||
return {
|
||||
'input.emailId': {
|
||||
equals: id,
|
||||
equals: emailId ? String(emailId) : '',
|
||||
},
|
||||
}
|
||||
},
|
||||
@@ -220,7 +243,8 @@ const Emails: CollectionConfig = {
|
||||
}
|
||||
} catch (error) {
|
||||
// Log error but don't throw - we don't want to fail the email operation
|
||||
console.error(`Failed to ensure job for email ${doc.id}:`, error)
|
||||
const logger = createContextLogger(req.payload, 'EMAILS_HOOK')
|
||||
logger.error(`Failed to ensure job for email ${doc.id}:`, error)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
import { processEmailsJob } from './processEmailsTask.js'
|
||||
import { processEmailJob } from './processEmailJob.js'
|
||||
|
||||
/**
|
||||
* All mailing-related jobs that get registered with Payload
|
||||
*
|
||||
* Note: The sendEmailJob has been removed as each email now gets its own individual processEmailJob
|
||||
*/
|
||||
export const mailingJobs = [
|
||||
processEmailsJob, // Kept for backward compatibility and batch processing if needed
|
||||
processEmailJob, // New individual email processing job
|
||||
processEmailJob,
|
||||
]
|
||||
|
||||
// Re-export everything from individual job files
|
||||
export * from './processEmailsTask.js'
|
||||
export * from './processEmailJob.js'
|
||||
|
||||
@@ -13,7 +13,6 @@ export interface ProcessEmailJobInput {
|
||||
|
||||
/**
|
||||
* Job definition for processing a single email
|
||||
* This replaces the batch processing approach with individual email jobs
|
||||
*/
|
||||
export const processEmailJob = {
|
||||
slug: 'process-email',
|
||||
@@ -69,4 +68,4 @@ export const processEmailJob = {
|
||||
}
|
||||
}
|
||||
|
||||
export default processEmailJob
|
||||
export default processEmailJob
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
import type { PayloadRequest, Payload } from 'payload'
|
||||
import { processAllEmails } from '../utils/emailProcessor.js'
|
||||
|
||||
/**
|
||||
* Data passed to the process emails task
|
||||
*/
|
||||
export interface ProcessEmailsTaskData {
|
||||
// Currently no data needed - always processes both pending and failed emails
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler function for processing emails
|
||||
* Used internally by the task definition
|
||||
*/
|
||||
export const processEmailsTaskHandler = async (
|
||||
job: { data: ProcessEmailsTaskData },
|
||||
context: { req: PayloadRequest }
|
||||
) => {
|
||||
const { req } = context
|
||||
const payload = (req as any).payload
|
||||
|
||||
// Use the shared email processing logic
|
||||
await processAllEmails(payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Task definition for processing emails
|
||||
* This is what gets registered with Payload's job system
|
||||
*/
|
||||
export const processEmailsTask = {
|
||||
slug: 'process-emails',
|
||||
handler: async ({ job, req }: { job: any; req: any }) => {
|
||||
// Get mailing context from payload
|
||||
const payload = (req as any).payload
|
||||
const mailingContext = payload.mailing
|
||||
|
||||
if (!mailingContext) {
|
||||
throw new Error('Mailing plugin not properly initialized')
|
||||
}
|
||||
|
||||
// Use the task handler
|
||||
await processEmailsTaskHandler(
|
||||
job as { data: ProcessEmailsTaskData },
|
||||
{ req }
|
||||
)
|
||||
|
||||
return {
|
||||
output: {
|
||||
success: true,
|
||||
message: 'Email queue processing completed successfully'
|
||||
}
|
||||
}
|
||||
},
|
||||
interfaceName: 'ProcessEmailsTask',
|
||||
}
|
||||
|
||||
// For backward compatibility, export as processEmailsJob
|
||||
export const processEmailsJob = processEmailsTask
|
||||
|
||||
/**
|
||||
* Helper function to schedule an email processing job
|
||||
* Used by the plugin during initialization and can be used by developers
|
||||
*/
|
||||
export const scheduleEmailsJob = async (
|
||||
payload: Payload,
|
||||
queueName: string,
|
||||
delay?: number
|
||||
) => {
|
||||
if (!payload.jobs) {
|
||||
console.warn('PayloadCMS jobs not configured - emails will not be processed automatically')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await payload.jobs.queue({
|
||||
queue: queueName,
|
||||
task: 'process-emails',
|
||||
input: {},
|
||||
waitUntil: delay ? new Date(Date.now() + delay) : undefined,
|
||||
} as any)
|
||||
} catch (error) {
|
||||
console.error('Failed to schedule email processing job:', error)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import { Payload } from 'payload'
|
||||
import { getMailing, renderTemplate, parseAndValidateEmails, sanitizeFromName } from './utils/helpers.js'
|
||||
import { BaseEmailDocument } from './types/index.js'
|
||||
import { processJobById } from './utils/emailProcessor.js'
|
||||
import { createContextLogger } from './utils/logger.js'
|
||||
import { pollForJobId } from './utils/jobPolling.js'
|
||||
|
||||
// Options for sending emails
|
||||
export interface SendEmailOptions<T extends BaseEmailDocument = BaseEmailDocument> {
|
||||
@@ -47,7 +49,6 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
|
||||
let emailData: Partial<TEmail> = { ...options.data } as Partial<TEmail>
|
||||
|
||||
// If using a template, render it first
|
||||
if (options.template) {
|
||||
const { html, text, subject } = await renderTemplate(
|
||||
payload,
|
||||
@@ -55,7 +56,6 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
options.template.variables || {}
|
||||
)
|
||||
|
||||
// Template values take precedence over data values
|
||||
emailData = {
|
||||
...emailData,
|
||||
subject,
|
||||
@@ -69,20 +69,16 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
throw new Error('Field "to" is required for sending emails')
|
||||
}
|
||||
|
||||
// Validate required fields based on whether template was used
|
||||
if (options.template) {
|
||||
// When using template, subject and html should have been set by renderTemplate
|
||||
if (!emailData.subject || !emailData.html) {
|
||||
throw new Error(`Template rendering failed: template "${options.template.slug}" did not provide required subject and html content`)
|
||||
}
|
||||
} else {
|
||||
// When not using template, user must provide subject and html directly
|
||||
if (!emailData.subject || !emailData.html) {
|
||||
throw new Error('Fields "subject" and "html" are required when sending direct emails without a template')
|
||||
}
|
||||
}
|
||||
|
||||
// Process email addresses using shared validation (handle null values)
|
||||
if (emailData.to) {
|
||||
emailData.to = parseAndValidateEmails(emailData.to as string | string[])
|
||||
}
|
||||
@@ -94,19 +90,15 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
}
|
||||
if (emailData.replyTo) {
|
||||
const validated = parseAndValidateEmails(emailData.replyTo as string | string[])
|
||||
// replyTo should be a single email, so take the first one if array
|
||||
emailData.replyTo = validated && validated.length > 0 ? validated[0] : undefined
|
||||
}
|
||||
if (emailData.from) {
|
||||
const validated = parseAndValidateEmails(emailData.from as string | string[])
|
||||
// from should be a single email, so take the first one if array
|
||||
emailData.from = validated && validated.length > 0 ? validated[0] : undefined
|
||||
}
|
||||
|
||||
// Sanitize fromName to prevent header injection
|
||||
emailData.fromName = sanitizeFromName(emailData.fromName as string)
|
||||
|
||||
// Normalize Date objects to ISO strings for consistent database storage
|
||||
if (emailData.scheduledAt instanceof Date) {
|
||||
emailData.scheduledAt = emailData.scheduledAt.toISOString()
|
||||
}
|
||||
@@ -123,90 +115,36 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
||||
emailData.updatedAt = emailData.updatedAt.toISOString()
|
||||
}
|
||||
|
||||
// Create the email in the collection with proper typing
|
||||
// The hooks will automatically create and populate the job relationship
|
||||
const email = await payload.create({
|
||||
collection: collectionSlug,
|
||||
data: emailData
|
||||
})
|
||||
|
||||
// Validate that the created email has the expected structure
|
||||
if (!email || typeof email !== 'object' || !email.id) {
|
||||
throw new Error('Failed to create email: invalid response from database')
|
||||
}
|
||||
|
||||
// If processImmediately is true, get the job from the relationship and process it now
|
||||
if (options.processImmediately) {
|
||||
const logger = createContextLogger(payload, 'IMMEDIATE')
|
||||
|
||||
if (!payload.jobs) {
|
||||
throw new Error('PayloadCMS jobs not configured - cannot process email immediately')
|
||||
}
|
||||
|
||||
// Poll for the job with optimized backoff and timeout protection
|
||||
// This handles the async nature of hooks and ensures we wait for job creation
|
||||
const maxAttempts = 5 // Reduced from 10 to minimize delay
|
||||
const initialDelay = 25 // Reduced from 50ms for faster response
|
||||
const maxTotalTime = 3000 // 3 second total timeout
|
||||
const startTime = Date.now()
|
||||
let jobId: string | undefined
|
||||
|
||||
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
||||
// Check total timeout before continuing
|
||||
if (Date.now() - startTime > maxTotalTime) {
|
||||
throw new Error(
|
||||
`Job polling timed out after ${maxTotalTime}ms for email ${email.id}. ` +
|
||||
`The auto-scheduling may have failed or is taking longer than expected.`
|
||||
)
|
||||
}
|
||||
|
||||
// Calculate delay with exponential backoff (25ms, 50ms, 100ms, 200ms, 400ms)
|
||||
// Cap at 400ms per attempt for better responsiveness
|
||||
const delay = Math.min(initialDelay * Math.pow(2, attempt), 400)
|
||||
|
||||
if (attempt > 0) {
|
||||
await new Promise(resolve => setTimeout(resolve, delay))
|
||||
}
|
||||
|
||||
// Refetch the email to check for jobs
|
||||
const emailWithJobs = await payload.findByID({
|
||||
collection: collectionSlug,
|
||||
id: email.id,
|
||||
})
|
||||
|
||||
if (emailWithJobs.jobs && emailWithJobs.jobs.length > 0) {
|
||||
// Job found! Get the first job ID (should only be one for a new email)
|
||||
jobId = Array.isArray(emailWithJobs.jobs)
|
||||
? String(emailWithJobs.jobs[0])
|
||||
: String(emailWithJobs.jobs)
|
||||
break
|
||||
}
|
||||
|
||||
// Log on later attempts to help with debugging (reduced threshold)
|
||||
if (attempt >= 2) {
|
||||
console.log(`Waiting for job creation for email ${email.id}, attempt ${attempt + 1}/${maxAttempts}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (!jobId) {
|
||||
// Distinguish between different failure scenarios for better error handling
|
||||
const timeoutMsg = Date.now() - startTime >= maxTotalTime
|
||||
const errorType = timeoutMsg ? 'POLLING_TIMEOUT' : 'JOB_NOT_FOUND'
|
||||
|
||||
const baseMessage = timeoutMsg
|
||||
? `Job polling timed out after ${maxTotalTime}ms for email ${email.id}`
|
||||
: `No processing job found for email ${email.id} after ${maxAttempts} attempts (${Date.now() - startTime}ms)`
|
||||
|
||||
throw new Error(
|
||||
`${errorType}: ${baseMessage}. ` +
|
||||
`This indicates the email was created but job auto-scheduling failed. ` +
|
||||
`The email exists in the database but immediate processing cannot proceed. ` +
|
||||
`You may need to: 1) Check job queue configuration, 2) Verify database hooks are working, ` +
|
||||
`3) Process the email later using processEmailById('${email.id}').`
|
||||
)
|
||||
}
|
||||
// Poll for the job ID using configurable polling mechanism
|
||||
const { jobId } = await pollForJobId({
|
||||
payload,
|
||||
collectionSlug,
|
||||
emailId: email.id,
|
||||
config: mailingConfig.jobPolling,
|
||||
logger,
|
||||
})
|
||||
|
||||
try {
|
||||
await processJobById(payload, jobId)
|
||||
logger.debug(`Successfully processed email ${email.id} immediately`)
|
||||
} catch (error) {
|
||||
logger.error(`Failed to process email ${email.id} immediately:`, error)
|
||||
throw new Error(`Failed to process email ${email.id} immediately: ${String(error)}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,7 +366,7 @@ export class MailingService implements IMailingService {
|
||||
if (engine === 'liquidjs') {
|
||||
try {
|
||||
await this.ensureLiquidJSInitialized()
|
||||
if (this.liquid && typeof this.liquid !== 'boolean') {
|
||||
if (this.liquid) {
|
||||
return await this.liquid.parseAndRender(template, variables)
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import { Payload } from 'payload'
|
||||
import type { CollectionConfig, RichTextField } from 'payload'
|
||||
|
||||
// Payload ID type (string or number)
|
||||
export type PayloadID = string | number
|
||||
|
||||
// Payload relation type - can be populated (object with id) or unpopulated (just the ID)
|
||||
export type PayloadRelation<T extends { id: PayloadID }> = T | PayloadID
|
||||
|
||||
// JSON value type that matches Payload's JSON field type
|
||||
export type JSONValue = string | number | boolean | { [k: string]: unknown } | unknown[] | null | undefined
|
||||
|
||||
@@ -62,6 +68,13 @@ export interface BeforeSendMailOptions {
|
||||
|
||||
export type BeforeSendHook = (options: BeforeSendMailOptions, email: BaseEmailDocument) => BeforeSendMailOptions | Promise<BeforeSendMailOptions>
|
||||
|
||||
export interface JobPollingConfig {
|
||||
maxAttempts?: number // Maximum number of polling attempts (default: 5)
|
||||
initialDelay?: number // Initial delay in milliseconds (default: 25)
|
||||
maxTotalTime?: number // Maximum total polling time in milliseconds (default: 3000)
|
||||
maxBackoffDelay?: number // Maximum delay between attempts in milliseconds (default: 400)
|
||||
}
|
||||
|
||||
export interface MailingPluginConfig {
|
||||
collections?: {
|
||||
templates?: string | Partial<CollectionConfig>
|
||||
@@ -77,6 +90,7 @@ export interface MailingPluginConfig {
|
||||
richTextEditor?: RichTextField['editor']
|
||||
beforeSend?: BeforeSendHook
|
||||
initOrder?: 'before' | 'after'
|
||||
jobPolling?: JobPollingConfig
|
||||
}
|
||||
|
||||
export interface QueuedEmail {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Payload } from 'payload'
|
||||
import { createContextLogger } from './logger.js'
|
||||
|
||||
/**
|
||||
* Processes a single email by ID using the mailing service
|
||||
@@ -42,7 +43,7 @@ export async function processJobById(payload: Payload, jobId: string): Promise<v
|
||||
|
||||
try {
|
||||
// Run a specific job by its ID (using where clause to find the job)
|
||||
await payload.jobs.run({
|
||||
const result = await payload.jobs.run({
|
||||
where: {
|
||||
id: {
|
||||
equals: jobId
|
||||
@@ -50,6 +51,8 @@ export async function processJobById(payload: Payload, jobId: string): Promise<v
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
const logger = createContextLogger(payload, 'PROCESSOR')
|
||||
logger.error(`Job ${jobId} execution failed:`, error)
|
||||
throw new Error(`Failed to process job ${jobId}: ${String(error)}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Payload } from 'payload'
|
||||
import { TemplateVariables } from '../types/index.js'
|
||||
import { TemplateVariables, PayloadID, PayloadRelation } from '../types/index.js'
|
||||
|
||||
/**
|
||||
* Parse and validate email addresses
|
||||
@@ -74,6 +74,49 @@ export const sanitizeFromName = (fromName: string | null | undefined): string |
|
||||
return sanitized.length > 0 ? sanitized : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard to check if a Payload relation is populated (object) or unpopulated (ID)
|
||||
*/
|
||||
export const isPopulated = <T extends { id: PayloadID }>(
|
||||
value: PayloadRelation<T> | null | undefined
|
||||
): value is T => {
|
||||
return value !== null && value !== undefined && typeof value === 'object' && 'id' in value
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a Payload relation to just the ID
|
||||
* Handles both populated (object with id) and unpopulated (string/number) values
|
||||
*/
|
||||
export const resolveID = <T extends { id: PayloadID }>(
|
||||
value: PayloadRelation<T> | null | undefined
|
||||
): PayloadID | undefined => {
|
||||
if (value === null || value === undefined) return undefined
|
||||
|
||||
if (typeof value === 'string' || typeof value === 'number') {
|
||||
return value
|
||||
}
|
||||
|
||||
if (typeof value === 'object' && 'id' in value) {
|
||||
return value.id
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves an array of Payload relations to an array of IDs
|
||||
* Handles mixed arrays of populated and unpopulated values
|
||||
*/
|
||||
export const resolveIDs = <T extends { id: PayloadID }>(
|
||||
values: (PayloadRelation<T> | null | undefined)[] | null | undefined
|
||||
): PayloadID[] => {
|
||||
if (!values || !Array.isArray(values)) return []
|
||||
|
||||
return values
|
||||
.map(value => resolveID(value))
|
||||
.filter((id): id is PayloadID => id !== undefined)
|
||||
}
|
||||
|
||||
export const getMailing = (payload: Payload) => {
|
||||
const mailing = (payload as any).mailing
|
||||
if (!mailing) {
|
||||
|
||||
115
src/utils/jobPolling.ts
Normal file
115
src/utils/jobPolling.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { Payload } from 'payload'
|
||||
import { JobPollingConfig } from '../types/index.js'
|
||||
|
||||
export interface PollForJobIdOptions {
|
||||
payload: Payload
|
||||
collectionSlug: string
|
||||
emailId: string | number
|
||||
config?: JobPollingConfig
|
||||
logger?: {
|
||||
debug: (message: string, ...args: any[]) => void
|
||||
info: (message: string, ...args: any[]) => void
|
||||
warn: (message: string, ...args: any[]) => void
|
||||
error: (message: string, ...args: any[]) => void
|
||||
}
|
||||
}
|
||||
|
||||
export interface PollForJobIdResult {
|
||||
jobId: string
|
||||
attempts: number
|
||||
elapsedTime: number
|
||||
}
|
||||
|
||||
// Default job polling configuration values
|
||||
const DEFAULT_JOB_POLLING_CONFIG: Required<JobPollingConfig> = {
|
||||
maxAttempts: 5,
|
||||
initialDelay: 25,
|
||||
maxTotalTime: 3000,
|
||||
maxBackoffDelay: 400,
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls for a job ID associated with an email document using exponential backoff.
|
||||
* This utility handles the complexity of waiting for auto-scheduled jobs to be created.
|
||||
*
|
||||
* The polling mechanism uses exponential backoff with configurable parameters:
|
||||
* - Starts with an initial delay and doubles on each retry
|
||||
* - Caps individual delays at maxBackoffDelay
|
||||
* - Enforces a maximum total polling time
|
||||
*
|
||||
* @param options - Polling options including payload, collection, email ID, and config
|
||||
* @returns Promise resolving to job ID and timing information
|
||||
* @throws Error if job is not found within the configured limits
|
||||
*/
|
||||
export const pollForJobId = async (options: PollForJobIdOptions): Promise<PollForJobIdResult> => {
|
||||
const { payload, collectionSlug, emailId, logger } = options
|
||||
|
||||
// Merge user config with defaults
|
||||
const config: Required<JobPollingConfig> = {
|
||||
...DEFAULT_JOB_POLLING_CONFIG,
|
||||
...options.config,
|
||||
}
|
||||
|
||||
const { maxAttempts, initialDelay, maxTotalTime, maxBackoffDelay } = config
|
||||
const startTime = Date.now()
|
||||
let jobId: string | undefined
|
||||
|
||||
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
||||
const elapsedTime = Date.now() - startTime
|
||||
|
||||
// Check if we've exceeded the maximum total polling time
|
||||
if (elapsedTime > maxTotalTime) {
|
||||
throw new Error(
|
||||
`Job polling timed out after ${maxTotalTime}ms for email ${emailId}. ` +
|
||||
`The auto-scheduling may have failed or is taking longer than expected.`
|
||||
)
|
||||
}
|
||||
|
||||
// Calculate exponential backoff delay, capped at maxBackoffDelay
|
||||
const delay = Math.min(initialDelay * Math.pow(2, attempt), maxBackoffDelay)
|
||||
|
||||
// Wait before checking (skip on first attempt)
|
||||
if (attempt > 0) {
|
||||
await new Promise(resolve => setTimeout(resolve, delay))
|
||||
}
|
||||
|
||||
// Fetch the email document to check for associated jobs
|
||||
const emailWithJobs = await payload.findByID({
|
||||
collection: collectionSlug,
|
||||
id: emailId,
|
||||
})
|
||||
|
||||
// Check if jobs array exists and has entries
|
||||
if (emailWithJobs.jobs && emailWithJobs.jobs.length > 0) {
|
||||
const firstJob = Array.isArray(emailWithJobs.jobs) ? emailWithJobs.jobs[0] : emailWithJobs.jobs
|
||||
jobId = typeof firstJob === 'string' ? firstJob : String(firstJob.id || firstJob)
|
||||
|
||||
return {
|
||||
jobId,
|
||||
attempts: attempt + 1,
|
||||
elapsedTime: Date.now() - startTime,
|
||||
}
|
||||
}
|
||||
|
||||
// Log progress for attempts after the second try
|
||||
if (attempt >= 2 && logger) {
|
||||
logger.debug(`Waiting for job creation for email ${emailId}, attempt ${attempt + 1}/${maxAttempts}`)
|
||||
}
|
||||
}
|
||||
|
||||
// If we reach here, job was not found
|
||||
const elapsedTime = Date.now() - startTime
|
||||
const timeoutMsg = elapsedTime >= maxTotalTime
|
||||
const errorType = timeoutMsg ? 'POLLING_TIMEOUT' : 'JOB_NOT_FOUND'
|
||||
const baseMessage = timeoutMsg
|
||||
? `Job polling timed out after ${maxTotalTime}ms for email ${emailId}`
|
||||
: `No processing job found for email ${emailId} after ${maxAttempts} attempts (${elapsedTime}ms)`
|
||||
|
||||
throw new Error(
|
||||
`${errorType}: ${baseMessage}. ` +
|
||||
`This indicates the email was created but job auto-scheduling failed. ` +
|
||||
`The email exists in the database but immediate processing cannot proceed. ` +
|
||||
`You may need to: 1) Check job queue configuration, 2) Verify database hooks are working, ` +
|
||||
`3) Process the email later using processEmailById('${emailId}').`
|
||||
)
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Payload } from 'payload'
|
||||
import { createContextLogger } from './logger.js'
|
||||
|
||||
/**
|
||||
* Finds existing processing jobs for an email
|
||||
@@ -47,6 +48,8 @@ export async function ensureEmailJob(
|
||||
const mailingContext = (payload as any).mailing
|
||||
const queueName = options?.queueName || mailingContext?.config?.queue || 'default'
|
||||
|
||||
const logger = createContextLogger(payload, 'JOB_SCHEDULER')
|
||||
|
||||
// First, optimistically try to create the job
|
||||
// If it fails due to uniqueness constraint, then check for existing jobs
|
||||
// This approach minimizes the race condition window
|
||||
@@ -62,21 +65,23 @@ export async function ensureEmailJob(
|
||||
waitUntil: options?.scheduledAt ? new Date(options.scheduledAt) : undefined
|
||||
})
|
||||
|
||||
console.log(`Auto-scheduled processing job ${job.id} for email ${normalizedEmailId}`)
|
||||
logger.info(`Auto-scheduled processing job ${job.id} for email ${normalizedEmailId}`)
|
||||
|
||||
return {
|
||||
jobIds: [job.id],
|
||||
created: true
|
||||
}
|
||||
} catch (createError) {
|
||||
|
||||
// Job creation failed - likely due to duplicate constraint or system issue
|
||||
|
||||
// Check if duplicate jobs exist (handles race condition where another process created job)
|
||||
const existingJobs = await findExistingJobs(payload, normalizedEmailId)
|
||||
|
||||
|
||||
if (existingJobs.totalDocs > 0) {
|
||||
// Found existing jobs - return them (race condition handled successfully)
|
||||
console.log(`Found existing jobs for email ${normalizedEmailId}: ${existingJobs.docs.map(j => j.id).join(', ')}`)
|
||||
logger.debug(`Using existing jobs for email ${normalizedEmailId}: ${existingJobs.docs.map(j => j.id).join(', ')}`)
|
||||
return {
|
||||
jobIds: existingJobs.docs.map(job => job.id),
|
||||
created: false
|
||||
@@ -92,6 +97,7 @@ export async function ensureEmailJob(
|
||||
|
||||
if (isLikelyUniqueConstraint) {
|
||||
// This should not happen if our check above worked, but provide a clear error
|
||||
logger.warn(`Unique constraint violation but no existing jobs found for email ${normalizedEmailId}`)
|
||||
throw new Error(
|
||||
`Database uniqueness constraint violation for email ${normalizedEmailId}, but no existing jobs found. ` +
|
||||
`This indicates a potential data consistency issue. Original error: ${errorMessage}`
|
||||
@@ -99,6 +105,7 @@ export async function ensureEmailJob(
|
||||
}
|
||||
|
||||
// Non-constraint related error
|
||||
logger.error(`Job creation error for email ${normalizedEmailId}: ${errorMessage}`)
|
||||
throw new Error(`Failed to create job for email ${normalizedEmailId}: ${errorMessage}`)
|
||||
}
|
||||
}
|
||||
@@ -134,7 +141,8 @@ export async function updateEmailJobRelationship(
|
||||
})
|
||||
} catch (error) {
|
||||
const normalizedEmailId = String(emailId)
|
||||
console.error(`Failed to update email ${normalizedEmailId} with job relationship:`, error)
|
||||
const logger = createContextLogger(payload, 'JOB_SCHEDULER')
|
||||
logger.error(`Failed to update email ${normalizedEmailId} with job relationship:`, error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
48
src/utils/logger.ts
Normal file
48
src/utils/logger.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
let pluginLogger: any = null
|
||||
|
||||
/**
|
||||
* Get or create the plugin logger instance
|
||||
* Uses PAYLOAD_MAILING_LOG_LEVEL environment variable to configure log level
|
||||
* Defaults to 'info' if not set
|
||||
*/
|
||||
export function getPluginLogger(payload: Payload) {
|
||||
if (!pluginLogger && payload.logger) {
|
||||
const logLevel = process.env.PAYLOAD_MAILING_LOG_LEVEL || 'info'
|
||||
|
||||
pluginLogger = payload.logger.child({
|
||||
level: logLevel,
|
||||
plugin: '@xtr-dev/payload-mailing'
|
||||
})
|
||||
|
||||
// Log the configured log level on first initialization
|
||||
pluginLogger.info(`Logger initialized with level: ${logLevel}`)
|
||||
}
|
||||
|
||||
// Fallback to console if logger not available (shouldn't happen in normal operation)
|
||||
if (!pluginLogger) {
|
||||
return {
|
||||
debug: (...args: any[]) => console.log('[MAILING DEBUG]', ...args),
|
||||
info: (...args: any[]) => console.log('[MAILING INFO]', ...args),
|
||||
warn: (...args: any[]) => console.warn('[MAILING WARN]', ...args),
|
||||
error: (...args: any[]) => console.error('[MAILING ERROR]', ...args),
|
||||
}
|
||||
}
|
||||
|
||||
return pluginLogger
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a context-specific logger for a particular operation
|
||||
*/
|
||||
export function createContextLogger(payload: Payload, context: string) {
|
||||
const logger = getPluginLogger(payload)
|
||||
|
||||
return {
|
||||
debug: (message: string, ...args: any[]) => logger.debug(`[${context}] ${message}`, ...args),
|
||||
info: (message: string, ...args: any[]) => logger.info(`[${context}] ${message}`, ...args),
|
||||
warn: (message: string, ...args: any[]) => logger.warn(`[${context}] ${message}`, ...args),
|
||||
error: (message: string, ...args: any[]) => logger.error(`[${context}] ${message}`, ...args),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user