@monque/core 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/dist/CHANGELOG.md +89 -0
- package/dist/LICENSE +15 -0
- package/dist/README.md +150 -0
- package/dist/index.cjs +6 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -14
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +6 -14
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +6 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -7
- package/src/events/index.ts +1 -0
- package/src/events/types.ts +113 -0
- package/src/index.ts +51 -0
- package/src/jobs/guards.ts +220 -0
- package/src/jobs/index.ts +29 -0
- package/src/jobs/types.ts +335 -0
- package/src/scheduler/helpers.ts +107 -0
- package/src/scheduler/index.ts +5 -0
- package/src/scheduler/monque.ts +1309 -0
- package/src/scheduler/services/change-stream-handler.ts +239 -0
- package/src/scheduler/services/index.ts +8 -0
- package/src/scheduler/services/job-manager.ts +455 -0
- package/src/scheduler/services/job-processor.ts +301 -0
- package/src/scheduler/services/job-query.ts +411 -0
- package/src/scheduler/services/job-scheduler.ts +267 -0
- package/src/scheduler/services/types.ts +48 -0
- package/src/scheduler/types.ts +123 -0
- package/src/shared/errors.ts +225 -0
- package/src/shared/index.ts +18 -0
- package/src/shared/utils/backoff.ts +77 -0
- package/src/shared/utils/cron.ts +67 -0
- package/src/shared/utils/index.ts +7 -0
- package/src/workers/index.ts +1 -0
- package/src/workers/types.ts +39 -0
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["CronExpressionParser","ObjectId","ObjectId","result","EventEmitter"],"sources":["../src/jobs/types.ts","../src/jobs/guards.ts","../src/shared/errors.ts","../src/shared/utils/backoff.ts","../src/shared/utils/cron.ts","../src/scheduler/helpers.ts","../src/scheduler/services/change-stream-handler.ts","../src/scheduler/services/job-manager.ts","../src/scheduler/services/job-processor.ts","../src/scheduler/services/job-query.ts","../src/scheduler/services/job-scheduler.ts","../src/scheduler/monque.ts"],"sourcesContent":["import type { ObjectId } from 'mongodb';\n\n/**\n * Represents the lifecycle states of a job in the queue.\n *\n * Jobs transition through states as follows:\n * - PENDING → PROCESSING (when picked up by a worker)\n * - PROCESSING → COMPLETED (on success)\n * - PROCESSING → PENDING (on failure, if retries remain)\n * - PROCESSING → FAILED (on failure, after max retries exhausted)\n * - PENDING → CANCELLED (on manual cancellation)\n *\n * @example\n * ```typescript\n * if (job.status === JobStatus.PENDING) {\n * // job is waiting to be picked up\n * }\n * ```\n */\nexport const JobStatus = {\n\t/** Job is waiting to be picked up by a worker */\n\tPENDING: 'pending',\n\t/** Job is currently being executed by a worker */\n\tPROCESSING: 'processing',\n\t/** Job completed successfully */\n\tCOMPLETED: 'completed',\n\t/** Job permanently failed after exhausting all retry attempts */\n\tFAILED: 'failed',\n\t/** Job was manually cancelled */\n\tCANCELLED: 'cancelled',\n} as const;\n\n/**\n * Union type of all possible job status values: `'pending' | 'processing' | 'completed' | 'failed' | 'cancelled'`\n */\nexport type JobStatusType = (typeof JobStatus)[keyof typeof JobStatus];\n\n/**\n * Represents a job in the Monque queue.\n *\n * @template T - The type of the job's data payload\n *\n * @example\n * ```typescript\n * interface EmailJobData {\n * to: string;\n * subject: string;\n * template: string;\n * }\n *\n * const job: Job<EmailJobData> = {\n * name: 'send-email',\n * data: { to: 'user@example.com', subject: 'Welcome!', template: 'welcome' },\n * status: JobStatus.PENDING,\n * nextRunAt: new Date(),\n * failCount: 0,\n * createdAt: new Date(),\n * updatedAt: new Date(),\n * };\n * ```\n */\nexport interface Job<T = unknown> {\n\t/** MongoDB document identifier */\n\t_id?: ObjectId;\n\n\t/** Job type identifier, matches worker registration */\n\tname: string;\n\n\t/** Job payload - must be JSON-serializable */\n\tdata: T;\n\n\t/** Current lifecycle state */\n\tstatus: JobStatusType;\n\n\t/** When the job should be processed */\n\tnextRunAt: Date;\n\n\t/** Timestamp when job was locked for processing */\n\tlockedAt?: Date | null;\n\n\t/**\n\t * Unique identifier of the scheduler instance that claimed this job.\n\t * Used for atomic claim pattern - ensures only one instance processes each job.\n\t * Set when a job is claimed, cleared when job completes or fails.\n\t */\n\tclaimedBy?: string | null;\n\n\t/**\n\t * Timestamp of the last heartbeat update for this job.\n\t * Used to detect stale jobs when a scheduler instance crashes without releasing.\n\t * Updated periodically while job is being processed.\n\t */\n\tlastHeartbeat?: Date | null;\n\n\t/**\n\t * Heartbeat interval in milliseconds for this job.\n\t * Stored on the job to allow recovery logic to use the correct timeout.\n\t */\n\theartbeatInterval?: number;\n\n\t/** Number of failed attempts */\n\tfailCount: number;\n\n\t/** Last failure error message */\n\tfailReason?: string;\n\n\t/** Cron expression for recurring jobs */\n\trepeatInterval?: string;\n\n\t/** Deduplication key to prevent duplicate jobs */\n\tuniqueKey?: string;\n\n\t/** Job creation timestamp */\n\tcreatedAt: Date;\n\n\t/** Last modification timestamp */\n\tupdatedAt: Date;\n}\n\n/**\n * A job that has been persisted to MongoDB and has a guaranteed `_id`.\n * This is returned by `enqueue()`, `now()`, and `schedule()` methods.\n *\n * @template T - The type of the job's data payload\n */\nexport type PersistedJob<T = unknown> = Job<T> & { _id: ObjectId };\n\n/**\n * Options for enqueueing a job.\n *\n * @example\n * ```typescript\n * await monque.enqueue('sync-user', { userId: '123' }, {\n * uniqueKey: 'sync-user-123',\n * runAt: new Date(Date.now() + 5000), // Run in 5 seconds\n * });\n * ```\n */\nexport interface EnqueueOptions {\n\t/**\n\t * Deduplication key. If a job with this key is already pending or processing,\n\t * the enqueue operation will not create a duplicate.\n\t */\n\tuniqueKey?: string;\n\n\t/**\n\t * When the job should be processed. Defaults to immediately (new Date()).\n\t */\n\trunAt?: Date;\n}\n\n/**\n * Options for scheduling a recurring job.\n *\n * @example\n * ```typescript\n * await monque. schedule('0 * * * *', 'hourly-cleanup', { dir: '/tmp' }, {\n * uniqueKey: 'hourly-cleanup-job',\n * });\n * ```\n */\nexport interface ScheduleOptions {\n\t/**\n\t * Deduplication key. If a job with this key is already pending or processing,\n\t * the schedule operation will not create a duplicate.\n\t */\n\tuniqueKey?: string;\n}\n\n/**\n * Filter options for querying jobs.\n *\n * Use with `monque.getJobs()` to filter jobs by name, status, or limit results.\n *\n * @example\n * ```typescript\n * // Get all pending email jobs\n * const pendingEmails = await monque.getJobs({\n * name: 'send-email',\n * status: JobStatus.PENDING,\n * });\n *\n * // Get all failed or completed jobs (paginated)\n * const finishedJobs = await monque.getJobs({\n * status: [JobStatus.COMPLETED, JobStatus.FAILED],\n * limit: 50,\n * skip: 100,\n * });\n * ```\n */\nexport interface GetJobsFilter {\n\t/** Filter by job type name */\n\tname?: string;\n\n\t/** Filter by status (single or multiple) */\n\tstatus?: JobStatusType | JobStatusType[];\n\n\t/** Maximum number of jobs to return (default: 100) */\n\tlimit?: number;\n\n\t/** Number of jobs to skip for pagination */\n\tskip?: number;\n}\n\n/**\n * Handler function signature for processing jobs.\n *\n * @template T - The type of the job's data payload\n *\n * @example\n * ```typescript\n * const emailHandler: JobHandler<EmailJobData> = async (job) => {\n * await sendEmail(job.data.to, job.data.subject);\n * };\n * ```\n */\nexport type JobHandler<T = unknown> = (job: Job<T>) => Promise<void> | void;\n\n/**\n * Valid cursor directions for pagination.\n *\n * @example\n * ```typescript\n * const direction = CursorDirection.FORWARD;\n * ```\n */\nexport const CursorDirection = {\n\tFORWARD: 'forward',\n\tBACKWARD: 'backward',\n} as const;\n\nexport type CursorDirectionType = (typeof CursorDirection)[keyof typeof CursorDirection];\n\n/**\n * Selector options for bulk operations.\n *\n * Used to select multiple jobs for operations like cancellation or deletion.\n *\n * @example\n * ```typescript\n * // Select all failed jobs older than 7 days\n * const selector: JobSelector = {\n * status: JobStatus.FAILED,\n * olderThan: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),\n * };\n * ```\n */\nexport interface JobSelector {\n\tname?: string;\n\tstatus?: JobStatusType | JobStatusType[];\n\tolderThan?: Date;\n\tnewerThan?: Date;\n}\n\n/**\n * Options for cursor-based pagination.\n *\n * @example\n * ```typescript\n * const options: CursorOptions = {\n * limit: 50,\n * direction: CursorDirection.FORWARD,\n * filter: { status: JobStatus.PENDING },\n * };\n * ```\n */\nexport interface CursorOptions {\n\tcursor?: string;\n\tlimit?: number;\n\tdirection?: CursorDirectionType;\n\tfilter?: Pick<GetJobsFilter, 'name' | 'status'>;\n}\n\n/**\n * Response structure for cursor-based pagination.\n *\n * @template T - The type of the job's data payload\n *\n * @example\n * ```typescript\n * const page = await monque.listJobs({ limit: 10 });\n * console.log(`Got ${page.jobs.length} jobs`);\n *\n * if (page.hasNextPage) {\n * console.log(`Next cursor: ${page.cursor}`);\n * }\n * ```\n */\nexport interface CursorPage<T = unknown> {\n\tjobs: PersistedJob<T>[];\n\tcursor: string | null;\n\thasNextPage: boolean;\n\thasPreviousPage: boolean;\n}\n\n/**\n * Aggregated statistics for the job queue.\n *\n * @example\n * ```typescript\n * const stats = await monque.getQueueStats();\n * console.log(`Total jobs: ${stats.total}`);\n * console.log(`Pending: ${stats.pending}`);\n * console.log(`Processing: ${stats.processing}`);\n * console.log(`Failed: ${stats.failed}`);\n * console.log(`Start to finish avg: ${stats.avgProcessingDurationMs}ms`);\n * ```\n */\nexport interface QueueStats {\n\tpending: number;\n\tprocessing: number;\n\tcompleted: number;\n\tfailed: number;\n\tcancelled: number;\n\ttotal: number;\n\tavgProcessingDurationMs?: number;\n}\n\n/**\n * Result of a bulk operation.\n *\n * @example\n * ```typescript\n * const result = await monque.cancelJobs(selector);\n * console.log(`Cancelled ${result.count} jobs`);\n *\n * if (result.errors.length > 0) {\n * console.warn('Some jobs could not be cancelled:', result.errors);\n * }\n * ```\n */\nexport interface BulkOperationResult {\n\tcount: number;\n\terrors: Array<{ jobId: string; error: string }>;\n}\n","import type { Job, JobStatusType, PersistedJob } from './types.js';\nimport { JobStatus } from './types.js';\n\n/**\n * Type guard to check if a job has been persisted to MongoDB.\n *\n * A persisted job is guaranteed to have an `_id` field, which means it has been\n * successfully inserted into the database. This is useful when you need to ensure\n * a job can be updated or referenced by its ID.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job has a valid `_id`, narrowing the type to `PersistedJob<T>`\n *\n * @example Basic usage\n * ```typescript\n * const job: Job<EmailData> = await monque.enqueue('send-email', emailData);\n *\n * if (isPersistedJob(job)) {\n * // TypeScript knows job._id exists\n * console.log(`Job ID: ${job._id.toString()}`);\n * }\n * ```\n *\n * @example In a conditional\n * ```typescript\n * function logJobId(job: Job) {\n * if (!isPersistedJob(job)) {\n * console.log('Job not yet persisted');\n * return;\n * }\n * // TypeScript knows job is PersistedJob here\n * console.log(`Processing job ${job._id.toString()}`);\n * }\n * ```\n */\nexport function isPersistedJob<T>(job: Job<T>): job is PersistedJob<T> {\n\treturn '_id' in job && job._id !== undefined && job._id !== null;\n}\n\n/**\n * Type guard to check if a value is a valid job status.\n *\n * Validates that a value is one of the five valid job statuses: `'pending'`,\n * `'processing'`, `'completed'`, `'failed'`, or `'cancelled'`. Useful for runtime validation\n * of user input or external data.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid `JobStatusType`, narrowing the type\n *\n * @example Validating user input\n * ```typescript\n * function filterByStatus(status: string) {\n * if (!isValidJobStatus(status)) {\n * throw new Error(`Invalid status: ${status}`);\n * }\n * // TypeScript knows status is JobStatusType here\n * return db.jobs.find({ status });\n * }\n * ```\n *\n * @example Runtime validation\n * ```typescript\n * const statusFromApi = externalData.status;\n *\n * if (isValidJobStatus(statusFromApi)) {\n * job.status = statusFromApi;\n * } else {\n * job.status = JobStatus.PENDING;\n * }\n * ```\n */\nexport function isValidJobStatus(value: unknown): value is JobStatusType {\n\treturn typeof value === 'string' && Object.values(JobStatus).includes(value as JobStatusType);\n}\n\n/**\n * Type guard to check if a job is in pending status.\n *\n * A convenience helper for checking if a job is waiting to be processed.\n * Equivalent to `job.status === JobStatus.PENDING` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'pending'`\n *\n * @example Filter pending jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const pendingJobs = jobs.filter(isPendingJob);\n * console.log(`${pendingJobs.length} jobs waiting to be processed`);\n * ```\n *\n * @example Conditional logic\n * ```typescript\n * if (isPendingJob(job)) {\n * await monque.now(job.name, job.data);\n * }\n * ```\n */\nexport function isPendingJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.PENDING;\n}\n\n/**\n * Type guard to check if a job is currently being processed.\n *\n * A convenience helper for checking if a job is actively running.\n * Equivalent to `job.status === JobStatus.PROCESSING` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'processing'`\n *\n * @example Monitor active jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const activeJobs = jobs.filter(isProcessingJob);\n * console.log(`${activeJobs.length} jobs currently running`);\n * ```\n */\nexport function isProcessingJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.PROCESSING;\n}\n\n/**\n * Type guard to check if a job has completed successfully.\n *\n * A convenience helper for checking if a job finished without errors.\n * Equivalent to `job.status === JobStatus.COMPLETED` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'completed'`\n *\n * @example Find completed jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const completedJobs = jobs.filter(isCompletedJob);\n * console.log(`${completedJobs.length} jobs completed successfully`);\n * ```\n */\nexport function isCompletedJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.COMPLETED;\n}\n\n/**\n * Type guard to check if a job has permanently failed.\n *\n * A convenience helper for checking if a job exhausted all retries.\n * Equivalent to `job.status === JobStatus.FAILED` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'failed'`\n *\n * @example Handle failed jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const failedJobs = jobs.filter(isFailedJob);\n *\n * for (const job of failedJobs) {\n * console.error(`Job ${job.name} failed: ${job.failReason}`);\n * await sendAlert(job);\n * }\n * ```\n */\nexport function isFailedJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.FAILED;\n}\n\n/**\n * Type guard to check if a job has been manually cancelled.\n *\n * A convenience helper for checking if a job was cancelled by an operator.\n * Equivalent to `job.status === JobStatus.CANCELLED` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'cancelled'`\n *\n * @example Filter cancelled jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const cancelledJobs = jobs.filter(isCancelledJob);\n * console.log(`${cancelledJobs.length} jobs were cancelled`);\n * ```\n */\nexport function isCancelledJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.CANCELLED;\n}\n\n/**\n * Type guard to check if a job is a recurring scheduled job.\n *\n * A recurring job has a `repeatInterval` cron expression and will be automatically\n * rescheduled after each successful completion.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job has a `repeatInterval` defined\n *\n * @example Filter recurring jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const recurringJobs = jobs.filter(isRecurringJob);\n * console.log(`${recurringJobs.length} jobs will repeat automatically`);\n * ```\n *\n * @example Conditional cleanup\n * ```typescript\n * if (!isRecurringJob(job) && isCompletedJob(job)) {\n * // Safe to delete one-time completed jobs\n * await deleteJob(job._id);\n * }\n * ```\n */\nexport function isRecurringJob<T>(job: Job<T>): boolean {\n\treturn job.repeatInterval !== undefined && job.repeatInterval !== null;\n}\n","import type { Job } from '@/jobs';\n\n/**\n * Base error class for all Monque-related errors.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof MonqueError) {\n * console.error('Monque error:', error.message);\n * }\n * }\n * ```\n */\nexport class MonqueError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'MonqueError';\n\t\t// Maintains proper stack trace for where our error was thrown (only available on V8)\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, MonqueError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when an invalid cron expression is provided.\n *\n * @example\n * ```typescript\n * try {\n * await monque.schedule('invalid cron', 'job', data);\n * } catch (error) {\n * if (error instanceof InvalidCronError) {\n * console.error('Invalid expression:', error.expression);\n * }\n * }\n * ```\n */\nexport class InvalidCronError extends MonqueError {\n\tconstructor(\n\t\tpublic readonly expression: string,\n\t\tmessage: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'InvalidCronError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, InvalidCronError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when there's a database connection issue.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof ConnectionError) {\n * console.error('Database connection lost');\n * }\n * }\n * ```\n */\nexport class ConnectionError extends MonqueError {\n\tconstructor(message: string, options?: { cause?: Error }) {\n\t\tsuper(message);\n\t\tthis.name = 'ConnectionError';\n\t\tif (options?.cause) {\n\t\t\tthis.cause = options.cause;\n\t\t}\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ConnectionError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when graceful shutdown times out.\n * Includes information about jobs that were still in progress.\n *\n * @example\n * ```typescript\n * try {\n * await monque.stop();\n * } catch (error) {\n * if (error instanceof ShutdownTimeoutError) {\n * console.error('Incomplete jobs:', error.incompleteJobs.length);\n * }\n * }\n * ```\n */\nexport class ShutdownTimeoutError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly incompleteJobs: Job[],\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ShutdownTimeoutError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ShutdownTimeoutError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when attempting to register a worker for a job name\n * that already has a registered worker, without explicitly allowing replacement.\n *\n * @example\n * ```typescript\n * try {\n * monque.register('send-email', handler1);\n * monque.register('send-email', handler2); // throws\n * } catch (error) {\n * if (error instanceof WorkerRegistrationError) {\n * console.error('Worker already registered for:', error.jobName);\n * }\n * }\n *\n * // To intentionally replace a worker:\n * monque.register('send-email', handler2, { replace: true });\n * ```\n */\nexport class WorkerRegistrationError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly jobName: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'WorkerRegistrationError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, WorkerRegistrationError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when a state transition is invalid.\n *\n * @example\n * ```typescript\n * try {\n * await monque.cancelJob(jobId);\n * } catch (error) {\n * if (error instanceof JobStateError) {\n * console.error(`Cannot cancel job in state: ${error.currentStatus}`);\n * }\n * }\n * ```\n */\nexport class JobStateError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly jobId: string,\n\t\tpublic readonly currentStatus: string,\n\t\tpublic readonly attemptedAction: 'cancel' | 'retry' | 'reschedule',\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'JobStateError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, JobStateError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when a pagination cursor is invalid or malformed.\n *\n * @example\n * ```typescript\n * try {\n * await monque.listJobs({ cursor: 'invalid-cursor' });\n * } catch (error) {\n * if (error instanceof InvalidCursorError) {\n * console.error('Invalid cursor provided');\n * }\n * }\n * ```\n */\nexport class InvalidCursorError extends MonqueError {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'InvalidCursorError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, InvalidCursorError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when a statistics aggregation times out.\n *\n * @example\n * ```typescript\n * try {\n * const stats = await monque.getQueueStats();\n * } catch (error) {\n * if (error instanceof AggregationTimeoutError) {\n * console.error('Stats took too long to calculate');\n * }\n * }\n * ```\n */\nexport class AggregationTimeoutError extends MonqueError {\n\tconstructor(message: string = 'Statistics aggregation exceeded 30 second timeout') {\n\t\tsuper(message);\n\t\tthis.name = 'AggregationTimeoutError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, AggregationTimeoutError);\n\t\t}\n\t}\n}\n","/**\n * Default base interval for exponential backoff in milliseconds.\n * @default 1000\n */\nexport const DEFAULT_BASE_INTERVAL = 1000;\n\n/**\n * Default maximum delay cap for exponential backoff in milliseconds.\n *\n * This prevents unbounded delays (e.g. failCount=20 is >11 days at 1s base)\n * and avoids precision/overflow issues for very large fail counts.\n * @default 86400000 (24 hours)\n */\nexport const DEFAULT_MAX_BACKOFF_DELAY = 24 * 60 * 60 * 1_000;\n\n/**\n * Calculate the next run time using exponential backoff.\n *\n * Formula: nextRunAt = now + (2^failCount × baseInterval)\n *\n * @param failCount - Number of previous failed attempts\n * @param baseInterval - Base interval in milliseconds (default: 1000ms)\n * @param maxDelay - Maximum delay in milliseconds (optional)\n * @returns The next run date\n *\n * @example\n * ```typescript\n * // First retry (failCount=1): 2^1 * 1000 = 2000ms delay\n * const nextRun = calculateBackoff(1);\n *\n * // Second retry (failCount=2): 2^2 * 1000 = 4000ms delay\n * const nextRun = calculateBackoff(2);\n *\n * // With custom base interval\n * const nextRun = calculateBackoff(3, 500); // 2^3 * 500 = 4000ms delay\n *\n * // With max delay\n * const nextRun = calculateBackoff(10, 1000, 60000); // capped at 60000ms\n * ```\n */\nexport function calculateBackoff(\n\tfailCount: number,\n\tbaseInterval: number = DEFAULT_BASE_INTERVAL,\n\tmaxDelay?: number,\n): Date {\n\tconst effectiveMaxDelay = maxDelay ?? DEFAULT_MAX_BACKOFF_DELAY;\n\tlet delay = 2 ** failCount * baseInterval;\n\n\tif (delay > effectiveMaxDelay) {\n\t\tdelay = effectiveMaxDelay;\n\t}\n\n\treturn new Date(Date.now() + delay);\n}\n\n/**\n * Calculate just the delay in milliseconds for a given fail count.\n *\n * @param failCount - Number of previous failed attempts\n * @param baseInterval - Base interval in milliseconds (default: 1000ms)\n * @param maxDelay - Maximum delay in milliseconds (optional)\n * @returns The delay in milliseconds\n */\nexport function calculateBackoffDelay(\n\tfailCount: number,\n\tbaseInterval: number = DEFAULT_BASE_INTERVAL,\n\tmaxDelay?: number,\n): number {\n\tconst effectiveMaxDelay = maxDelay ?? DEFAULT_MAX_BACKOFF_DELAY;\n\tlet delay = 2 ** failCount * baseInterval;\n\n\tif (delay > effectiveMaxDelay) {\n\t\tdelay = effectiveMaxDelay;\n\t}\n\n\treturn delay;\n}\n","import { CronExpressionParser } from 'cron-parser';\n\nimport { InvalidCronError } from '../errors.js';\n\n/**\n * Parse a cron expression and return the next scheduled run date.\n *\n * @param expression - A 5-field cron expression (minute hour day-of-month month day-of-week) or a predefined expression\n * @param currentDate - The reference date for calculating next run (default: now)\n * @returns The next scheduled run date\n * @throws {InvalidCronError} If the cron expression is invalid\n *\n * @example\n * ```typescript\n * // Every minute\n * const nextRun = getNextCronDate('* * * * *');\n *\n * // Every day at midnight\n * const nextRun = getNextCronDate('0 0 * * *');\n *\n * // Using predefined expression\n * const nextRun = getNextCronDate('@daily');\n *\n * // Every Monday at 9am\n * const nextRun = getNextCronDate('0 9 * * 1');\n * ```\n */\nexport function getNextCronDate(expression: string, currentDate?: Date): Date {\n\ttry {\n\t\tconst interval = CronExpressionParser.parse(expression, {\n\t\t\tcurrentDate: currentDate ?? new Date(),\n\t\t});\n\t\treturn interval.next().toDate();\n\t} catch (error) {\n\t\thandleCronParseError(expression, error);\n\t}\n}\n\n/**\n * Validate a cron expression without calculating the next run date.\n *\n * @param expression - A 5-field cron expression\n * @throws {InvalidCronError} If the cron expression is invalid\n *\n * @example\n * ```typescript\n * validateCronExpression('0 9 * * 1'); // Throws if invalid\n * ```\n */\nexport function validateCronExpression(expression: string): void {\n\ttry {\n\t\tCronExpressionParser.parse(expression);\n\t} catch (error) {\n\t\thandleCronParseError(expression, error);\n\t}\n}\n\nfunction handleCronParseError(expression: string, error: unknown): never {\n\t/* istanbul ignore next -- @preserve cron-parser always throws Error objects */\n\tconst errorMessage = error instanceof Error ? error.message : 'Unknown parsing error';\n\tthrow new InvalidCronError(\n\t\texpression,\n\t\t`Invalid cron expression \"${expression}\": ${errorMessage}. ` +\n\t\t\t'Expected 5-field format: \"minute hour day-of-month month day-of-week\" or predefined expression (e.g. @daily). ' +\n\t\t\t'Example: \"0 9 * * 1\" (every Monday at 9am)',\n\t);\n}\n","import { type Document, type Filter, ObjectId } from 'mongodb';\n\nimport { CursorDirection, type CursorDirectionType, type JobSelector } from '@/jobs';\nimport { InvalidCursorError } from '@/shared';\n\n/**\n * Build a MongoDB query filter from a JobSelector.\n *\n * Translates the high-level `JobSelector` interface into a MongoDB `Filter<Document>`.\n * Handles array values for status (using `$in`) and date range filtering.\n *\n * @param filter - The user-provided job selector\n * @returns A standard MongoDB filter object\n */\nexport function buildSelectorQuery(filter: JobSelector): Filter<Document> {\n\tconst query: Filter<Document> = {};\n\n\tif (filter.name) {\n\t\tquery['name'] = filter.name;\n\t}\n\n\tif (filter.status) {\n\t\tif (Array.isArray(filter.status)) {\n\t\t\tquery['status'] = { $in: filter.status };\n\t\t} else {\n\t\t\tquery['status'] = filter.status;\n\t\t}\n\t}\n\n\tif (filter.olderThan || filter.newerThan) {\n\t\tquery['createdAt'] = {};\n\t\tif (filter.olderThan) {\n\t\t\tquery['createdAt'].$lt = filter.olderThan;\n\t\t}\n\t\tif (filter.newerThan) {\n\t\t\tquery['createdAt'].$gt = filter.newerThan;\n\t\t}\n\t}\n\n\treturn query;\n}\n\n/**\n * Encode an ObjectId and direction into an opaque cursor string.\n *\n * Format: `prefix` + `base64url(objectId)`\n * Prefix: 'F' (forward) or 'B' (backward)\n *\n * @param id - The job ID to use as the cursor anchor (exclusive)\n * @param direction - 'forward' or 'backward'\n * @returns Base64url-encoded cursor string\n */\nexport function encodeCursor(id: ObjectId, direction: CursorDirectionType): string {\n\tconst prefix = direction === 'forward' ? 'F' : 'B';\n\tconst buffer = Buffer.from(id.toHexString(), 'hex');\n\n\treturn prefix + buffer.toString('base64url');\n}\n\n/**\n * Decode an opaque cursor string into an ObjectId and direction.\n *\n * Validates format and returns the components.\n *\n * @param cursor - The opaque cursor string\n * @returns The decoded ID and direction\n * @throws {InvalidCursorError} If the cursor format is invalid or ID is malformed\n */\nexport function decodeCursor(cursor: string): {\n\tid: ObjectId;\n\tdirection: CursorDirectionType;\n} {\n\tif (!cursor || cursor.length < 2) {\n\t\tthrow new InvalidCursorError('Cursor is empty or too short');\n\t}\n\n\tconst prefix = cursor.charAt(0);\n\tconst payload = cursor.slice(1);\n\n\tlet direction: CursorDirectionType;\n\n\tif (prefix === 'F') {\n\t\tdirection = CursorDirection.FORWARD;\n\t} else if (prefix === 'B') {\n\t\tdirection = CursorDirection.BACKWARD;\n\t} else {\n\t\tthrow new InvalidCursorError(`Invalid cursor prefix: ${prefix}`);\n\t}\n\n\ttry {\n\t\tconst buffer = Buffer.from(payload, 'base64url');\n\t\tconst hex = buffer.toString('hex');\n\t\t// standard ObjectID is 12 bytes = 24 hex chars\n\t\tif (hex.length !== 24) {\n\t\t\tthrow new InvalidCursorError('Invalid length');\n\t\t}\n\n\t\tconst id = new ObjectId(hex);\n\n\t\treturn { id, direction };\n\t} catch (error) {\n\t\tif (error instanceof InvalidCursorError) {\n\t\t\tthrow error;\n\t\t}\n\t\tthrow new InvalidCursorError('Invalid cursor payload');\n\t}\n}\n","import type { ChangeStream, ChangeStreamDocument, Document } from 'mongodb';\n\nimport { JobStatus } from '@/jobs';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for MongoDB Change Stream lifecycle.\n *\n * Provides real-time job notifications when available, with automatic\n * reconnection and graceful fallback to polling-only mode.\n *\n * @internal Not part of public API.\n */\nexport class ChangeStreamHandler {\n\t/** MongoDB Change Stream for real-time job notifications */\n\tprivate changeStream: ChangeStream | null = null;\n\n\t/** Number of consecutive reconnection attempts */\n\tprivate reconnectAttempts = 0;\n\n\t/** Maximum reconnection attempts before falling back to polling-only mode */\n\tprivate readonly maxReconnectAttempts = 3;\n\n\t/** Debounce timer for change stream event processing */\n\tprivate debounceTimer: ReturnType<typeof setTimeout> | null = null;\n\n\t/** Timer ID for reconnection with exponential backoff */\n\tprivate reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n\n\t/** Whether the scheduler is currently using change streams */\n\tprivate usingChangeStreams = false;\n\n\tconstructor(\n\t\tprivate readonly ctx: SchedulerContext,\n\t\tprivate readonly onPoll: () => Promise<void>,\n\t) {}\n\n\t/**\n\t * Set up MongoDB Change Stream for real-time job notifications.\n\t *\n\t * Change streams provide instant notifications when jobs are inserted or when\n\t * job status changes to pending (e.g., after a retry). This eliminates the\n\t * polling delay for reactive job processing.\n\t *\n\t * The change stream watches for:\n\t * - Insert operations (new jobs)\n\t * - Update operations where status field changes\n\t *\n\t * If change streams are unavailable (e.g., standalone MongoDB), the system\n\t * gracefully falls back to polling-only mode.\n\t */\n\tsetup(): void {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\t// Create change stream with pipeline to filter relevant events\n\t\t\tconst pipeline = [\n\t\t\t\t{\n\t\t\t\t\t$match: {\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{ operationType: 'insert' },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\toperationType: 'update',\n\t\t\t\t\t\t\t\t'updateDescription.updatedFields.status': { $exists: true },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\n\t\t\tthis.changeStream = this.ctx.collection.watch(pipeline, {\n\t\t\t\tfullDocument: 'updateLookup',\n\t\t\t});\n\n\t\t\t// Handle change events\n\t\t\tthis.changeStream.on('change', (change) => {\n\t\t\t\tthis.handleEvent(change);\n\t\t\t});\n\n\t\t\t// Handle errors with reconnection\n\t\t\tthis.changeStream.on('error', (error: Error) => {\n\t\t\t\tthis.ctx.emit('changestream:error', { error });\n\t\t\t\tthis.handleError(error);\n\t\t\t});\n\n\t\t\t// Mark as connected\n\t\t\tthis.usingChangeStreams = true;\n\t\t\tthis.reconnectAttempts = 0;\n\t\t\tthis.ctx.emit('changestream:connected', undefined);\n\t\t} catch (error) {\n\t\t\t// Change streams not available (e.g., standalone MongoDB)\n\t\t\tthis.usingChangeStreams = false;\n\t\t\tconst reason = error instanceof Error ? error.message : 'Unknown error';\n\t\t\tthis.ctx.emit('changestream:fallback', { reason });\n\t\t}\n\t}\n\n\t/**\n\t * Handle a change stream event by triggering a debounced poll.\n\t *\n\t * Events are debounced to prevent \"claim storms\" when multiple changes arrive\n\t * in rapid succession (e.g., bulk job inserts). A 100ms debounce window\n\t * collects multiple events and triggers a single poll.\n\t *\n\t * @param change - The change stream event document\n\t */\n\thandleEvent(change: ChangeStreamDocument<Document>): void {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Trigger poll on insert (new job) or update where status changes\n\t\tconst isInsert = change.operationType === 'insert';\n\t\tconst isUpdate = change.operationType === 'update';\n\n\t\t// Get fullDocument if available (for insert or with updateLookup option)\n\t\tconst fullDocument = 'fullDocument' in change ? change.fullDocument : undefined;\n\t\tconst isPendingStatus = fullDocument?.['status'] === JobStatus.PENDING;\n\n\t\t// For inserts: always trigger since new pending jobs need processing\n\t\t// For updates: trigger if status changed to pending (retry/release scenario)\n\t\tconst shouldTrigger = isInsert || (isUpdate && isPendingStatus);\n\n\t\tif (shouldTrigger) {\n\t\t\t// Debounce poll triggers to avoid claim storms\n\t\t\tif (this.debounceTimer) {\n\t\t\t\tclearTimeout(this.debounceTimer);\n\t\t\t}\n\n\t\t\tthis.debounceTimer = setTimeout(() => {\n\t\t\t\tthis.debounceTimer = null;\n\t\t\t\tthis.onPoll().catch((error: unknown) => {\n\t\t\t\t\tthis.ctx.emit('job:error', { error: error as Error });\n\t\t\t\t});\n\t\t\t}, 100);\n\t\t}\n\t}\n\n\t/**\n\t * Handle change stream errors with exponential backoff reconnection.\n\t *\n\t * Attempts to reconnect up to `maxReconnectAttempts` times with\n\t * exponential backoff (base 1000ms). After exhausting retries, falls back to\n\t * polling-only mode.\n\t *\n\t * @param error - The error that caused the change stream failure\n\t */\n\thandleError(error: Error): void {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.reconnectAttempts++;\n\n\t\tif (this.reconnectAttempts > this.maxReconnectAttempts) {\n\t\t\t// Fall back to polling-only mode\n\t\t\tthis.usingChangeStreams = false;\n\n\t\t\tif (this.reconnectTimer) {\n\t\t\t\tclearTimeout(this.reconnectTimer);\n\t\t\t\tthis.reconnectTimer = null;\n\t\t\t}\n\n\t\t\tif (this.changeStream) {\n\t\t\t\tthis.changeStream.close().catch(() => {});\n\t\t\t\tthis.changeStream = null;\n\t\t\t}\n\n\t\t\tthis.ctx.emit('changestream:fallback', {\n\t\t\t\treason: `Exhausted ${this.maxReconnectAttempts} reconnection attempts: ${error.message}`,\n\t\t\t});\n\n\t\t\treturn;\n\t\t}\n\n\t\t// Exponential backoff: 1s, 2s, 4s\n\t\tconst delay = 2 ** (this.reconnectAttempts - 1) * 1000;\n\n\t\t// Clear any existing reconnect timer before scheduling a new one\n\t\tif (this.reconnectTimer) {\n\t\t\tclearTimeout(this.reconnectTimer);\n\t\t}\n\n\t\tthis.reconnectTimer = setTimeout(() => {\n\t\t\tthis.reconnectTimer = null;\n\t\t\tif (this.ctx.isRunning()) {\n\t\t\t\t// Close existing change stream before reconnecting\n\t\t\t\tif (this.changeStream) {\n\t\t\t\t\tthis.changeStream.close().catch(() => {});\n\t\t\t\t\tthis.changeStream = null;\n\t\t\t\t}\n\t\t\t\tthis.setup();\n\t\t\t}\n\t\t}, delay);\n\t}\n\n\t/**\n\t * Close the change stream cursor and emit closed event.\n\t */\n\tasync close(): Promise<void> {\n\t\t// Clear debounce timer\n\t\tif (this.debounceTimer) {\n\t\t\tclearTimeout(this.debounceTimer);\n\t\t\tthis.debounceTimer = null;\n\t\t}\n\n\t\t// Clear reconnection timer\n\t\tif (this.reconnectTimer) {\n\t\t\tclearTimeout(this.reconnectTimer);\n\t\t\tthis.reconnectTimer = null;\n\t\t}\n\n\t\tif (this.changeStream) {\n\t\t\ttry {\n\t\t\t\tawait this.changeStream.close();\n\t\t\t} catch {\n\t\t\t\t// Ignore close errors during shutdown\n\t\t\t}\n\t\t\tthis.changeStream = null;\n\n\t\t\tif (this.usingChangeStreams) {\n\t\t\t\tthis.ctx.emit('changestream:closed', undefined);\n\t\t\t}\n\t\t}\n\n\t\tthis.usingChangeStreams = false;\n\t\tthis.reconnectAttempts = 0;\n\t}\n\n\t/**\n\t * Check if change streams are currently active.\n\t */\n\tisActive(): boolean {\n\t\treturn this.usingChangeStreams;\n\t}\n}\n","import { ObjectId, type WithId } from 'mongodb';\n\nimport {\n\ttype BulkOperationResult,\n\ttype Job,\n\ttype JobSelector,\n\tJobStatus,\n\ttype PersistedJob,\n} from '@/jobs';\nimport { buildSelectorQuery } from '@/scheduler';\nimport { JobStateError } from '@/shared';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job lifecycle management operations.\n *\n * Provides atomic state transitions (cancel, retry, reschedule) and deletion.\n * Emits appropriate events on each operation.\n *\n * @internal Not part of public API - use Monque class methods instead.\n */\nexport class JobManager {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Cancel a pending or scheduled job.\n\t *\n\t * Sets the job status to 'cancelled' and emits a 'job:cancelled' event.\n\t * If the job is already cancelled, this is a no-op and returns the job.\n\t * Cannot cancel jobs that are currently 'processing', 'completed', or 'failed'.\n\t *\n\t * @param jobId - The ID of the job to cancel\n\t * @returns The cancelled job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for cancellation\n\t *\n\t * @example Cancel a pending job\n\t * ```typescript\n\t * const job = await monque.enqueue('report', { type: 'daily' });\n\t * await monque.cancelJob(job._id.toString());\n\t * ```\n\t */\n\tasync cancelJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tif (!ObjectId.isValid(jobId)) return null;\n\n\t\tconst _id = new ObjectId(jobId);\n\n\t\t// Fetch job first to allow emitting the full job object in the event\n\t\tconst jobDoc = await this.ctx.collection.findOne({ _id });\n\t\tif (!jobDoc) return null;\n\n\t\tconst currentJob = jobDoc as unknown as WithId<Job>;\n\n\t\tif (currentJob.status === JobStatus.CANCELLED) {\n\t\t\treturn this.ctx.documentToPersistedJob(currentJob);\n\t\t}\n\n\t\tif (currentJob.status !== JobStatus.PENDING) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t`Cannot cancel job in status '${currentJob.status}'`,\n\t\t\t\tjobId,\n\t\t\t\tcurrentJob.status,\n\t\t\t\t'cancel',\n\t\t\t);\n\t\t}\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{ _id, status: JobStatus.PENDING },\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.CANCELLED,\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ returnDocument: 'after' },\n\t\t);\n\n\t\tif (!result) {\n\t\t\t// Race condition: job changed state between check and update\n\t\t\tthrow new JobStateError(\n\t\t\t\t'Job status changed during cancellation attempt',\n\t\t\t\tjobId,\n\t\t\t\t'unknown',\n\t\t\t\t'cancel',\n\t\t\t);\n\t\t}\n\n\t\tconst job = this.ctx.documentToPersistedJob(result);\n\t\tthis.ctx.emit('job:cancelled', { job });\n\t\treturn job;\n\t}\n\n\t/**\n\t * Retry a failed or cancelled job.\n\t *\n\t * Resets the job to 'pending' status, clears failure count/reason, and sets\n\t * nextRunAt to now (immediate retry). Emits a 'job:retried' event.\n\t *\n\t * @param jobId - The ID of the job to retry\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for retry (must be failed or cancelled)\n\t *\n\t * @example Retry a failed job\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * console.log(`Job ${job._id} failed, retrying manually...`);\n\t * await monque.retryJob(job._id.toString());\n\t * });\n\t * ```\n\t */\n\tasync retryJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tif (!ObjectId.isValid(jobId)) return null;\n\n\t\tconst _id = new ObjectId(jobId);\n\t\tconst currentJob = await this.ctx.collection.findOne({ _id });\n\n\t\tif (!currentJob) return null;\n\n\t\tif (currentJob['status'] !== JobStatus.FAILED && currentJob['status'] !== JobStatus.CANCELLED) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t`Cannot retry job in status '${currentJob['status']}'`,\n\t\t\t\tjobId,\n\t\t\t\tcurrentJob['status'],\n\t\t\t\t'retry',\n\t\t\t);\n\t\t}\n\n\t\tconst previousStatus = currentJob['status'] as 'failed' | 'cancelled';\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id,\n\t\t\t\tstatus: { $in: [JobStatus.FAILED, JobStatus.CANCELLED] },\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\tfailCount: 0,\n\t\t\t\t\tnextRunAt: new Date(),\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t\t$unset: {\n\t\t\t\t\tfailReason: '',\n\t\t\t\t\tlockedAt: '',\n\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ returnDocument: 'after' },\n\t\t);\n\n\t\tif (!result) {\n\t\t\tthrow new JobStateError('Job status changed during retry attempt', jobId, 'unknown', 'retry');\n\t\t}\n\n\t\tconst job = this.ctx.documentToPersistedJob(result);\n\t\tthis.ctx.emit('job:retried', { job, previousStatus });\n\t\treturn job;\n\t}\n\n\t/**\n\t * Reschedule a pending job to run at a different time.\n\t *\n\t * Only works for jobs in 'pending' status.\n\t *\n\t * @param jobId - The ID of the job to reschedule\n\t * @param runAt - The new Date when the job should run\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is not in pending state\n\t *\n\t * @example Delay a job by 1 hour\n\t * ```typescript\n\t * const nextHour = new Date(Date.now() + 60 * 60 * 1000);\n\t * await monque.rescheduleJob(jobId, nextHour);\n\t * ```\n\t */\n\tasync rescheduleJob(jobId: string, runAt: Date): Promise<PersistedJob<unknown> | null> {\n\t\tif (!ObjectId.isValid(jobId)) return null;\n\n\t\tconst _id = new ObjectId(jobId);\n\t\tconst currentJobDoc = await this.ctx.collection.findOne({ _id });\n\n\t\tif (!currentJobDoc) return null;\n\n\t\tconst currentJob = currentJobDoc as unknown as WithId<Job>;\n\n\t\tif (currentJob.status !== JobStatus.PENDING) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t`Cannot reschedule job in status '${currentJob.status}'`,\n\t\t\t\tjobId,\n\t\t\t\tcurrentJob.status,\n\t\t\t\t'reschedule',\n\t\t\t);\n\t\t}\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{ _id, status: JobStatus.PENDING },\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tnextRunAt: runAt,\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ returnDocument: 'after' },\n\t\t);\n\n\t\tif (!result) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t'Job status changed during reschedule attempt',\n\t\t\t\tjobId,\n\t\t\t\t'unknown',\n\t\t\t\t'reschedule',\n\t\t\t);\n\t\t}\n\n\t\treturn this.ctx.documentToPersistedJob(result);\n\t}\n\n\t/**\n\t * Permanently delete a job.\n\t *\n\t * This action is irreversible. Emits a 'job:deleted' event upon success.\n\t * Can delete a job in any state.\n\t *\n\t * @param jobId - The ID of the job to delete\n\t * @returns true if deleted, false if job not found\n\t *\n\t * @example Delete a cleanup job\n\t * ```typescript\n\t * const deleted = await monque.deleteJob(jobId);\n\t * if (deleted) {\n\t * console.log('Job permanently removed');\n\t * }\n\t * ```\n\t */\n\tasync deleteJob(jobId: string): Promise<boolean> {\n\t\tif (!ObjectId.isValid(jobId)) return false;\n\n\t\tconst _id = new ObjectId(jobId);\n\n\t\tconst result = await this.ctx.collection.deleteOne({ _id });\n\n\t\tif (result.deletedCount > 0) {\n\t\t\tthis.ctx.emit('job:deleted', { jobId });\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Bulk Operations\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Cancel multiple jobs matching the given filter.\n\t *\n\t * Only cancels jobs in 'pending' status. Jobs in other states are collected\n\t * as errors in the result. Emits a 'jobs:cancelled' event with the IDs of\n\t * successfully cancelled jobs.\n\t *\n\t * @param filter - Selector for which jobs to cancel (name, status, date range)\n\t * @returns Result with count of cancelled jobs and any errors encountered\n\t *\n\t * @example Cancel all pending jobs for a queue\n\t * ```typescript\n\t * const result = await monque.cancelJobs({\n\t * name: 'email-queue',\n\t * status: 'pending'\n\t * });\n\t * console.log(`Cancelled ${result.count} jobs`);\n\t * ```\n\t */\n\tasync cancelJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tconst baseQuery = buildSelectorQuery(filter);\n\t\tconst errors: Array<{ jobId: string; error: string }> = [];\n\t\tconst cancelledIds: string[] = [];\n\n\t\t// Find all matching jobs and stream them to avoid memory pressure\n\t\tconst cursor = this.ctx.collection.find(baseQuery);\n\n\t\tfor await (const doc of cursor) {\n\t\t\tconst job = doc as unknown as WithId<Job>;\n\t\t\tconst jobId = job._id.toString();\n\n\t\t\tif (job.status !== JobStatus.PENDING && job.status !== JobStatus.CANCELLED) {\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: `Cannot cancel job in status '${job.status}'`,\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Skip already cancelled jobs (idempotent)\n\t\t\tif (job.status === JobStatus.CANCELLED) {\n\t\t\t\tcancelledIds.push(jobId);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Atomically update to cancelled\n\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t{ _id: job._id, status: JobStatus.PENDING },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.CANCELLED,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ returnDocument: 'after' },\n\t\t\t);\n\n\t\t\tif (result) {\n\t\t\t\tcancelledIds.push(jobId);\n\t\t\t} else {\n\t\t\t\t// Race condition: status changed\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: 'Job status changed during cancellation',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (cancelledIds.length > 0) {\n\t\t\tthis.ctx.emit('jobs:cancelled', {\n\t\t\t\tjobIds: cancelledIds,\n\t\t\t\tcount: cancelledIds.length,\n\t\t\t});\n\t\t}\n\n\t\treturn {\n\t\t\tcount: cancelledIds.length,\n\t\t\terrors,\n\t\t};\n\t}\n\n\t/**\n\t * Retry multiple jobs matching the given filter.\n\t *\n\t * Only retries jobs in 'failed' or 'cancelled' status. Jobs in other states\n\t * are collected as errors in the result. Emits a 'jobs:retried' event with\n\t * the IDs of successfully retried jobs.\n\t *\n\t * @param filter - Selector for which jobs to retry (name, status, date range)\n\t * @returns Result with count of retried jobs and any errors encountered\n\t *\n\t * @example Retry all failed jobs\n\t * ```typescript\n\t * const result = await monque.retryJobs({\n\t * status: 'failed'\n\t * });\n\t * console.log(`Retried ${result.count} jobs`);\n\t * ```\n\t */\n\tasync retryJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tconst baseQuery = buildSelectorQuery(filter);\n\t\tconst errors: Array<{ jobId: string; error: string }> = [];\n\t\tconst retriedIds: string[] = [];\n\n\t\tconst cursor = this.ctx.collection.find(baseQuery);\n\n\t\tfor await (const doc of cursor) {\n\t\t\tconst job = doc as unknown as WithId<Job>;\n\t\t\tconst jobId = job._id.toString();\n\n\t\t\tif (job.status !== JobStatus.FAILED && job.status !== JobStatus.CANCELLED) {\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: `Cannot retry job in status '${job.status}'`,\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t{\n\t\t\t\t\t_id: job._id,\n\t\t\t\t\tstatus: { $in: [JobStatus.FAILED, JobStatus.CANCELLED] },\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\t\tfailCount: 0,\n\t\t\t\t\t\tnextRunAt: new Date(),\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tfailReason: '',\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ returnDocument: 'after' },\n\t\t\t);\n\n\t\t\tif (result) {\n\t\t\t\tretriedIds.push(jobId);\n\t\t\t} else {\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: 'Job status changed during retry attempt',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (retriedIds.length > 0) {\n\t\t\tthis.ctx.emit('jobs:retried', {\n\t\t\t\tjobIds: retriedIds,\n\t\t\t\tcount: retriedIds.length,\n\t\t\t});\n\t\t}\n\n\t\treturn {\n\t\t\tcount: retriedIds.length,\n\t\t\terrors,\n\t\t};\n\t}\n\n\t/**\n\t * Delete multiple jobs matching the given filter.\n\t *\n\t * Deletes jobs in any status. Uses a batch delete for efficiency.\n\t * Emits a 'jobs:deleted' event with the count of deleted jobs.\n\t * Does not emit individual 'job:deleted' events to avoid noise.\n\t *\n\t * @param filter - Selector for which jobs to delete (name, status, date range)\n\t * @returns Result with count of deleted jobs (errors array always empty for delete)\n\t *\n\t * @example Delete old completed jobs\n\t * ```typescript\n\t * const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);\n\t * const result = await monque.deleteJobs({\n\t * status: 'completed',\n\t * olderThan: weekAgo\n\t * });\n\t * console.log(`Deleted ${result.count} jobs`);\n\t * ```\n\t */\n\tasync deleteJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tconst query = buildSelectorQuery(filter);\n\n\t\t// Use deleteMany for efficiency\n\t\tconst result = await this.ctx.collection.deleteMany(query);\n\n\t\tif (result.deletedCount > 0) {\n\t\t\tthis.ctx.emit('jobs:deleted', { count: result.deletedCount });\n\t\t}\n\n\t\treturn {\n\t\t\tcount: result.deletedCount,\n\t\t\terrors: [],\n\t\t};\n\t}\n}\n","import { isPersistedJob, type Job, JobStatus, type PersistedJob } from '@/jobs';\nimport { calculateBackoff, getNextCronDate } from '@/shared';\nimport type { WorkerRegistration } from '@/workers';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job processing and execution.\n *\n * Manages the poll loop, atomic job acquisition, handler execution,\n * and job completion/failure with exponential backoff retry logic.\n *\n * @internal Not part of public API.\n */\nexport class JobProcessor {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Poll for available jobs and process them.\n\t *\n\t * Called at regular intervals (configured by `pollInterval`). For each registered worker,\n\t * attempts to acquire jobs up to the worker's available concurrency slots.\n\t * Aborts early if the scheduler is stopping (`isRunning` is false).\n\t */\n\tasync poll(): Promise<void> {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\tfor (const [name, worker] of this.ctx.workers) {\n\t\t\t// Check if worker has capacity\n\t\t\tconst availableSlots = worker.concurrency - worker.activeJobs.size;\n\n\t\t\tif (availableSlots <= 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Try to acquire jobs up to available slots\n\t\t\tfor (let i = 0; i < availableSlots; i++) {\n\t\t\t\tif (!this.ctx.isRunning()) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst job = await this.acquireJob(name);\n\n\t\t\t\tif (job) {\n\t\t\t\t\tthis.processJob(job, worker).catch((error: unknown) => {\n\t\t\t\t\t\tthis.ctx.emit('job:error', { error: error as Error, job });\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t// No more jobs available for this worker\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Atomically acquire a pending job for processing using the claimedBy pattern.\n\t *\n\t * Uses MongoDB's `findOneAndUpdate` with atomic operations to ensure only one scheduler\n\t * instance can claim a job. The query ensures the job is:\n\t * - In pending status\n\t * - Has nextRunAt <= now\n\t * - Is not claimed by another instance (claimedBy is null/undefined)\n\t *\n\t * Returns `null` immediately if scheduler is stopping (`isRunning` is false).\n\t *\n\t * @param name - The job type to acquire\n\t * @returns The acquired job with updated status, claimedBy, and heartbeat info, or `null` if no jobs available\n\t */\n\tasync acquireJob(name: string): Promise<PersistedJob | null> {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst now = new Date();\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{\n\t\t\t\tname,\n\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\tnextRunAt: { $lte: now },\n\t\t\t\t$or: [{ claimedBy: null }, { claimedBy: { $exists: false } }],\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.PROCESSING,\n\t\t\t\t\tclaimedBy: this.ctx.instanceId,\n\t\t\t\t\tlockedAt: now,\n\t\t\t\t\tlastHeartbeat: now,\n\t\t\t\t\theartbeatInterval: this.ctx.options.heartbeatInterval,\n\t\t\t\t\tupdatedAt: now,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tsort: { nextRunAt: 1 },\n\t\t\t\treturnDocument: 'after',\n\t\t\t},\n\t\t);\n\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (!result) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn this.ctx.documentToPersistedJob(result);\n\t}\n\n\t/**\n\t * Execute a job using its registered worker handler.\n\t *\n\t * Tracks the job as active during processing, emits lifecycle events, and handles\n\t * both success and failure cases. On success, calls `completeJob()`. On failure,\n\t * calls `failJob()` which implements exponential backoff retry logic.\n\t *\n\t * @param job - The job to process\n\t * @param worker - The worker registration containing the handler and active job tracking\n\t */\n\tasync processJob(job: PersistedJob, worker: WorkerRegistration): Promise<void> {\n\t\tconst jobId = job._id.toString();\n\t\tworker.activeJobs.set(jobId, job);\n\n\t\tconst startTime = Date.now();\n\t\tthis.ctx.emit('job:start', job);\n\n\t\ttry {\n\t\t\tawait worker.handler(job);\n\n\t\t\t// Job completed successfully\n\t\t\tconst duration = Date.now() - startTime;\n\t\t\tawait this.completeJob(job);\n\t\t\tthis.ctx.emit('job:complete', { job, duration });\n\t\t} catch (error) {\n\t\t\t// Job failed\n\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\tawait this.failJob(job, err);\n\n\t\t\tconst willRetry = job.failCount + 1 < this.ctx.options.maxRetries;\n\t\t\tthis.ctx.emit('job:fail', { job, error: err, willRetry });\n\t\t} finally {\n\t\t\tworker.activeJobs.delete(jobId);\n\t\t}\n\t}\n\n\t/**\n\t * Mark a job as completed successfully.\n\t *\n\t * For recurring jobs (with `repeatInterval`), schedules the next run based on the cron\n\t * expression and resets `failCount` to 0. For one-time jobs, sets status to `completed`.\n\t * Clears `lockedAt` and `failReason` fields in both cases.\n\t *\n\t * @param job - The job that completed successfully\n\t */\n\tasync completeJob(job: Job): Promise<void> {\n\t\tif (!isPersistedJob(job)) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (job.repeatInterval) {\n\t\t\t// Recurring job - schedule next run\n\t\t\tconst nextRunAt = getNextCronDate(job.repeatInterval);\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\t\tnextRunAt,\n\t\t\t\t\t\tfailCount: 0,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t\tfailReason: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t} else {\n\t\t\t// One-time job - mark as completed\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.COMPLETED,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t\tfailReason: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t\tjob.status = JobStatus.COMPLETED;\n\t\t}\n\t}\n\n\t/**\n\t * Handle job failure with exponential backoff retry logic.\n\t *\n\t * Increments `failCount` and calculates next retry time using exponential backoff:\n\t * `nextRunAt = 2^failCount × baseRetryInterval` (capped by optional `maxBackoffDelay`).\n\t *\n\t * If `failCount >= maxRetries`, marks job as permanently `failed`. Otherwise, resets\n\t * to `pending` status for retry. Stores error message in `failReason` field.\n\t *\n\t * @param job - The job that failed\n\t * @param error - The error that caused the failure\n\t */\n\tasync failJob(job: Job, error: Error): Promise<void> {\n\t\tif (!isPersistedJob(job)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newFailCount = job.failCount + 1;\n\n\t\tif (newFailCount >= this.ctx.options.maxRetries) {\n\t\t\t// Permanent failure\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.FAILED,\n\t\t\t\t\t\tfailCount: newFailCount,\n\t\t\t\t\t\tfailReason: error.message,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t} else {\n\t\t\t// Schedule retry with exponential backoff\n\t\t\tconst nextRunAt = calculateBackoff(\n\t\t\t\tnewFailCount,\n\t\t\t\tthis.ctx.options.baseRetryInterval,\n\t\t\t\tthis.ctx.options.maxBackoffDelay,\n\t\t\t);\n\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\t\tfailCount: newFailCount,\n\t\t\t\t\t\tfailReason: error.message,\n\t\t\t\t\t\tnextRunAt,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Update heartbeats for all jobs claimed by this scheduler instance.\n\t *\n\t * This method runs periodically while the scheduler is running to indicate\n\t * that jobs are still being actively processed.\n\t *\n\t * `lastHeartbeat` is primarily an observability signal (monitoring/debugging).\n\t * Stale recovery is based on `lockedAt` + `lockTimeout`.\n\t */\n\tasync updateHeartbeats(): Promise<void> {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst now = new Date();\n\n\t\tawait this.ctx.collection.updateMany(\n\t\t\t{\n\t\t\t\tclaimedBy: this.ctx.instanceId,\n\t\t\t\tstatus: JobStatus.PROCESSING,\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tlastHeartbeat: now,\n\t\t\t\t\tupdatedAt: now,\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n}\n","import type { Document, Filter, ObjectId, WithId } from 'mongodb';\n\nimport {\n\tCursorDirection,\n\ttype CursorDirectionType,\n\ttype CursorOptions,\n\ttype CursorPage,\n\ttype GetJobsFilter,\n\ttype JobSelector,\n\tJobStatus,\n\ttype PersistedJob,\n\ttype QueueStats,\n} from '@/jobs';\nimport { AggregationTimeoutError, ConnectionError } from '@/shared';\n\nimport { buildSelectorQuery, decodeCursor, encodeCursor } from '../helpers.js';\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job query operations.\n *\n * Provides read-only access to jobs with filtering and cursor-based pagination.\n * All queries use efficient index-backed access patterns.\n *\n * @internal Not part of public API - use Monque class methods instead.\n */\nexport class JobQueryService {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Get a single job by its MongoDB ObjectId.\n\t *\n\t * Useful for retrieving job details when you have a job ID from events,\n\t * logs, or stored references.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param id - The job's ObjectId\n\t * @returns Promise resolving to the job if found, null otherwise\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Look up job from event\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * // Later, retrieve the job to check its status\n\t * const currentJob = await monque.getJob(job._id);\n\t * console.log(`Job status: ${currentJob?.status}`);\n\t * });\n\t * ```\n\t *\n\t * @example Admin endpoint\n\t * ```typescript\n\t * app.get('/jobs/:id', async (req, res) => {\n\t * const job = await monque.getJob(new ObjectId(req.params.id));\n\t * if (!job) {\n\t * return res.status(404).json({ error: 'Job not found' });\n\t * }\n\t * res.json(job);\n\t * });\n\t * ```\n\t */\n\tasync getJob<T = unknown>(id: ObjectId): Promise<PersistedJob<T> | null> {\n\t\ttry {\n\t\t\tconst doc = await this.ctx.collection.findOne({ _id: id });\n\t\t\tif (!doc) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn this.ctx.documentToPersistedJob<T>(doc as WithId<Document>);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during getJob';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to get job: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Query jobs from the queue with optional filters.\n\t *\n\t * Provides read-only access to job data for monitoring, debugging, and\n\t * administrative purposes. Results are ordered by `nextRunAt` ascending.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param filter - Optional filter criteria\n\t * @returns Promise resolving to array of matching jobs\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Get all pending jobs\n\t * ```typescript\n\t * const pendingJobs = await monque.getJobs({ status: JobStatus.PENDING });\n\t * console.log(`${pendingJobs.length} jobs waiting`);\n\t * ```\n\t *\n\t * @example Get failed email jobs\n\t * ```typescript\n\t * const failedEmails = await monque.getJobs({\n\t * name: 'send-email',\n\t * status: JobStatus.FAILED,\n\t * });\n\t * for (const job of failedEmails) {\n\t * console.error(`Job ${job._id} failed: ${job.failReason}`);\n\t * }\n\t * ```\n\t *\n\t * @example Paginated job listing\n\t * ```typescript\n\t * const page1 = await monque.getJobs({ limit: 50, skip: 0 });\n\t * const page2 = await monque.getJobs({ limit: 50, skip: 50 });\n\t * ```\n\t *\n\t * @example Use with type guards from @monque/core\n\t * ```typescript\n\t * import { isPendingJob, isRecurringJob } from '@monque/core';\n\t *\n\t * const jobs = await monque.getJobs();\n\t * const pendingRecurring = jobs.filter(job => isPendingJob(job) && isRecurringJob(job));\n\t * ```\n\t */\n\tasync getJobs<T = unknown>(filter: GetJobsFilter = {}): Promise<PersistedJob<T>[]> {\n\t\tconst query: Document = {};\n\n\t\tif (filter.name !== undefined) {\n\t\t\tquery['name'] = filter.name;\n\t\t}\n\n\t\tif (filter.status !== undefined) {\n\t\t\tif (Array.isArray(filter.status)) {\n\t\t\t\tquery['status'] = { $in: filter.status };\n\t\t\t} else {\n\t\t\t\tquery['status'] = filter.status;\n\t\t\t}\n\t\t}\n\n\t\tconst limit = filter.limit ?? 100;\n\t\tconst skip = filter.skip ?? 0;\n\n\t\ttry {\n\t\t\tconst cursor = this.ctx.collection.find(query).sort({ nextRunAt: 1 }).skip(skip).limit(limit);\n\n\t\t\tconst docs = await cursor.toArray();\n\t\t\treturn docs.map((doc) => this.ctx.documentToPersistedJob<T>(doc));\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during getJobs';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to query jobs: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Get a paginated list of jobs using opaque cursors.\n\t *\n\t * Provides stable pagination for large job lists. Supports forward and backward\n\t * navigation, filtering, and efficient database access via index-based cursor queries.\n\t *\n\t * @template T - The job data payload type\n\t * @param options - Pagination options (cursor, limit, direction, filter)\n\t * @returns Page of jobs with next/prev cursors\n\t * @throws {InvalidCursorError} If the provided cursor is malformed\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example List pending jobs\n\t * ```typescript\n\t * const page = await monque.getJobsWithCursor({\n\t * limit: 20,\n\t * filter: { status: 'pending' }\n\t * });\n\t * const jobs = page.jobs;\n\t *\n\t * // Get next page\n\t * if (page.hasNextPage) {\n\t * const page2 = await monque.getJobsWithCursor({\n\t * cursor: page.cursor,\n\t * limit: 20\n\t * });\n\t * }\n\t * ```\n\t */\n\tasync getJobsWithCursor<T = unknown>(options: CursorOptions = {}): Promise<CursorPage<T>> {\n\t\tconst limit = options.limit ?? 50;\n\t\t// Default to forward if not specified.\n\t\tconst direction: CursorDirectionType = options.direction ?? CursorDirection.FORWARD;\n\t\tlet anchorId: ObjectId | null = null;\n\n\t\tif (options.cursor) {\n\t\t\tconst decoded = decodeCursor(options.cursor);\n\t\t\tanchorId = decoded.id;\n\t\t}\n\n\t\t// Build base query from filters\n\t\tconst query: Filter<Document> = options.filter ? buildSelectorQuery(options.filter) : {};\n\n\t\t// Add cursor condition to query\n\t\tconst sortDir = direction === CursorDirection.FORWARD ? 1 : -1;\n\n\t\tif (anchorId) {\n\t\t\tif (direction === CursorDirection.FORWARD) {\n\t\t\t\tquery._id = { ...query._id, $gt: anchorId };\n\t\t\t} else {\n\t\t\t\tquery._id = { ...query._id, $lt: anchorId };\n\t\t\t}\n\t\t}\n\n\t\t// Fetch limit + 1 to detect hasNext/hasPrev\n\t\tconst fetchLimit = limit + 1;\n\n\t\t// Sort: Always deterministic.\n\t\tlet docs: WithId<Document>[];\n\t\ttry {\n\t\t\tdocs = await this.ctx.collection\n\t\t\t\t.find(query)\n\t\t\t\t.sort({ _id: sortDir })\n\t\t\t\t.limit(fetchLimit)\n\t\t\t\t.toArray();\n\t\t} catch (error) {\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : 'Unknown error during getJobsWithCursor';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to query jobs with cursor: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\n\t\tlet hasMore = false;\n\t\tif (docs.length > limit) {\n\t\t\thasMore = true;\n\t\t\tdocs.pop(); // Remove the extra item\n\t\t}\n\n\t\tif (direction === CursorDirection.BACKWARD) {\n\t\t\tdocs.reverse();\n\t\t}\n\n\t\tconst jobs = docs.map((doc) => this.ctx.documentToPersistedJob<T>(doc as WithId<Document>));\n\n\t\tlet nextCursor: string | null = null;\n\n\t\tif (jobs.length > 0) {\n\t\t\tconst lastJob = jobs[jobs.length - 1];\n\t\t\t// Check for existence to satisfy strict null checks/noUncheckedIndexedAccess\n\t\t\tif (lastJob) {\n\t\t\t\tnextCursor = encodeCursor(lastJob._id, direction);\n\t\t\t}\n\t\t}\n\n\t\tlet hasNextPage = false;\n\t\tlet hasPreviousPage = false;\n\n\t\t// Determine availability of next/prev pages\n\t\tif (direction === CursorDirection.FORWARD) {\n\t\t\thasNextPage = hasMore;\n\t\t\thasPreviousPage = !!anchorId;\n\t\t} else {\n\t\t\thasNextPage = !!anchorId;\n\t\t\thasPreviousPage = hasMore;\n\t\t}\n\n\t\treturn {\n\t\t\tjobs,\n\t\t\tcursor: nextCursor,\n\t\t\thasNextPage,\n\t\t\thasPreviousPage,\n\t\t};\n\t}\n\n\t/**\n\t * Get aggregate statistics for the job queue.\n\t *\n\t * Uses MongoDB aggregation pipeline for efficient server-side calculation.\n\t * Returns counts per status and optional average processing duration for completed jobs.\n\t *\n\t * @param filter - Optional filter to scope statistics by job name\n\t * @returns Promise resolving to queue statistics\n\t * @throws {AggregationTimeoutError} If aggregation exceeds 30 second timeout\n\t * @throws {ConnectionError} If database operation fails\n\t *\n\t * @example Get overall queue statistics\n\t * ```typescript\n\t * const stats = await monque.getQueueStats();\n\t * console.log(`Pending: ${stats.pending}, Failed: ${stats.failed}`);\n\t * ```\n\t *\n\t * @example Get statistics for a specific job type\n\t * ```typescript\n\t * const emailStats = await monque.getQueueStats({ name: 'send-email' });\n\t * console.log(`${emailStats.total} email jobs in queue`);\n\t * ```\n\t */\n\tasync getQueueStats(filter?: Pick<JobSelector, 'name'>): Promise<QueueStats> {\n\t\tconst matchStage: Document = {};\n\n\t\tif (filter?.name) {\n\t\t\tmatchStage['name'] = filter.name;\n\t\t}\n\n\t\tconst pipeline: Document[] = [\n\t\t\t// Optional match stage for filtering by name\n\t\t\t...(Object.keys(matchStage).length > 0 ? [{ $match: matchStage }] : []),\n\t\t\t// Facet to calculate counts and avg processing duration in parallel\n\t\t\t{\n\t\t\t\t$facet: {\n\t\t\t\t\t// Count by status\n\t\t\t\t\tstatusCounts: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t_id: '$status',\n\t\t\t\t\t\t\t\tcount: { $sum: 1 },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\t// Calculate average job lifetime for completed jobs.\n\t\t\t\t\t// Uses createdAt → updatedAt (total lifetime = queue wait + processing)\n\t\t\t\t\t// since completeJob() unsets lockedAt, making pure processing time unavailable.\n\t\t\t\t\tavgDuration: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\tstatus: JobStatus.COMPLETED,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t_id: null,\n\t\t\t\t\t\t\t\tavgMs: {\n\t\t\t\t\t\t\t\t\t$avg: {\n\t\t\t\t\t\t\t\t\t\t$subtract: ['$updatedAt', '$createdAt'],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\t// Total count\n\t\t\t\t\ttotal: [{ $count: 'count' }],\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\ttry {\n\t\t\tconst results = await this.ctx.collection.aggregate(pipeline, { maxTimeMS: 30000 }).toArray();\n\n\t\t\tconst result = results[0];\n\n\t\t\t// Initialize with zeros\n\t\t\tconst stats: QueueStats = {\n\t\t\t\tpending: 0,\n\t\t\t\tprocessing: 0,\n\t\t\t\tcompleted: 0,\n\t\t\t\tfailed: 0,\n\t\t\t\tcancelled: 0,\n\t\t\t\ttotal: 0,\n\t\t\t};\n\n\t\t\tif (!result) {\n\t\t\t\treturn stats;\n\t\t\t}\n\n\t\t\t// Map status counts to stats\n\t\t\tconst statusCounts = result['statusCounts'] as Array<{ _id: string; count: number }>;\n\t\t\tfor (const entry of statusCounts) {\n\t\t\t\tconst status = entry._id;\n\t\t\t\tconst count = entry.count;\n\n\t\t\t\tswitch (status) {\n\t\t\t\t\tcase JobStatus.PENDING:\n\t\t\t\t\t\tstats.pending = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.PROCESSING:\n\t\t\t\t\t\tstats.processing = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.COMPLETED:\n\t\t\t\t\t\tstats.completed = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.FAILED:\n\t\t\t\t\t\tstats.failed = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.CANCELLED:\n\t\t\t\t\t\tstats.cancelled = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Extract total\n\t\t\tconst totalResult = result['total'] as Array<{ count: number }>;\n\t\t\tif (totalResult.length > 0 && totalResult[0]) {\n\t\t\t\tstats.total = totalResult[0].count;\n\t\t\t}\n\n\t\t\t// Extract average processing duration\n\t\t\tconst avgDurationResult = result['avgDuration'] as Array<{ avgMs: number }>;\n\t\t\tif (avgDurationResult.length > 0 && avgDurationResult[0]) {\n\t\t\t\tconst avgMs = avgDurationResult[0].avgMs;\n\t\t\t\tif (typeof avgMs === 'number' && !Number.isNaN(avgMs)) {\n\t\t\t\t\tstats.avgProcessingDurationMs = Math.round(avgMs);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn stats;\n\t\t} catch (error) {\n\t\t\t// Check for timeout error\n\t\t\tif (error instanceof Error && error.message.includes('exceeded time limit')) {\n\t\t\t\tthrow new AggregationTimeoutError();\n\t\t\t}\n\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during getQueueStats';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to get queue stats: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n}\n","import type { Document } from 'mongodb';\n\nimport {\n\ttype EnqueueOptions,\n\ttype Job,\n\tJobStatus,\n\ttype PersistedJob,\n\ttype ScheduleOptions,\n} from '@/jobs';\nimport { ConnectionError, getNextCronDate, MonqueError } from '@/shared';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job scheduling operations.\n *\n * Handles enqueueing new jobs, immediate dispatch, and cron scheduling.\n * All operations are atomic and support deduplication via uniqueKey.\n *\n * @internal Not part of public API - use Monque class methods instead.\n */\nexport class JobScheduler {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Enqueue a job for processing.\n\t *\n\t * Jobs are stored in MongoDB and processed by registered workers. Supports\n\t * delayed execution via `runAt` and deduplication via `uniqueKey`.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. Completed or failed jobs don't block new jobs with the same key.\n\t *\n\t * Failed jobs are automatically retried with exponential backoff up to `maxRetries`\n\t * (default: 10 attempts). The delay between retries is calculated as `2^failCount × baseRetryInterval`.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @param options - Scheduling and deduplication options\n\t * @returns Promise resolving to the created or existing job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Basic job enqueueing\n\t * ```typescript\n\t * await monque.enqueue('send-email', {\n\t * to: 'user@example.com',\n\t * subject: 'Welcome!',\n\t * body: 'Thanks for signing up.'\n\t * });\n\t * ```\n\t *\n\t * @example Delayed execution\n\t * ```typescript\n\t * const oneHourLater = new Date(Date.now() + 3600000);\n\t * await monque.enqueue('reminder', { message: 'Check in!' }, {\n\t * runAt: oneHourLater\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicates with unique key\n\t * ```typescript\n\t * await monque.enqueue('sync-user', { userId: '123' }, {\n\t * uniqueKey: 'sync-user-123'\n\t * });\n\t * // Subsequent enqueues with same uniqueKey return existing pending/processing job\n\t * ```\n\t */\n\tasync enqueue<T>(name: string, data: T, options: EnqueueOptions = {}): Promise<PersistedJob<T>> {\n\t\tconst now = new Date();\n\t\tconst job: Omit<Job<T>, '_id'> = {\n\t\t\tname,\n\t\t\tdata,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tnextRunAt: options.runAt ?? now,\n\t\t\tfailCount: 0,\n\t\t\tcreatedAt: now,\n\t\t\tupdatedAt: now,\n\t\t};\n\n\t\tif (options.uniqueKey) {\n\t\t\tjob.uniqueKey = options.uniqueKey;\n\t\t}\n\n\t\ttry {\n\t\t\tif (options.uniqueKey) {\n\t\t\t\t// Use upsert with $setOnInsert for deduplication (scoped by name + uniqueKey)\n\t\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t\t{\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tuniqueKey: options.uniqueKey,\n\t\t\t\t\t\tstatus: { $in: [JobStatus.PENDING, JobStatus.PROCESSING] },\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$setOnInsert: job,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tupsert: true,\n\t\t\t\t\t\treturnDocument: 'after',\n\t\t\t\t\t},\n\t\t\t\t);\n\n\t\t\t\tif (!result) {\n\t\t\t\t\tthrow new ConnectionError('Failed to enqueue job: findOneAndUpdate returned no document');\n\t\t\t\t}\n\n\t\t\t\treturn this.ctx.documentToPersistedJob<T>(result);\n\t\t\t}\n\n\t\t\tconst result = await this.ctx.collection.insertOne(job as Document);\n\n\t\t\treturn { ...job, _id: result.insertedId } as PersistedJob<T>;\n\t\t} catch (error) {\n\t\t\tif (error instanceof ConnectionError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during enqueue';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to enqueue job: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Enqueue a job for immediate processing.\n\t *\n\t * Convenience method equivalent to `enqueue(name, data, { runAt: new Date() })`.\n\t * Jobs are picked up on the next poll cycle (typically within 1 second based on `pollInterval`).\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @returns Promise resolving to the created job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Send email immediately\n\t * ```typescript\n\t * await monque.now('send-email', {\n\t * to: 'admin@example.com',\n\t * subject: 'Alert',\n\t * body: 'Immediate attention required'\n\t * });\n\t * ```\n\t *\n\t * @example Process order in background\n\t * ```typescript\n\t * const order = await createOrder(data);\n\t * await monque.now('process-order', { orderId: order.id });\n\t * return order; // Return immediately, processing happens async\n\t * ```\n\t */\n\tasync now<T>(name: string, data: T): Promise<PersistedJob<T>> {\n\t\treturn this.enqueue(name, data, { runAt: new Date() });\n\t}\n\n\t/**\n\t * Schedule a recurring job with a cron expression.\n\t *\n\t * Creates a job that automatically re-schedules itself based on the cron pattern.\n\t * Uses standard 5-field cron format: minute, hour, day of month, month, day of week.\n\t * Also supports predefined expressions like `@daily`, `@weekly`, `@monthly`, etc.\n\t * After successful completion, the job is reset to `pending` status and scheduled\n\t * for its next run based on the cron expression.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. This prevents duplicate scheduled jobs on application restart.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param cron - Cron expression (5 fields or predefined expression)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler on each run\n\t * @param options - Scheduling options (uniqueKey for deduplication)\n\t * @returns Promise resolving to the created job document with `repeatInterval` set\n\t * @throws {InvalidCronError} If cron expression is invalid\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Hourly cleanup job\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'cleanup-temp-files', {\n\t * directory: '/tmp/uploads'\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicate scheduled jobs with unique key\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'hourly-report', { type: 'sales' }, {\n\t * uniqueKey: 'hourly-report-sales'\n\t * });\n\t * // Subsequent calls with same uniqueKey return existing pending/processing job\n\t * ```\n\t *\n\t * @example Daily report at midnight (using predefined expression)\n\t * ```typescript\n\t * await monque.schedule('@daily', 'daily-report', {\n\t * reportType: 'sales',\n\t * recipients: ['analytics@example.com']\n\t * });\n\t * ```\n\t */\n\tasync schedule<T>(\n\t\tcron: string,\n\t\tname: string,\n\t\tdata: T,\n\t\toptions: ScheduleOptions = {},\n\t): Promise<PersistedJob<T>> {\n\t\t// Validate cron and get next run date (throws InvalidCronError if invalid)\n\t\tconst nextRunAt = getNextCronDate(cron);\n\n\t\tconst now = new Date();\n\t\tconst job: Omit<Job<T>, '_id'> = {\n\t\t\tname,\n\t\t\tdata,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tnextRunAt,\n\t\t\trepeatInterval: cron,\n\t\t\tfailCount: 0,\n\t\t\tcreatedAt: now,\n\t\t\tupdatedAt: now,\n\t\t};\n\n\t\tif (options.uniqueKey) {\n\t\t\tjob.uniqueKey = options.uniqueKey;\n\t\t}\n\n\t\ttry {\n\t\t\tif (options.uniqueKey) {\n\t\t\t\t// Use upsert with $setOnInsert for deduplication (scoped by name + uniqueKey)\n\t\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t\t{\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tuniqueKey: options.uniqueKey,\n\t\t\t\t\t\tstatus: { $in: [JobStatus.PENDING, JobStatus.PROCESSING] },\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$setOnInsert: job,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tupsert: true,\n\t\t\t\t\t\treturnDocument: 'after',\n\t\t\t\t\t},\n\t\t\t\t);\n\n\t\t\t\tif (!result) {\n\t\t\t\t\tthrow new ConnectionError(\n\t\t\t\t\t\t'Failed to schedule job: findOneAndUpdate returned no document',\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn this.ctx.documentToPersistedJob<T>(result);\n\t\t\t}\n\n\t\t\tconst result = await this.ctx.collection.insertOne(job as Document);\n\n\t\t\treturn { ...job, _id: result.insertedId } as PersistedJob<T>;\n\t\t} catch (error) {\n\t\t\tif (error instanceof MonqueError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during schedule';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to schedule job: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n}\n","import { randomUUID } from 'node:crypto';\nimport { EventEmitter } from 'node:events';\nimport type { Collection, Db, DeleteResult, Document, ObjectId, WithId } from 'mongodb';\n\nimport type { MonqueEventMap } from '@/events';\nimport {\n\ttype BulkOperationResult,\n\ttype CursorOptions,\n\ttype CursorPage,\n\ttype EnqueueOptions,\n\ttype GetJobsFilter,\n\ttype Job,\n\ttype JobHandler,\n\ttype JobSelector,\n\tJobStatus,\n\ttype JobStatusType,\n\ttype PersistedJob,\n\ttype QueueStats,\n\ttype ScheduleOptions,\n} from '@/jobs';\nimport { ConnectionError, ShutdownTimeoutError, WorkerRegistrationError } from '@/shared';\nimport type { WorkerOptions, WorkerRegistration } from '@/workers';\n\nimport {\n\tChangeStreamHandler,\n\tJobManager,\n\tJobProcessor,\n\tJobQueryService,\n\tJobScheduler,\n\ttype ResolvedMonqueOptions,\n\ttype SchedulerContext,\n} from './services/index.js';\nimport type { MonqueOptions } from './types.js';\n\n/**\n * Default configuration values\n */\nconst DEFAULTS = {\n\tcollectionName: 'monque_jobs',\n\tpollInterval: 1000,\n\tmaxRetries: 10,\n\tbaseRetryInterval: 1000,\n\tshutdownTimeout: 30000,\n\tdefaultConcurrency: 5,\n\tlockTimeout: 1_800_000, // 30 minutes\n\trecoverStaleJobs: true,\n\theartbeatInterval: 30000, // 30 seconds\n\tretentionInterval: 3600_000, // 1 hour\n} as const;\n\n/**\n * Monque - MongoDB-backed job scheduler\n *\n * A type-safe job scheduler with atomic locking, exponential backoff, cron scheduling,\n * stale job recovery, and event-driven observability. Built on native MongoDB driver.\n *\n * @example Complete lifecycle\n * ```typescript\n * import { Monque } from '@monque/core';\n * import { MongoClient } from 'mongodb';\n *\n * const client = new MongoClient('mongodb://localhost:27017');\n * await client.connect();\n * const db = client.db('myapp');\n *\n * // Create instance with options\n * const monque = new Monque(db, {\n * collectionName: 'jobs',\n * pollInterval: 1000,\n * maxRetries: 10,\n * shutdownTimeout: 30000,\n * });\n *\n * // Initialize (sets up indexes and recovers stale jobs)\n * await monque.initialize();\n *\n * // Register workers with type safety\n * type EmailJob = {\n * to: string;\n * subject: string;\n * body: string;\n * };\n *\n * monque.register<EmailJob>('send-email', async (job) => {\n * await emailService.send(job.data.to, job.data.subject, job.data.body);\n * });\n *\n * // Monitor events for observability\n * monque.on('job:complete', ({ job, duration }) => {\n * logger.info(`Job ${job.name} completed in ${duration}ms`);\n * });\n *\n * monque.on('job:fail', ({ job, error, willRetry }) => {\n * logger.error(`Job ${job.name} failed:`, error);\n * });\n *\n * // Start processing\n * monque.start();\n *\n * // Enqueue jobs\n * await monque.enqueue('send-email', {\n * to: 'user@example.com',\n * subject: 'Welcome!',\n * body: 'Thanks for signing up.'\n * });\n *\n * // Graceful shutdown\n * process.on('SIGTERM', async () => {\n * await monque.stop();\n * await client.close();\n * process.exit(0);\n * });\n * ```\n */\nexport class Monque extends EventEmitter {\n\tprivate readonly db: Db;\n\tprivate readonly options: ResolvedMonqueOptions;\n\tprivate collection: Collection<Document> | null = null;\n\tprivate workers: Map<string, WorkerRegistration> = new Map();\n\tprivate pollIntervalId: ReturnType<typeof setInterval> | null = null;\n\tprivate heartbeatIntervalId: ReturnType<typeof setInterval> | null = null;\n\tprivate cleanupIntervalId: ReturnType<typeof setInterval> | null = null;\n\tprivate isRunning = false;\n\tprivate isInitialized = false;\n\n\t// Internal services (initialized in initialize())\n\tprivate _scheduler: JobScheduler | null = null;\n\tprivate _manager: JobManager | null = null;\n\tprivate _query: JobQueryService | null = null;\n\tprivate _processor: JobProcessor | null = null;\n\tprivate _changeStreamHandler: ChangeStreamHandler | null = null;\n\n\tconstructor(db: Db, options: MonqueOptions = {}) {\n\t\tsuper();\n\t\tthis.db = db;\n\t\tthis.options = {\n\t\t\tcollectionName: options.collectionName ?? DEFAULTS.collectionName,\n\t\t\tpollInterval: options.pollInterval ?? DEFAULTS.pollInterval,\n\t\t\tmaxRetries: options.maxRetries ?? DEFAULTS.maxRetries,\n\t\t\tbaseRetryInterval: options.baseRetryInterval ?? DEFAULTS.baseRetryInterval,\n\t\t\tshutdownTimeout: options.shutdownTimeout ?? DEFAULTS.shutdownTimeout,\n\t\t\tdefaultConcurrency: options.defaultConcurrency ?? DEFAULTS.defaultConcurrency,\n\t\t\tlockTimeout: options.lockTimeout ?? DEFAULTS.lockTimeout,\n\t\t\trecoverStaleJobs: options.recoverStaleJobs ?? DEFAULTS.recoverStaleJobs,\n\t\t\tmaxBackoffDelay: options.maxBackoffDelay,\n\t\t\tschedulerInstanceId: options.schedulerInstanceId ?? randomUUID(),\n\t\t\theartbeatInterval: options.heartbeatInterval ?? DEFAULTS.heartbeatInterval,\n\t\t\tjobRetention: options.jobRetention,\n\t\t};\n\t}\n\n\t/**\n\t * Initialize the scheduler by setting up the MongoDB collection and indexes.\n\t * Must be called before start().\n\t *\n\t * @throws {ConnectionError} If collection or index creation fails\n\t */\n\tasync initialize(): Promise<void> {\n\t\tif (this.isInitialized) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tthis.collection = this.db.collection(this.options.collectionName);\n\n\t\t\t// Create indexes for efficient queries\n\t\t\tawait this.createIndexes();\n\n\t\t\t// Recover stale jobs if enabled\n\t\t\tif (this.options.recoverStaleJobs) {\n\t\t\t\tawait this.recoverStaleJobs();\n\t\t\t}\n\n\t\t\t// Initialize services with shared context\n\t\t\tconst ctx = this.buildContext();\n\t\t\tthis._scheduler = new JobScheduler(ctx);\n\t\t\tthis._manager = new JobManager(ctx);\n\t\t\tthis._query = new JobQueryService(ctx);\n\t\t\tthis._processor = new JobProcessor(ctx);\n\t\t\tthis._changeStreamHandler = new ChangeStreamHandler(ctx, () => this.processor.poll());\n\n\t\t\tthis.isInitialized = true;\n\t\t} catch (error) {\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : 'Unknown error during initialization';\n\t\t\tthrow new ConnectionError(`Failed to initialize Monque: ${message}`);\n\t\t}\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Service Accessors (throw if not initialized)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get scheduler(): JobScheduler {\n\t\tif (!this._scheduler) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._scheduler;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get manager(): JobManager {\n\t\tif (!this._manager) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._manager;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get query(): JobQueryService {\n\t\tif (!this._query) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._query;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get processor(): JobProcessor {\n\t\tif (!this._processor) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._processor;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get changeStreamHandler(): ChangeStreamHandler {\n\t\tif (!this._changeStreamHandler) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._changeStreamHandler;\n\t}\n\n\t/**\n\t * Build the shared context for internal services.\n\t */\n\tprivate buildContext(): SchedulerContext {\n\t\tif (!this.collection) {\n\t\t\tthrow new ConnectionError('Collection not initialized');\n\t\t}\n\n\t\treturn {\n\t\t\tcollection: this.collection,\n\t\t\toptions: this.options,\n\t\t\tinstanceId: this.options.schedulerInstanceId,\n\t\t\tworkers: this.workers,\n\t\t\tisRunning: () => this.isRunning,\n\t\t\temit: <K extends keyof MonqueEventMap>(event: K, payload: MonqueEventMap[K]) =>\n\t\t\t\tthis.emit(event, payload),\n\t\t\tdocumentToPersistedJob: <T>(doc: WithId<Document>) => this.documentToPersistedJob<T>(doc),\n\t\t};\n\t}\n\t/**\n\t * Create required MongoDB indexes for efficient job processing.\n\t *\n\t * The following indexes are created:\n\t * - `{status, nextRunAt}` - For efficient job polling queries\n\t * - `{name, uniqueKey}` - Partial unique index for deduplication (pending/processing only)\n\t * - `{name, status}` - For job lookup by type\n\t * - `{claimedBy, status}` - For finding jobs owned by a specific scheduler instance\n\t * - `{lastHeartbeat, status}` - For monitoring/debugging queries (e.g., inspecting heartbeat age)\n\t * - `{status, nextRunAt, claimedBy}` - For atomic claim queries (find unclaimed pending jobs)\n\t * - `{lockedAt, lastHeartbeat, status}` - Supports recovery scans and monitoring access patterns\n\t */\n\tprivate async createIndexes(): Promise<void> {\n\t\tif (!this.collection) {\n\t\t\tthrow new ConnectionError('Collection not initialized');\n\t\t}\n\n\t\t// Compound index for job polling - status + nextRunAt for efficient queries\n\t\tawait this.collection.createIndex({ status: 1, nextRunAt: 1 }, { background: true });\n\n\t\t// Partial unique index for deduplication - scoped by name + uniqueKey\n\t\t// Only enforced where uniqueKey exists and status is pending/processing\n\t\tawait this.collection.createIndex(\n\t\t\t{ name: 1, uniqueKey: 1 },\n\t\t\t{\n\t\t\t\tunique: true,\n\t\t\t\tpartialFilterExpression: {\n\t\t\t\t\tuniqueKey: { $exists: true },\n\t\t\t\t\tstatus: { $in: [JobStatus.PENDING, JobStatus.PROCESSING] },\n\t\t\t\t},\n\t\t\t\tbackground: true,\n\t\t\t},\n\t\t);\n\n\t\t// Index for job lookup by name\n\t\tawait this.collection.createIndex({ name: 1, status: 1 }, { background: true });\n\n\t\t// Compound index for finding jobs claimed by a specific scheduler instance.\n\t\t// Used for heartbeat updates and cleanup on shutdown.\n\t\tawait this.collection.createIndex({ claimedBy: 1, status: 1 }, { background: true });\n\n\t\t// Compound index for monitoring/debugging via heartbeat timestamps.\n\t\t// Note: stale recovery uses lockedAt + lockTimeout as the source of truth.\n\t\tawait this.collection.createIndex({ lastHeartbeat: 1, status: 1 }, { background: true });\n\n\t\t// Compound index for atomic claim queries.\n\t\t// Optimizes the findOneAndUpdate query that claims unclaimed pending jobs.\n\t\tawait this.collection.createIndex(\n\t\t\t{ status: 1, nextRunAt: 1, claimedBy: 1 },\n\t\t\t{ background: true },\n\t\t);\n\n\t\t// Expanded index that supports recovery scans (status + lockedAt) plus heartbeat monitoring patterns.\n\t\tawait this.collection.createIndex(\n\t\t\t{ status: 1, lockedAt: 1, lastHeartbeat: 1 },\n\t\t\t{ background: true },\n\t\t);\n\t}\n\n\t/**\n\t * Recover stale jobs that were left in 'processing' status.\n\t * A job is considered stale if its `lockedAt` timestamp exceeds the configured `lockTimeout`.\n\t * Stale jobs are reset to 'pending' so they can be picked up by workers again.\n\t */\n\tprivate async recoverStaleJobs(): Promise<void> {\n\t\tif (!this.collection) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst staleThreshold = new Date(Date.now() - this.options.lockTimeout);\n\n\t\tconst result = await this.collection.updateMany(\n\t\t\t{\n\t\t\t\tstatus: JobStatus.PROCESSING,\n\t\t\t\tlockedAt: { $lt: staleThreshold },\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t\t$unset: {\n\t\t\t\t\tlockedAt: '',\n\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (result.modifiedCount > 0) {\n\t\t\t// Emit event for recovered jobs\n\t\t\tthis.emit('stale:recovered', {\n\t\t\t\tcount: result.modifiedCount,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Clean up old completed and failed jobs based on retention policy.\n\t *\n\t * - Removes completed jobs older than `jobRetention.completed`\n\t * - Removes failed jobs older than `jobRetention.failed`\n\t *\n\t * The cleanup runs concurrently for both statuses if configured.\n\t *\n\t * @returns Promise resolving when all deletion operations complete\n\t */\n\tprivate async cleanupJobs(): Promise<void> {\n\t\tif (!this.collection || !this.options.jobRetention) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { completed, failed } = this.options.jobRetention;\n\t\tconst now = Date.now();\n\t\tconst deletions: Promise<DeleteResult>[] = [];\n\n\t\tif (completed) {\n\t\t\tconst cutoff = new Date(now - completed);\n\t\t\tdeletions.push(\n\t\t\t\tthis.collection.deleteMany({\n\t\t\t\t\tstatus: JobStatus.COMPLETED,\n\t\t\t\t\tupdatedAt: { $lt: cutoff },\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\tif (failed) {\n\t\t\tconst cutoff = new Date(now - failed);\n\t\t\tdeletions.push(\n\t\t\t\tthis.collection.deleteMany({\n\t\t\t\t\tstatus: JobStatus.FAILED,\n\t\t\t\t\tupdatedAt: { $lt: cutoff },\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\tif (deletions.length > 0) {\n\t\t\tawait Promise.all(deletions);\n\t\t}\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Job Scheduling (delegates to JobScheduler)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Enqueue a job for processing.\n\t *\n\t * Jobs are stored in MongoDB and processed by registered workers. Supports\n\t * delayed execution via `runAt` and deduplication via `uniqueKey`.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. Completed or failed jobs don't block new jobs with the same key.\n\t *\n\t * Failed jobs are automatically retried with exponential backoff up to `maxRetries`\n\t * (default: 10 attempts). The delay between retries is calculated as `2^failCount × baseRetryInterval`.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @param options - Scheduling and deduplication options\n\t * @returns Promise resolving to the created or existing job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Basic job enqueueing\n\t * ```typescript\n\t * await monque.enqueue('send-email', {\n\t * to: 'user@example.com',\n\t * subject: 'Welcome!',\n\t * body: 'Thanks for signing up.'\n\t * });\n\t * ```\n\t *\n\t * @example Delayed execution\n\t * ```typescript\n\t * const oneHourLater = new Date(Date.now() + 3600000);\n\t * await monque.enqueue('reminder', { message: 'Check in!' }, {\n\t * runAt: oneHourLater\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicates with unique key\n\t * ```typescript\n\t * await monque.enqueue('sync-user', { userId: '123' }, {\n\t * uniqueKey: 'sync-user-123'\n\t * });\n\t * // Subsequent enqueues with same uniqueKey return existing pending/processing job\n\t * ```\n\t */\n\tasync enqueue<T>(name: string, data: T, options: EnqueueOptions = {}): Promise<PersistedJob<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.scheduler.enqueue(name, data, options);\n\t}\n\n\t/**\n\t * Enqueue a job for immediate processing.\n\t *\n\t * Convenience method equivalent to `enqueue(name, data, { runAt: new Date() })`.\n\t * Jobs are picked up on the next poll cycle (typically within 1 second based on `pollInterval`).\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @returns Promise resolving to the created job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Send email immediately\n\t * ```typescript\n\t * await monque.now('send-email', {\n\t * to: 'admin@example.com',\n\t * subject: 'Alert',\n\t * body: 'Immediate attention required'\n\t * });\n\t * ```\n\t *\n\t * @example Process order in background\n\t * ```typescript\n\t * const order = await createOrder(data);\n\t * await monque.now('process-order', { orderId: order.id });\n\t * return order; // Return immediately, processing happens async\n\t * ```\n\t */\n\tasync now<T>(name: string, data: T): Promise<PersistedJob<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.scheduler.now(name, data);\n\t}\n\n\t/**\n\t * Schedule a recurring job with a cron expression.\n\t *\n\t * Creates a job that automatically re-schedules itself based on the cron pattern.\n\t * Uses standard 5-field cron format: minute, hour, day of month, month, day of week.\n\t * Also supports predefined expressions like `@daily`, `@weekly`, `@monthly`, etc.\n\t * After successful completion, the job is reset to `pending` status and scheduled\n\t * for its next run based on the cron expression.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. This prevents duplicate scheduled jobs on application restart.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param cron - Cron expression (5 fields or predefined expression)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler on each run\n\t * @param options - Scheduling options (uniqueKey for deduplication)\n\t * @returns Promise resolving to the created job document with `repeatInterval` set\n\t * @throws {InvalidCronError} If cron expression is invalid\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Hourly cleanup job\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'cleanup-temp-files', {\n\t * directory: '/tmp/uploads'\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicate scheduled jobs with unique key\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'hourly-report', { type: 'sales' }, {\n\t * uniqueKey: 'hourly-report-sales'\n\t * });\n\t * // Subsequent calls with same uniqueKey return existing pending/processing job\n\t * ```\n\t *\n\t * @example Daily report at midnight (using predefined expression)\n\t * ```typescript\n\t * await monque.schedule('@daily', 'daily-report', {\n\t * reportType: 'sales',\n\t * recipients: ['analytics@example.com']\n\t * });\n\t * ```\n\t */\n\tasync schedule<T>(\n\t\tcron: string,\n\t\tname: string,\n\t\tdata: T,\n\t\toptions: ScheduleOptions = {},\n\t): Promise<PersistedJob<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.scheduler.schedule(cron, name, data, options);\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Job Management (delegates to JobManager)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Cancel a pending or scheduled job.\n\t *\n\t * Sets the job status to 'cancelled' and emits a 'job:cancelled' event.\n\t * If the job is already cancelled, this is a no-op and returns the job.\n\t * Cannot cancel jobs that are currently 'processing', 'completed', or 'failed'.\n\t *\n\t * @param jobId - The ID of the job to cancel\n\t * @returns The cancelled job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for cancellation\n\t *\n\t * @example Cancel a pending job\n\t * ```typescript\n\t * const job = await monque.enqueue('report', { type: 'daily' });\n\t * await monque.cancelJob(job._id.toString());\n\t * ```\n\t */\n\tasync cancelJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.cancelJob(jobId);\n\t}\n\n\t/**\n\t * Retry a failed or cancelled job.\n\t *\n\t * Resets the job to 'pending' status, clears failure count/reason, and sets\n\t * nextRunAt to now (immediate retry). Emits a 'job:retried' event.\n\t *\n\t * @param jobId - The ID of the job to retry\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for retry (must be failed or cancelled)\n\t *\n\t * @example Retry a failed job\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * console.log(`Job ${job._id} failed, retrying manually...`);\n\t * await monque.retryJob(job._id.toString());\n\t * });\n\t * ```\n\t */\n\tasync retryJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.retryJob(jobId);\n\t}\n\n\t/**\n\t * Reschedule a pending job to run at a different time.\n\t *\n\t * Only works for jobs in 'pending' status.\n\t *\n\t * @param jobId - The ID of the job to reschedule\n\t * @param runAt - The new Date when the job should run\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is not in pending state\n\t *\n\t * @example Delay a job by 1 hour\n\t * ```typescript\n\t * const nextHour = new Date(Date.now() + 60 * 60 * 1000);\n\t * await monque.rescheduleJob(jobId, nextHour);\n\t * ```\n\t */\n\tasync rescheduleJob(jobId: string, runAt: Date): Promise<PersistedJob<unknown> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.rescheduleJob(jobId, runAt);\n\t}\n\n\t/**\n\t * Permanently delete a job.\n\t *\n\t * This action is irreversible. Emits a 'job:deleted' event upon success.\n\t * Can delete a job in any state.\n\t *\n\t * @param jobId - The ID of the job to delete\n\t * @returns true if deleted, false if job not found\n\t *\n\t * @example Delete a cleanup job\n\t * ```typescript\n\t * const deleted = await monque.deleteJob(jobId);\n\t * if (deleted) {\n\t * console.log('Job permanently removed');\n\t * }\n\t * ```\n\t */\n\tasync deleteJob(jobId: string): Promise<boolean> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.deleteJob(jobId);\n\t}\n\n\t/**\n\t * Cancel multiple jobs matching the given filter.\n\t *\n\t * Only cancels jobs in 'pending' status. Jobs in other states are collected\n\t * as errors in the result. Emits a 'jobs:cancelled' event with the IDs of\n\t * successfully cancelled jobs.\n\t *\n\t * @param filter - Selector for which jobs to cancel (name, status, date range)\n\t * @returns Result with count of cancelled jobs and any errors encountered\n\t *\n\t * @example Cancel all pending jobs for a queue\n\t * ```typescript\n\t * const result = await monque.cancelJobs({\n\t * name: 'email-queue',\n\t * status: 'pending'\n\t * });\n\t * console.log(`Cancelled ${result.count} jobs`);\n\t * ```\n\t */\n\tasync cancelJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.cancelJobs(filter);\n\t}\n\n\t/**\n\t * Retry multiple jobs matching the given filter.\n\t *\n\t * Only retries jobs in 'failed' or 'cancelled' status. Jobs in other states\n\t * are collected as errors in the result. Emits a 'jobs:retried' event with\n\t * the IDs of successfully retried jobs.\n\t *\n\t * @param filter - Selector for which jobs to retry (name, status, date range)\n\t * @returns Result with count of retried jobs and any errors encountered\n\t *\n\t * @example Retry all failed jobs\n\t * ```typescript\n\t * const result = await monque.retryJobs({\n\t * status: 'failed'\n\t * });\n\t * console.log(`Retried ${result.count} jobs`);\n\t * ```\n\t */\n\tasync retryJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.retryJobs(filter);\n\t}\n\n\t/**\n\t * Delete multiple jobs matching the given filter.\n\t *\n\t * Deletes jobs in any status. Uses a batch delete for efficiency.\n\t * Does not emit individual 'job:deleted' events to avoid noise.\n\t *\n\t * @param filter - Selector for which jobs to delete (name, status, date range)\n\t * @returns Result with count of deleted jobs (errors array always empty for delete)\n\t *\n\t * @example Delete old completed jobs\n\t * ```typescript\n\t * const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);\n\t * const result = await monque.deleteJobs({\n\t * status: 'completed',\n\t * olderThan: weekAgo\n\t * });\n\t * console.log(`Deleted ${result.count} jobs`);\n\t * ```\n\t */\n\tasync deleteJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.deleteJobs(filter);\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Job Queries (delegates to JobQueryService)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Get a single job by its MongoDB ObjectId.\n\t *\n\t * Useful for retrieving job details when you have a job ID from events,\n\t * logs, or stored references.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param id - The job's ObjectId\n\t * @returns Promise resolving to the job if found, null otherwise\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Look up job from event\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * // Later, retrieve the job to check its status\n\t * const currentJob = await monque.getJob(job._id);\n\t * console.log(`Job status: ${currentJob?.status}`);\n\t * });\n\t * ```\n\t *\n\t * @example Admin endpoint\n\t * ```typescript\n\t * app.get('/jobs/:id', async (req, res) => {\n\t * const job = await monque.getJob(new ObjectId(req.params.id));\n\t * if (!job) {\n\t * return res.status(404).json({ error: 'Job not found' });\n\t * }\n\t * res.json(job);\n\t * });\n\t * ```\n\t */\n\tasync getJob<T = unknown>(id: ObjectId): Promise<PersistedJob<T> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getJob<T>(id);\n\t}\n\n\t/**\n\t * Query jobs from the queue with optional filters.\n\t *\n\t * Provides read-only access to job data for monitoring, debugging, and\n\t * administrative purposes. Results are ordered by `nextRunAt` ascending.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param filter - Optional filter criteria\n\t * @returns Promise resolving to array of matching jobs\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Get all pending jobs\n\t * ```typescript\n\t * const pendingJobs = await monque.getJobs({ status: JobStatus.PENDING });\n\t * console.log(`${pendingJobs.length} jobs waiting`);\n\t * ```\n\t *\n\t * @example Get failed email jobs\n\t * ```typescript\n\t * const failedEmails = await monque.getJobs({\n\t * name: 'send-email',\n\t * status: JobStatus.FAILED,\n\t * });\n\t * for (const job of failedEmails) {\n\t * console.error(`Job ${job._id} failed: ${job.failReason}`);\n\t * }\n\t * ```\n\t *\n\t * @example Paginated job listing\n\t * ```typescript\n\t * const page1 = await monque.getJobs({ limit: 50, skip: 0 });\n\t * const page2 = await monque.getJobs({ limit: 50, skip: 50 });\n\t * ```\n\t *\n\t * @example Use with type guards from @monque/core\n\t * ```typescript\n\t * import { isPendingJob, isRecurringJob } from '@monque/core';\n\t *\n\t * const jobs = await monque.getJobs();\n\t * const pendingRecurring = jobs.filter(job => isPendingJob(job) && isRecurringJob(job));\n\t * ```\n\t */\n\tasync getJobs<T = unknown>(filter: GetJobsFilter = {}): Promise<PersistedJob<T>[]> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getJobs<T>(filter);\n\t}\n\n\t/**\n\t * Get a paginated list of jobs using opaque cursors.\n\t *\n\t * Provides stable pagination for large job lists. Supports forward and backward\n\t * navigation, filtering, and efficient database access via index-based cursor queries.\n\t *\n\t * @template T - The job data payload type\n\t * @param options - Pagination options (cursor, limit, direction, filter)\n\t * @returns Page of jobs with next/prev cursors\n\t * @throws {InvalidCursorError} If the provided cursor is malformed\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example List pending jobs\n\t * ```typescript\n\t * const page = await monque.getJobsWithCursor({\n\t * limit: 20,\n\t * filter: { status: 'pending' }\n\t * });\n\t * const jobs = page.jobs;\n\t *\n\t * // Get next page\n\t * if (page.hasNextPage) {\n\t * const page2 = await monque.getJobsWithCursor({\n\t * cursor: page.cursor,\n\t * limit: 20\n\t * });\n\t * }\n\t * ```\n\t */\n\tasync getJobsWithCursor<T = unknown>(options: CursorOptions = {}): Promise<CursorPage<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getJobsWithCursor<T>(options);\n\t}\n\n\t/**\n\t * Get aggregate statistics for the job queue.\n\t *\n\t * Uses MongoDB aggregation pipeline for efficient server-side calculation.\n\t * Returns counts per status and optional average processing duration for completed jobs.\n\t *\n\t * @param filter - Optional filter to scope statistics by job name\n\t * @returns Promise resolving to queue statistics\n\t * @throws {AggregationTimeoutError} If aggregation exceeds 30 second timeout\n\t * @throws {ConnectionError} If database operation fails\n\t *\n\t * @example Get overall queue statistics\n\t * ```typescript\n\t * const stats = await monque.getQueueStats();\n\t * console.log(`Pending: ${stats.pending}, Failed: ${stats.failed}`);\n\t * ```\n\t *\n\t * @example Get statistics for a specific job type\n\t * ```typescript\n\t * const emailStats = await monque.getQueueStats({ name: 'send-email' });\n\t * console.log(`${emailStats.total} email jobs in queue`);\n\t * ```\n\t */\n\tasync getQueueStats(filter?: Pick<JobSelector, 'name'>): Promise<QueueStats> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getQueueStats(filter);\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Worker Registration\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Register a worker to process jobs of a specific type.\n\t *\n\t * Workers can be registered before or after calling `start()`. Each worker\n\t * processes jobs concurrently up to its configured concurrency limit (default: 5).\n\t *\n\t * The handler function receives the full job object including metadata (`_id`, `status`,\n\t * `failCount`, etc.). If the handler throws an error, the job is retried with exponential\n\t * backoff up to `maxRetries` times. After exhausting retries, the job is marked as `failed`.\n\t *\n\t * Events are emitted during job processing: `job:start`, `job:complete`, `job:fail`, and `job:error`.\n\t *\n\t * **Duplicate Registration**: By default, registering a worker for a job name that already has\n\t * a worker will throw a `WorkerRegistrationError`. This fail-fast behavior prevents accidental\n\t * replacement of handlers. To explicitly replace a worker, pass `{ replace: true }`.\n\t *\n\t * @template T - The job data payload type for type-safe access to `job.data`\n\t * @param name - Job type identifier to handle\n\t * @param handler - Async function to execute for each job\n\t * @param options - Worker configuration\n\t * @param options.concurrency - Maximum concurrent jobs for this worker (default: `defaultConcurrency`)\n\t * @param options.replace - When `true`, replace existing worker instead of throwing error\n\t * @throws {WorkerRegistrationError} When a worker is already registered for `name` and `replace` is not `true`\n\t *\n\t * @example Basic email worker\n\t * ```typescript\n\t * interface EmailJob {\n\t * to: string;\n\t * subject: string;\n\t * body: string;\n\t * }\n\t *\n\t * monque.register<EmailJob>('send-email', async (job) => {\n\t * await emailService.send(job.data.to, job.data.subject, job.data.body);\n\t * });\n\t * ```\n\t *\n\t * @example Worker with custom concurrency\n\t * ```typescript\n\t * // Limit to 2 concurrent video processing jobs (resource-intensive)\n\t * monque.register('process-video', async (job) => {\n\t * await videoProcessor.transcode(job.data.videoId);\n\t * }, { concurrency: 2 });\n\t * ```\n\t *\n\t * @example Replacing an existing worker\n\t * ```typescript\n\t * // Replace the existing handler for 'send-email'\n\t * monque.register('send-email', newEmailHandler, { replace: true });\n\t * ```\n\t *\n\t * @example Worker with error handling\n\t * ```typescript\n\t * monque.register('sync-user', async (job) => {\n\t * try {\n\t * await externalApi.syncUser(job.data.userId);\n\t * } catch (error) {\n\t * // Job will retry with exponential backoff\n\t * // Delay = 2^failCount × baseRetryInterval (default: 1000ms)\n\t * throw new Error(`Sync failed: ${error.message}`);\n\t * }\n\t * });\n\t * ```\n\t */\n\tregister<T>(name: string, handler: JobHandler<T>, options: WorkerOptions = {}): void {\n\t\tconst concurrency = options.concurrency ?? this.options.defaultConcurrency;\n\n\t\t// Check for existing worker and throw unless replace is explicitly true\n\t\tif (this.workers.has(name) && options.replace !== true) {\n\t\t\tthrow new WorkerRegistrationError(\n\t\t\t\t`Worker already registered for job name \"${name}\". Use { replace: true } to replace.`,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\n\t\tthis.workers.set(name, {\n\t\t\thandler: handler as JobHandler,\n\t\t\tconcurrency,\n\t\t\tactiveJobs: new Map(),\n\t\t});\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Lifecycle\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Start polling for and processing jobs.\n\t *\n\t * Begins polling MongoDB at the configured interval (default: 1 second) to pick up\n\t * pending jobs and dispatch them to registered workers. Must call `initialize()` first.\n\t * Workers can be registered before or after calling `start()`.\n\t *\n\t * Jobs are processed concurrently up to each worker's configured concurrency limit.\n\t * The scheduler continues running until `stop()` is called.\n\t *\n\t * @example Basic startup\n\t * ```typescript\n\t * const monque = new Monque(db);\n\t * await monque.initialize();\n\t *\n\t * monque.register('send-email', emailHandler);\n\t * monque.register('process-order', orderHandler);\n\t *\n\t * monque.start(); // Begin processing jobs\n\t * ```\n\t *\n\t * @example With event monitoring\n\t * ```typescript\n\t * monque.on('job:start', (job) => {\n\t * logger.info(`Starting job ${job.name}`);\n\t * });\n\t *\n\t * monque.on('job:complete', ({ job, duration }) => {\n\t * metrics.recordJobDuration(job.name, duration);\n\t * });\n\t *\n\t * monque.on('job:fail', ({ job, error, willRetry }) => {\n\t * logger.error(`Job ${job.name} failed:`, error);\n\t * if (!willRetry) {\n\t * alerting.sendAlert(`Job permanently failed: ${job.name}`);\n\t * }\n\t * });\n\t *\n\t * monque.start();\n\t * ```\n\t *\n\t * @throws {ConnectionError} If scheduler not initialized (call `initialize()` first)\n\t */\n\tstart(): void {\n\t\tif (this.isRunning) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.isInitialized) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() before start().');\n\t\t}\n\n\t\tthis.isRunning = true;\n\n\t\t// Set up change streams as the primary notification mechanism\n\t\tthis.changeStreamHandler.setup();\n\n\t\t// Set up polling as backup (runs at configured interval)\n\t\tthis.pollIntervalId = setInterval(() => {\n\t\t\tthis.processor.poll().catch((error: unknown) => {\n\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t});\n\t\t}, this.options.pollInterval);\n\n\t\t// Start heartbeat interval for claimed jobs\n\t\tthis.heartbeatIntervalId = setInterval(() => {\n\t\t\tthis.processor.updateHeartbeats().catch((error: unknown) => {\n\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t});\n\t\t}, this.options.heartbeatInterval);\n\n\t\t// Start cleanup interval if retention is configured\n\t\tif (this.options.jobRetention) {\n\t\t\tconst interval = this.options.jobRetention.interval ?? DEFAULTS.retentionInterval;\n\n\t\t\t// Run immediately on start\n\t\t\tthis.cleanupJobs().catch((error: unknown) => {\n\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t});\n\n\t\t\tthis.cleanupIntervalId = setInterval(() => {\n\t\t\t\tthis.cleanupJobs().catch((error: unknown) => {\n\t\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t\t});\n\t\t\t}, interval);\n\t\t}\n\n\t\t// Run initial poll immediately to pick up any existing jobs\n\t\tthis.processor.poll().catch((error: unknown) => {\n\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t});\n\t}\n\n\t/**\n\t * Stop the scheduler gracefully, waiting for in-progress jobs to complete.\n\t *\n\t * Stops polling for new jobs and waits for all active jobs to finish processing.\n\t * Times out after the configured `shutdownTimeout` (default: 30 seconds), emitting\n\t * a `job:error` event with a `ShutdownTimeoutError` containing incomplete jobs.\n\t * On timeout, jobs still in progress are left as `processing` for stale job recovery.\n\t *\n\t * It's safe to call `stop()` multiple times - subsequent calls are no-ops if already stopped.\n\t *\n\t * @returns Promise that resolves when all jobs complete or timeout is reached\n\t *\n\t * @example Graceful application shutdown\n\t * ```typescript\n\t * process.on('SIGTERM', async () => {\n\t * console.log('Shutting down gracefully...');\n\t * await monque.stop(); // Wait for jobs to complete\n\t * await mongoClient.close();\n\t * process.exit(0);\n\t * });\n\t * ```\n\t *\n\t * @example With timeout handling\n\t * ```typescript\n\t * monque.on('job:error', ({ error }) => {\n\t * if (error.name === 'ShutdownTimeoutError') {\n\t * logger.warn('Forced shutdown after timeout:', error.incompleteJobs);\n\t * }\n\t * });\n\t *\n\t * await monque.stop();\n\t * ```\n\t */\n\n\tasync stop(): Promise<void> {\n\t\tif (!this.isRunning) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.isRunning = false;\n\n\t\t// Close change stream\n\t\tawait this.changeStreamHandler.close();\n\n\t\tif (this.cleanupIntervalId) {\n\t\t\tclearInterval(this.cleanupIntervalId);\n\t\t\tthis.cleanupIntervalId = null;\n\t\t}\n\n\t\t// Clear polling interval\n\t\tif (this.pollIntervalId) {\n\t\t\tclearInterval(this.pollIntervalId);\n\t\t\tthis.pollIntervalId = null;\n\t\t}\n\n\t\t// Clear heartbeat interval\n\t\tif (this.heartbeatIntervalId) {\n\t\t\tclearInterval(this.heartbeatIntervalId);\n\t\t\tthis.heartbeatIntervalId = null;\n\t\t}\n\n\t\t// Wait for all active jobs to complete (with timeout)\n\t\tconst activeJobs = this.getActiveJobs();\n\t\tif (activeJobs.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Create a promise that resolves when all jobs are done\n\t\tlet checkInterval: ReturnType<typeof setInterval> | undefined;\n\t\tconst waitForJobs = new Promise<undefined>((resolve) => {\n\t\t\tcheckInterval = setInterval(() => {\n\t\t\t\tif (this.getActiveJobs().length === 0) {\n\t\t\t\t\tclearInterval(checkInterval);\n\t\t\t\t\tresolve(undefined);\n\t\t\t\t}\n\t\t\t}, 100);\n\t\t});\n\n\t\t// Race between job completion and timeout\n\t\tconst timeout = new Promise<'timeout'>((resolve) => {\n\t\t\tsetTimeout(() => resolve('timeout'), this.options.shutdownTimeout);\n\t\t});\n\n\t\tlet result: undefined | 'timeout';\n\n\t\ttry {\n\t\t\tresult = await Promise.race([waitForJobs, timeout]);\n\t\t} finally {\n\t\t\tif (checkInterval) {\n\t\t\t\tclearInterval(checkInterval);\n\t\t\t}\n\t\t}\n\n\t\tif (result === 'timeout') {\n\t\t\tconst incompleteJobs = this.getActiveJobsList();\n\n\t\t\tconst error = new ShutdownTimeoutError(\n\t\t\t\t`Shutdown timed out after ${this.options.shutdownTimeout}ms with ${incompleteJobs.length} incomplete jobs`,\n\t\t\t\tincompleteJobs,\n\t\t\t);\n\t\t\tthis.emit('job:error', { error });\n\t\t}\n\t}\n\n\t/**\n\t * Check if the scheduler is healthy (running and connected).\n\t *\n\t * Returns `true` when the scheduler is started, initialized, and has an active\n\t * MongoDB collection reference. Useful for health check endpoints and monitoring.\n\t *\n\t * A healthy scheduler:\n\t * - Has called `initialize()` successfully\n\t * - Has called `start()` and is actively polling\n\t * - Has a valid MongoDB collection reference\n\t *\n\t * @returns `true` if scheduler is running and connected, `false` otherwise\n\t *\n\t * @example Express health check endpoint\n\t * ```typescript\n\t * app.get('/health', (req, res) => {\n\t * const healthy = monque.isHealthy();\n\t * res.status(healthy ? 200 : 503).json({\n\t * status: healthy ? 'ok' : 'unavailable',\n\t * scheduler: healthy,\n\t * timestamp: new Date().toISOString()\n\t * });\n\t * });\n\t * ```\n\t *\n\t * @example Kubernetes readiness probe\n\t * ```typescript\n\t * app.get('/readyz', (req, res) => {\n\t * if (monque.isHealthy() && dbConnected) {\n\t * res.status(200).send('ready');\n\t * } else {\n\t * res.status(503).send('not ready');\n\t * }\n\t * });\n\t * ```\n\t *\n\t * @example Periodic health monitoring\n\t * ```typescript\n\t * setInterval(() => {\n\t * if (!monque.isHealthy()) {\n\t * logger.error('Scheduler unhealthy');\n\t * metrics.increment('scheduler.unhealthy');\n\t * }\n\t * }, 60000); // Check every minute\n\t * ```\n\t */\n\tisHealthy(): boolean {\n\t\treturn this.isRunning && this.isInitialized && this.collection !== null;\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Private Helpers\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Ensure the scheduler is initialized before operations.\n\t *\n\t * @private\n\t * @throws {ConnectionError} If scheduler not initialized or collection unavailable\n\t */\n\tprivate ensureInitialized(): void {\n\t\tif (!this.isInitialized || !this.collection) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\t}\n\n\t/**\n\t * Get array of active job IDs across all workers.\n\t *\n\t * @private\n\t * @returns Array of job ID strings currently being processed\n\t */\n\tprivate getActiveJobs(): string[] {\n\t\tconst activeJobs: string[] = [];\n\t\tfor (const worker of this.workers.values()) {\n\t\t\tactiveJobs.push(...worker.activeJobs.keys());\n\t\t}\n\t\treturn activeJobs;\n\t}\n\n\t/**\n\t * Get list of active job documents (for shutdown timeout error).\n\t *\n\t * @private\n\t * @returns Array of active Job objects\n\t */\n\tprivate getActiveJobsList(): Job[] {\n\t\tconst activeJobs: Job[] = [];\n\t\tfor (const worker of this.workers.values()) {\n\t\t\tactiveJobs.push(...worker.activeJobs.values());\n\t\t}\n\t\treturn activeJobs;\n\t}\n\n\t/**\n\t * Convert a MongoDB document to a typed PersistedJob object.\n\t *\n\t * Maps raw MongoDB document fields to the strongly-typed `PersistedJob<T>` interface,\n\t * ensuring type safety and handling optional fields (`lockedAt`, `failReason`, etc.).\n\t *\n\t * @private\n\t * @template T - The job data payload type\n\t * @param doc - The raw MongoDB document with `_id`\n\t * @returns A strongly-typed PersistedJob object with guaranteed `_id`\n\t */\n\tprivate documentToPersistedJob<T>(doc: WithId<Document>): PersistedJob<T> {\n\t\tconst job: PersistedJob<T> = {\n\t\t\t_id: doc._id,\n\t\t\tname: doc['name'] as string,\n\t\t\tdata: doc['data'] as T,\n\t\t\tstatus: doc['status'] as JobStatusType,\n\t\t\tnextRunAt: doc['nextRunAt'] as Date,\n\t\t\tfailCount: doc['failCount'] as number,\n\t\t\tcreatedAt: doc['createdAt'] as Date,\n\t\t\tupdatedAt: doc['updatedAt'] as Date,\n\t\t};\n\n\t\t// Only set optional properties if they exist\n\t\tif (doc['lockedAt'] !== undefined) {\n\t\t\tjob.lockedAt = doc['lockedAt'] as Date | null;\n\t\t}\n\t\tif (doc['claimedBy'] !== undefined) {\n\t\t\tjob.claimedBy = doc['claimedBy'] as string | null;\n\t\t}\n\t\tif (doc['lastHeartbeat'] !== undefined) {\n\t\t\tjob.lastHeartbeat = doc['lastHeartbeat'] as Date | null;\n\t\t}\n\t\tif (doc['heartbeatInterval'] !== undefined) {\n\t\t\tjob.heartbeatInterval = doc['heartbeatInterval'] as number;\n\t\t}\n\t\tif (doc['failReason'] !== undefined) {\n\t\t\tjob.failReason = doc['failReason'] as string;\n\t\t}\n\t\tif (doc['repeatInterval'] !== undefined) {\n\t\t\tjob.repeatInterval = doc['repeatInterval'] as string;\n\t\t}\n\t\tif (doc['uniqueKey'] !== undefined) {\n\t\t\tjob.uniqueKey = doc['uniqueKey'] as string;\n\t\t}\n\n\t\treturn job;\n\t}\n\n\t/**\n\t * Type-safe event emitter methods\n\t */\n\toverride emit<K extends keyof MonqueEventMap>(event: K, payload: MonqueEventMap[K]): boolean {\n\t\treturn super.emit(event, payload);\n\t}\n\n\toverride on<K extends keyof MonqueEventMap>(\n\t\tevent: K,\n\t\tlistener: (payload: MonqueEventMap[K]) => void,\n\t): this {\n\t\treturn super.on(event, listener);\n\t}\n\n\toverride once<K extends keyof MonqueEventMap>(\n\t\tevent: K,\n\t\tlistener: (payload: MonqueEventMap[K]) => void,\n\t): this {\n\t\treturn super.once(event, listener);\n\t}\n\n\toverride off<K extends keyof MonqueEventMap>(\n\t\tevent: K,\n\t\tlistener: (payload: MonqueEventMap[K]) => void,\n\t): this {\n\t\treturn super.off(event, listener);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAa,YAAY;CAExB,SAAS;CAET,YAAY;CAEZ,WAAW;CAEX,QAAQ;CAER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ACP,SAAO,MAAM,GAAG,OAAO,SAAS;;CAGjC,AAAS,KACR,OACA,UACO;AACP,SAAO,MAAM,KAAK,OAAO,SAAS;;CAGnC,AAAS,IACR,OACA,UACO;AACP,SAAO,MAAM,IAAI,OAAO,SAAS"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["CronExpressionParser","ObjectId","ObjectId","EventEmitter"],"sources":["../src/jobs/types.ts","../src/jobs/guards.ts","../src/shared/errors.ts","../src/shared/utils/backoff.ts","../src/shared/utils/cron.ts","../src/scheduler/helpers.ts","../src/scheduler/services/change-stream-handler.ts","../src/scheduler/services/job-manager.ts","../src/scheduler/services/job-processor.ts","../src/scheduler/services/job-query.ts","../src/scheduler/services/job-scheduler.ts","../src/scheduler/monque.ts"],"sourcesContent":["import type { ObjectId } from 'mongodb';\n\n/**\n * Represents the lifecycle states of a job in the queue.\n *\n * Jobs transition through states as follows:\n * - PENDING → PROCESSING (when picked up by a worker)\n * - PROCESSING → COMPLETED (on success)\n * - PROCESSING → PENDING (on failure, if retries remain)\n * - PROCESSING → FAILED (on failure, after max retries exhausted)\n * - PENDING → CANCELLED (on manual cancellation)\n *\n * @example\n * ```typescript\n * if (job.status === JobStatus.PENDING) {\n * // job is waiting to be picked up\n * }\n * ```\n */\nexport const JobStatus = {\n\t/** Job is waiting to be picked up by a worker */\n\tPENDING: 'pending',\n\t/** Job is currently being executed by a worker */\n\tPROCESSING: 'processing',\n\t/** Job completed successfully */\n\tCOMPLETED: 'completed',\n\t/** Job permanently failed after exhausting all retry attempts */\n\tFAILED: 'failed',\n\t/** Job was manually cancelled */\n\tCANCELLED: 'cancelled',\n} as const;\n\n/**\n * Union type of all possible job status values: `'pending' | 'processing' | 'completed' | 'failed' | 'cancelled'`\n */\nexport type JobStatusType = (typeof JobStatus)[keyof typeof JobStatus];\n\n/**\n * Represents a job in the Monque queue.\n *\n * @template T - The type of the job's data payload\n *\n * @example\n * ```typescript\n * interface EmailJobData {\n * to: string;\n * subject: string;\n * template: string;\n * }\n *\n * const job: Job<EmailJobData> = {\n * name: 'send-email',\n * data: { to: 'user@example.com', subject: 'Welcome!', template: 'welcome' },\n * status: JobStatus.PENDING,\n * nextRunAt: new Date(),\n * failCount: 0,\n * createdAt: new Date(),\n * updatedAt: new Date(),\n * };\n * ```\n */\nexport interface Job<T = unknown> {\n\t/** MongoDB document identifier */\n\t_id?: ObjectId;\n\n\t/** Job type identifier, matches worker registration */\n\tname: string;\n\n\t/** Job payload - must be JSON-serializable */\n\tdata: T;\n\n\t/** Current lifecycle state */\n\tstatus: JobStatusType;\n\n\t/** When the job should be processed */\n\tnextRunAt: Date;\n\n\t/** Timestamp when job was locked for processing */\n\tlockedAt?: Date | null;\n\n\t/**\n\t * Unique identifier of the scheduler instance that claimed this job.\n\t * Used for atomic claim pattern - ensures only one instance processes each job.\n\t * Set when a job is claimed, cleared when job completes or fails.\n\t */\n\tclaimedBy?: string | null;\n\n\t/**\n\t * Timestamp of the last heartbeat update for this job.\n\t * Used to detect stale jobs when a scheduler instance crashes without releasing.\n\t * Updated periodically while job is being processed.\n\t */\n\tlastHeartbeat?: Date | null;\n\n\t/**\n\t * Heartbeat interval in milliseconds for this job.\n\t * Stored on the job to allow recovery logic to use the correct timeout.\n\t */\n\theartbeatInterval?: number;\n\n\t/** Number of failed attempts */\n\tfailCount: number;\n\n\t/** Last failure error message */\n\tfailReason?: string;\n\n\t/** Cron expression for recurring jobs */\n\trepeatInterval?: string;\n\n\t/** Deduplication key to prevent duplicate jobs */\n\tuniqueKey?: string;\n\n\t/** Job creation timestamp */\n\tcreatedAt: Date;\n\n\t/** Last modification timestamp */\n\tupdatedAt: Date;\n}\n\n/**\n * A job that has been persisted to MongoDB and has a guaranteed `_id`.\n * This is returned by `enqueue()`, `now()`, and `schedule()` methods.\n *\n * @template T - The type of the job's data payload\n */\nexport type PersistedJob<T = unknown> = Job<T> & { _id: ObjectId };\n\n/**\n * Options for enqueueing a job.\n *\n * @example\n * ```typescript\n * await monque.enqueue('sync-user', { userId: '123' }, {\n * uniqueKey: 'sync-user-123',\n * runAt: new Date(Date.now() + 5000), // Run in 5 seconds\n * });\n * ```\n */\nexport interface EnqueueOptions {\n\t/**\n\t * Deduplication key. If a job with this key is already pending or processing,\n\t * the enqueue operation will not create a duplicate.\n\t */\n\tuniqueKey?: string;\n\n\t/**\n\t * When the job should be processed. Defaults to immediately (new Date()).\n\t */\n\trunAt?: Date;\n}\n\n/**\n * Options for scheduling a recurring job.\n *\n * @example\n * ```typescript\n * await monque. schedule('0 * * * *', 'hourly-cleanup', { dir: '/tmp' }, {\n * uniqueKey: 'hourly-cleanup-job',\n * });\n * ```\n */\nexport interface ScheduleOptions {\n\t/**\n\t * Deduplication key. If a job with this key is already pending or processing,\n\t * the schedule operation will not create a duplicate.\n\t */\n\tuniqueKey?: string;\n}\n\n/**\n * Filter options for querying jobs.\n *\n * Use with `monque.getJobs()` to filter jobs by name, status, or limit results.\n *\n * @example\n * ```typescript\n * // Get all pending email jobs\n * const pendingEmails = await monque.getJobs({\n * name: 'send-email',\n * status: JobStatus.PENDING,\n * });\n *\n * // Get all failed or completed jobs (paginated)\n * const finishedJobs = await monque.getJobs({\n * status: [JobStatus.COMPLETED, JobStatus.FAILED],\n * limit: 50,\n * skip: 100,\n * });\n * ```\n */\nexport interface GetJobsFilter {\n\t/** Filter by job type name */\n\tname?: string;\n\n\t/** Filter by status (single or multiple) */\n\tstatus?: JobStatusType | JobStatusType[];\n\n\t/** Maximum number of jobs to return (default: 100) */\n\tlimit?: number;\n\n\t/** Number of jobs to skip for pagination */\n\tskip?: number;\n}\n\n/**\n * Handler function signature for processing jobs.\n *\n * @template T - The type of the job's data payload\n *\n * @example\n * ```typescript\n * const emailHandler: JobHandler<EmailJobData> = async (job) => {\n * await sendEmail(job.data.to, job.data.subject);\n * };\n * ```\n */\nexport type JobHandler<T = unknown> = (job: Job<T>) => Promise<void> | void;\n\n/**\n * Valid cursor directions for pagination.\n *\n * @example\n * ```typescript\n * const direction = CursorDirection.FORWARD;\n * ```\n */\nexport const CursorDirection = {\n\tFORWARD: 'forward',\n\tBACKWARD: 'backward',\n} as const;\n\nexport type CursorDirectionType = (typeof CursorDirection)[keyof typeof CursorDirection];\n\n/**\n * Selector options for bulk operations.\n *\n * Used to select multiple jobs for operations like cancellation or deletion.\n *\n * @example\n * ```typescript\n * // Select all failed jobs older than 7 days\n * const selector: JobSelector = {\n * status: JobStatus.FAILED,\n * olderThan: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),\n * };\n * ```\n */\nexport interface JobSelector {\n\tname?: string;\n\tstatus?: JobStatusType | JobStatusType[];\n\tolderThan?: Date;\n\tnewerThan?: Date;\n}\n\n/**\n * Options for cursor-based pagination.\n *\n * @example\n * ```typescript\n * const options: CursorOptions = {\n * limit: 50,\n * direction: CursorDirection.FORWARD,\n * filter: { status: JobStatus.PENDING },\n * };\n * ```\n */\nexport interface CursorOptions {\n\tcursor?: string;\n\tlimit?: number;\n\tdirection?: CursorDirectionType;\n\tfilter?: Pick<GetJobsFilter, 'name' | 'status'>;\n}\n\n/**\n * Response structure for cursor-based pagination.\n *\n * @template T - The type of the job's data payload\n *\n * @example\n * ```typescript\n * const page = await monque.listJobs({ limit: 10 });\n * console.log(`Got ${page.jobs.length} jobs`);\n *\n * if (page.hasNextPage) {\n * console.log(`Next cursor: ${page.cursor}`);\n * }\n * ```\n */\nexport interface CursorPage<T = unknown> {\n\tjobs: PersistedJob<T>[];\n\tcursor: string | null;\n\thasNextPage: boolean;\n\thasPreviousPage: boolean;\n}\n\n/**\n * Aggregated statistics for the job queue.\n *\n * @example\n * ```typescript\n * const stats = await monque.getQueueStats();\n * console.log(`Total jobs: ${stats.total}`);\n * console.log(`Pending: ${stats.pending}`);\n * console.log(`Processing: ${stats.processing}`);\n * console.log(`Failed: ${stats.failed}`);\n * console.log(`Start to finish avg: ${stats.avgProcessingDurationMs}ms`);\n * ```\n */\nexport interface QueueStats {\n\tpending: number;\n\tprocessing: number;\n\tcompleted: number;\n\tfailed: number;\n\tcancelled: number;\n\ttotal: number;\n\tavgProcessingDurationMs?: number;\n}\n\n/**\n * Result of a bulk operation.\n *\n * @example\n * ```typescript\n * const result = await monque.cancelJobs(selector);\n * console.log(`Cancelled ${result.count} jobs`);\n *\n * if (result.errors.length > 0) {\n * console.warn('Some jobs could not be cancelled:', result.errors);\n * }\n * ```\n */\nexport interface BulkOperationResult {\n\tcount: number;\n\terrors: Array<{ jobId: string; error: string }>;\n}\n","import type { Job, JobStatusType, PersistedJob } from './types.js';\nimport { JobStatus } from './types.js';\n\n/**\n * Type guard to check if a job has been persisted to MongoDB.\n *\n * A persisted job is guaranteed to have an `_id` field, which means it has been\n * successfully inserted into the database. This is useful when you need to ensure\n * a job can be updated or referenced by its ID.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job has a valid `_id`, narrowing the type to `PersistedJob<T>`\n *\n * @example Basic usage\n * ```typescript\n * const job: Job<EmailData> = await monque.enqueue('send-email', emailData);\n *\n * if (isPersistedJob(job)) {\n * // TypeScript knows job._id exists\n * console.log(`Job ID: ${job._id.toString()}`);\n * }\n * ```\n *\n * @example In a conditional\n * ```typescript\n * function logJobId(job: Job) {\n * if (!isPersistedJob(job)) {\n * console.log('Job not yet persisted');\n * return;\n * }\n * // TypeScript knows job is PersistedJob here\n * console.log(`Processing job ${job._id.toString()}`);\n * }\n * ```\n */\nexport function isPersistedJob<T>(job: Job<T>): job is PersistedJob<T> {\n\treturn '_id' in job && job._id !== undefined && job._id !== null;\n}\n\n/**\n * Type guard to check if a value is a valid job status.\n *\n * Validates that a value is one of the five valid job statuses: `'pending'`,\n * `'processing'`, `'completed'`, `'failed'`, or `'cancelled'`. Useful for runtime validation\n * of user input or external data.\n *\n * @param value - The value to check\n * @returns `true` if the value is a valid `JobStatusType`, narrowing the type\n *\n * @example Validating user input\n * ```typescript\n * function filterByStatus(status: string) {\n * if (!isValidJobStatus(status)) {\n * throw new Error(`Invalid status: ${status}`);\n * }\n * // TypeScript knows status is JobStatusType here\n * return db.jobs.find({ status });\n * }\n * ```\n *\n * @example Runtime validation\n * ```typescript\n * const statusFromApi = externalData.status;\n *\n * if (isValidJobStatus(statusFromApi)) {\n * job.status = statusFromApi;\n * } else {\n * job.status = JobStatus.PENDING;\n * }\n * ```\n */\nexport function isValidJobStatus(value: unknown): value is JobStatusType {\n\treturn typeof value === 'string' && Object.values(JobStatus).includes(value as JobStatusType);\n}\n\n/**\n * Type guard to check if a job is in pending status.\n *\n * A convenience helper for checking if a job is waiting to be processed.\n * Equivalent to `job.status === JobStatus.PENDING` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'pending'`\n *\n * @example Filter pending jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const pendingJobs = jobs.filter(isPendingJob);\n * console.log(`${pendingJobs.length} jobs waiting to be processed`);\n * ```\n *\n * @example Conditional logic\n * ```typescript\n * if (isPendingJob(job)) {\n * await monque.now(job.name, job.data);\n * }\n * ```\n */\nexport function isPendingJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.PENDING;\n}\n\n/**\n * Type guard to check if a job is currently being processed.\n *\n * A convenience helper for checking if a job is actively running.\n * Equivalent to `job.status === JobStatus.PROCESSING` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'processing'`\n *\n * @example Monitor active jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const activeJobs = jobs.filter(isProcessingJob);\n * console.log(`${activeJobs.length} jobs currently running`);\n * ```\n */\nexport function isProcessingJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.PROCESSING;\n}\n\n/**\n * Type guard to check if a job has completed successfully.\n *\n * A convenience helper for checking if a job finished without errors.\n * Equivalent to `job.status === JobStatus.COMPLETED` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'completed'`\n *\n * @example Find completed jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const completedJobs = jobs.filter(isCompletedJob);\n * console.log(`${completedJobs.length} jobs completed successfully`);\n * ```\n */\nexport function isCompletedJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.COMPLETED;\n}\n\n/**\n * Type guard to check if a job has permanently failed.\n *\n * A convenience helper for checking if a job exhausted all retries.\n * Equivalent to `job.status === JobStatus.FAILED` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'failed'`\n *\n * @example Handle failed jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const failedJobs = jobs.filter(isFailedJob);\n *\n * for (const job of failedJobs) {\n * console.error(`Job ${job.name} failed: ${job.failReason}`);\n * await sendAlert(job);\n * }\n * ```\n */\nexport function isFailedJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.FAILED;\n}\n\n/**\n * Type guard to check if a job has been manually cancelled.\n *\n * A convenience helper for checking if a job was cancelled by an operator.\n * Equivalent to `job.status === JobStatus.CANCELLED` but with better semantics.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job status is `'cancelled'`\n *\n * @example Filter cancelled jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const cancelledJobs = jobs.filter(isCancelledJob);\n * console.log(`${cancelledJobs.length} jobs were cancelled`);\n * ```\n */\nexport function isCancelledJob<T>(job: Job<T>): boolean {\n\treturn job.status === JobStatus.CANCELLED;\n}\n\n/**\n * Type guard to check if a job is a recurring scheduled job.\n *\n * A recurring job has a `repeatInterval` cron expression and will be automatically\n * rescheduled after each successful completion.\n *\n * @template T - The type of the job's data payload\n * @param job - The job to check\n * @returns `true` if the job has a `repeatInterval` defined\n *\n * @example Filter recurring jobs\n * ```typescript\n * const jobs = await monque.getJobs();\n * const recurringJobs = jobs.filter(isRecurringJob);\n * console.log(`${recurringJobs.length} jobs will repeat automatically`);\n * ```\n *\n * @example Conditional cleanup\n * ```typescript\n * if (!isRecurringJob(job) && isCompletedJob(job)) {\n * // Safe to delete one-time completed jobs\n * await deleteJob(job._id);\n * }\n * ```\n */\nexport function isRecurringJob<T>(job: Job<T>): boolean {\n\treturn job.repeatInterval !== undefined && job.repeatInterval !== null;\n}\n","import type { Job } from '@/jobs';\n\n/**\n * Base error class for all Monque-related errors.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof MonqueError) {\n * console.error('Monque error:', error.message);\n * }\n * }\n * ```\n */\nexport class MonqueError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'MonqueError';\n\t\t// Maintains proper stack trace for where our error was thrown (only available on V8)\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, MonqueError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when an invalid cron expression is provided.\n *\n * @example\n * ```typescript\n * try {\n * await monque.schedule('invalid cron', 'job', data);\n * } catch (error) {\n * if (error instanceof InvalidCronError) {\n * console.error('Invalid expression:', error.expression);\n * }\n * }\n * ```\n */\nexport class InvalidCronError extends MonqueError {\n\tconstructor(\n\t\tpublic readonly expression: string,\n\t\tmessage: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'InvalidCronError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, InvalidCronError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when there's a database connection issue.\n *\n * @example\n * ```typescript\n * try {\n * await monque.enqueue('job', data);\n * } catch (error) {\n * if (error instanceof ConnectionError) {\n * console.error('Database connection lost');\n * }\n * }\n * ```\n */\nexport class ConnectionError extends MonqueError {\n\tconstructor(message: string, options?: { cause?: Error }) {\n\t\tsuper(message);\n\t\tthis.name = 'ConnectionError';\n\t\tif (options?.cause) {\n\t\t\tthis.cause = options.cause;\n\t\t}\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ConnectionError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when graceful shutdown times out.\n * Includes information about jobs that were still in progress.\n *\n * @example\n * ```typescript\n * try {\n * await monque.stop();\n * } catch (error) {\n * if (error instanceof ShutdownTimeoutError) {\n * console.error('Incomplete jobs:', error.incompleteJobs.length);\n * }\n * }\n * ```\n */\nexport class ShutdownTimeoutError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly incompleteJobs: Job[],\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ShutdownTimeoutError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, ShutdownTimeoutError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when attempting to register a worker for a job name\n * that already has a registered worker, without explicitly allowing replacement.\n *\n * @example\n * ```typescript\n * try {\n * monque.register('send-email', handler1);\n * monque.register('send-email', handler2); // throws\n * } catch (error) {\n * if (error instanceof WorkerRegistrationError) {\n * console.error('Worker already registered for:', error.jobName);\n * }\n * }\n *\n * // To intentionally replace a worker:\n * monque.register('send-email', handler2, { replace: true });\n * ```\n */\nexport class WorkerRegistrationError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly jobName: string,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'WorkerRegistrationError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, WorkerRegistrationError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when a state transition is invalid.\n *\n * @example\n * ```typescript\n * try {\n * await monque.cancelJob(jobId);\n * } catch (error) {\n * if (error instanceof JobStateError) {\n * console.error(`Cannot cancel job in state: ${error.currentStatus}`);\n * }\n * }\n * ```\n */\nexport class JobStateError extends MonqueError {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly jobId: string,\n\t\tpublic readonly currentStatus: string,\n\t\tpublic readonly attemptedAction: 'cancel' | 'retry' | 'reschedule',\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'JobStateError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, JobStateError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when a pagination cursor is invalid or malformed.\n *\n * @example\n * ```typescript\n * try {\n * await monque.listJobs({ cursor: 'invalid-cursor' });\n * } catch (error) {\n * if (error instanceof InvalidCursorError) {\n * console.error('Invalid cursor provided');\n * }\n * }\n * ```\n */\nexport class InvalidCursorError extends MonqueError {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'InvalidCursorError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, InvalidCursorError);\n\t\t}\n\t}\n}\n\n/**\n * Error thrown when a statistics aggregation times out.\n *\n * @example\n * ```typescript\n * try {\n * const stats = await monque.getQueueStats();\n * } catch (error) {\n * if (error instanceof AggregationTimeoutError) {\n * console.error('Stats took too long to calculate');\n * }\n * }\n * ```\n */\nexport class AggregationTimeoutError extends MonqueError {\n\tconstructor(message: string = 'Statistics aggregation exceeded 30 second timeout') {\n\t\tsuper(message);\n\t\tthis.name = 'AggregationTimeoutError';\n\t\t/* istanbul ignore next -- @preserve captureStackTrace is always available in Node.js */\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, AggregationTimeoutError);\n\t\t}\n\t}\n}\n","/**\n * Default base interval for exponential backoff in milliseconds.\n * @default 1000\n */\nexport const DEFAULT_BASE_INTERVAL = 1000;\n\n/**\n * Default maximum delay cap for exponential backoff in milliseconds.\n *\n * This prevents unbounded delays (e.g. failCount=20 is >11 days at 1s base)\n * and avoids precision/overflow issues for very large fail counts.\n * @default 86400000 (24 hours)\n */\nexport const DEFAULT_MAX_BACKOFF_DELAY = 24 * 60 * 60 * 1_000;\n\n/**\n * Calculate the next run time using exponential backoff.\n *\n * Formula: nextRunAt = now + (2^failCount × baseInterval)\n *\n * @param failCount - Number of previous failed attempts\n * @param baseInterval - Base interval in milliseconds (default: 1000ms)\n * @param maxDelay - Maximum delay in milliseconds (optional)\n * @returns The next run date\n *\n * @example\n * ```typescript\n * // First retry (failCount=1): 2^1 * 1000 = 2000ms delay\n * const nextRun = calculateBackoff(1);\n *\n * // Second retry (failCount=2): 2^2 * 1000 = 4000ms delay\n * const nextRun = calculateBackoff(2);\n *\n * // With custom base interval\n * const nextRun = calculateBackoff(3, 500); // 2^3 * 500 = 4000ms delay\n *\n * // With max delay\n * const nextRun = calculateBackoff(10, 1000, 60000); // capped at 60000ms\n * ```\n */\nexport function calculateBackoff(\n\tfailCount: number,\n\tbaseInterval: number = DEFAULT_BASE_INTERVAL,\n\tmaxDelay?: number,\n): Date {\n\tconst effectiveMaxDelay = maxDelay ?? DEFAULT_MAX_BACKOFF_DELAY;\n\tlet delay = 2 ** failCount * baseInterval;\n\n\tif (delay > effectiveMaxDelay) {\n\t\tdelay = effectiveMaxDelay;\n\t}\n\n\treturn new Date(Date.now() + delay);\n}\n\n/**\n * Calculate just the delay in milliseconds for a given fail count.\n *\n * @param failCount - Number of previous failed attempts\n * @param baseInterval - Base interval in milliseconds (default: 1000ms)\n * @param maxDelay - Maximum delay in milliseconds (optional)\n * @returns The delay in milliseconds\n */\nexport function calculateBackoffDelay(\n\tfailCount: number,\n\tbaseInterval: number = DEFAULT_BASE_INTERVAL,\n\tmaxDelay?: number,\n): number {\n\tconst effectiveMaxDelay = maxDelay ?? DEFAULT_MAX_BACKOFF_DELAY;\n\tlet delay = 2 ** failCount * baseInterval;\n\n\tif (delay > effectiveMaxDelay) {\n\t\tdelay = effectiveMaxDelay;\n\t}\n\n\treturn delay;\n}\n","import { CronExpressionParser } from 'cron-parser';\n\nimport { InvalidCronError } from '../errors.js';\n\n/**\n * Parse a cron expression and return the next scheduled run date.\n *\n * @param expression - A 5-field cron expression (minute hour day-of-month month day-of-week) or a predefined expression\n * @param currentDate - The reference date for calculating next run (default: now)\n * @returns The next scheduled run date\n * @throws {InvalidCronError} If the cron expression is invalid\n *\n * @example\n * ```typescript\n * // Every minute\n * const nextRun = getNextCronDate('* * * * *');\n *\n * // Every day at midnight\n * const nextRun = getNextCronDate('0 0 * * *');\n *\n * // Using predefined expression\n * const nextRun = getNextCronDate('@daily');\n *\n * // Every Monday at 9am\n * const nextRun = getNextCronDate('0 9 * * 1');\n * ```\n */\nexport function getNextCronDate(expression: string, currentDate?: Date): Date {\n\ttry {\n\t\tconst interval = CronExpressionParser.parse(expression, {\n\t\t\tcurrentDate: currentDate ?? new Date(),\n\t\t});\n\t\treturn interval.next().toDate();\n\t} catch (error) {\n\t\thandleCronParseError(expression, error);\n\t}\n}\n\n/**\n * Validate a cron expression without calculating the next run date.\n *\n * @param expression - A 5-field cron expression\n * @throws {InvalidCronError} If the cron expression is invalid\n *\n * @example\n * ```typescript\n * validateCronExpression('0 9 * * 1'); // Throws if invalid\n * ```\n */\nexport function validateCronExpression(expression: string): void {\n\ttry {\n\t\tCronExpressionParser.parse(expression);\n\t} catch (error) {\n\t\thandleCronParseError(expression, error);\n\t}\n}\n\nfunction handleCronParseError(expression: string, error: unknown): never {\n\t/* istanbul ignore next -- @preserve cron-parser always throws Error objects */\n\tconst errorMessage = error instanceof Error ? error.message : 'Unknown parsing error';\n\tthrow new InvalidCronError(\n\t\texpression,\n\t\t`Invalid cron expression \"${expression}\": ${errorMessage}. ` +\n\t\t\t'Expected 5-field format: \"minute hour day-of-month month day-of-week\" or predefined expression (e.g. @daily). ' +\n\t\t\t'Example: \"0 9 * * 1\" (every Monday at 9am)',\n\t);\n}\n","import { type Document, type Filter, ObjectId } from 'mongodb';\n\nimport { CursorDirection, type CursorDirectionType, type JobSelector } from '@/jobs';\nimport { InvalidCursorError } from '@/shared';\n\n/**\n * Build a MongoDB query filter from a JobSelector.\n *\n * Translates the high-level `JobSelector` interface into a MongoDB `Filter<Document>`.\n * Handles array values for status (using `$in`) and date range filtering.\n *\n * @param filter - The user-provided job selector\n * @returns A standard MongoDB filter object\n */\nexport function buildSelectorQuery(filter: JobSelector): Filter<Document> {\n\tconst query: Filter<Document> = {};\n\n\tif (filter.name) {\n\t\tquery['name'] = filter.name;\n\t}\n\n\tif (filter.status) {\n\t\tif (Array.isArray(filter.status)) {\n\t\t\tquery['status'] = { $in: filter.status };\n\t\t} else {\n\t\t\tquery['status'] = filter.status;\n\t\t}\n\t}\n\n\tif (filter.olderThan || filter.newerThan) {\n\t\tquery['createdAt'] = {};\n\t\tif (filter.olderThan) {\n\t\t\tquery['createdAt'].$lt = filter.olderThan;\n\t\t}\n\t\tif (filter.newerThan) {\n\t\t\tquery['createdAt'].$gt = filter.newerThan;\n\t\t}\n\t}\n\n\treturn query;\n}\n\n/**\n * Encode an ObjectId and direction into an opaque cursor string.\n *\n * Format: `prefix` + `base64url(objectId)`\n * Prefix: 'F' (forward) or 'B' (backward)\n *\n * @param id - The job ID to use as the cursor anchor (exclusive)\n * @param direction - 'forward' or 'backward'\n * @returns Base64url-encoded cursor string\n */\nexport function encodeCursor(id: ObjectId, direction: CursorDirectionType): string {\n\tconst prefix = direction === 'forward' ? 'F' : 'B';\n\tconst buffer = Buffer.from(id.toHexString(), 'hex');\n\n\treturn prefix + buffer.toString('base64url');\n}\n\n/**\n * Decode an opaque cursor string into an ObjectId and direction.\n *\n * Validates format and returns the components.\n *\n * @param cursor - The opaque cursor string\n * @returns The decoded ID and direction\n * @throws {InvalidCursorError} If the cursor format is invalid or ID is malformed\n */\nexport function decodeCursor(cursor: string): {\n\tid: ObjectId;\n\tdirection: CursorDirectionType;\n} {\n\tif (!cursor || cursor.length < 2) {\n\t\tthrow new InvalidCursorError('Cursor is empty or too short');\n\t}\n\n\tconst prefix = cursor.charAt(0);\n\tconst payload = cursor.slice(1);\n\n\tlet direction: CursorDirectionType;\n\n\tif (prefix === 'F') {\n\t\tdirection = CursorDirection.FORWARD;\n\t} else if (prefix === 'B') {\n\t\tdirection = CursorDirection.BACKWARD;\n\t} else {\n\t\tthrow new InvalidCursorError(`Invalid cursor prefix: ${prefix}`);\n\t}\n\n\ttry {\n\t\tconst buffer = Buffer.from(payload, 'base64url');\n\t\tconst hex = buffer.toString('hex');\n\t\t// standard ObjectID is 12 bytes = 24 hex chars\n\t\tif (hex.length !== 24) {\n\t\t\tthrow new InvalidCursorError('Invalid length');\n\t\t}\n\n\t\tconst id = new ObjectId(hex);\n\n\t\treturn { id, direction };\n\t} catch (error) {\n\t\tif (error instanceof InvalidCursorError) {\n\t\t\tthrow error;\n\t\t}\n\t\tthrow new InvalidCursorError('Invalid cursor payload');\n\t}\n}\n","import type { ChangeStream, ChangeStreamDocument, Document } from 'mongodb';\n\nimport { JobStatus } from '@/jobs';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for MongoDB Change Stream lifecycle.\n *\n * Provides real-time job notifications when available, with automatic\n * reconnection and graceful fallback to polling-only mode.\n *\n * @internal Not part of public API.\n */\nexport class ChangeStreamHandler {\n\t/** MongoDB Change Stream for real-time job notifications */\n\tprivate changeStream: ChangeStream | null = null;\n\n\t/** Number of consecutive reconnection attempts */\n\tprivate reconnectAttempts = 0;\n\n\t/** Maximum reconnection attempts before falling back to polling-only mode */\n\tprivate readonly maxReconnectAttempts = 3;\n\n\t/** Debounce timer for change stream event processing */\n\tprivate debounceTimer: ReturnType<typeof setTimeout> | null = null;\n\n\t/** Timer ID for reconnection with exponential backoff */\n\tprivate reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n\n\t/** Whether the scheduler is currently using change streams */\n\tprivate usingChangeStreams = false;\n\n\tconstructor(\n\t\tprivate readonly ctx: SchedulerContext,\n\t\tprivate readonly onPoll: () => Promise<void>,\n\t) {}\n\n\t/**\n\t * Set up MongoDB Change Stream for real-time job notifications.\n\t *\n\t * Change streams provide instant notifications when jobs are inserted or when\n\t * job status changes to pending (e.g., after a retry). This eliminates the\n\t * polling delay for reactive job processing.\n\t *\n\t * The change stream watches for:\n\t * - Insert operations (new jobs)\n\t * - Update operations where status field changes\n\t *\n\t * If change streams are unavailable (e.g., standalone MongoDB), the system\n\t * gracefully falls back to polling-only mode.\n\t */\n\tsetup(): void {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\t// Create change stream with pipeline to filter relevant events\n\t\t\tconst pipeline = [\n\t\t\t\t{\n\t\t\t\t\t$match: {\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{ operationType: 'insert' },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\toperationType: 'update',\n\t\t\t\t\t\t\t\t'updateDescription.updatedFields.status': { $exists: true },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\n\t\t\tthis.changeStream = this.ctx.collection.watch(pipeline, {\n\t\t\t\tfullDocument: 'updateLookup',\n\t\t\t});\n\n\t\t\t// Handle change events\n\t\t\tthis.changeStream.on('change', (change) => {\n\t\t\t\tthis.handleEvent(change);\n\t\t\t});\n\n\t\t\t// Handle errors with reconnection\n\t\t\tthis.changeStream.on('error', (error: Error) => {\n\t\t\t\tthis.ctx.emit('changestream:error', { error });\n\t\t\t\tthis.handleError(error);\n\t\t\t});\n\n\t\t\t// Mark as connected\n\t\t\tthis.usingChangeStreams = true;\n\t\t\tthis.reconnectAttempts = 0;\n\t\t\tthis.ctx.emit('changestream:connected', undefined);\n\t\t} catch (error) {\n\t\t\t// Change streams not available (e.g., standalone MongoDB)\n\t\t\tthis.usingChangeStreams = false;\n\t\t\tconst reason = error instanceof Error ? error.message : 'Unknown error';\n\t\t\tthis.ctx.emit('changestream:fallback', { reason });\n\t\t}\n\t}\n\n\t/**\n\t * Handle a change stream event by triggering a debounced poll.\n\t *\n\t * Events are debounced to prevent \"claim storms\" when multiple changes arrive\n\t * in rapid succession (e.g., bulk job inserts). A 100ms debounce window\n\t * collects multiple events and triggers a single poll.\n\t *\n\t * @param change - The change stream event document\n\t */\n\thandleEvent(change: ChangeStreamDocument<Document>): void {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Trigger poll on insert (new job) or update where status changes\n\t\tconst isInsert = change.operationType === 'insert';\n\t\tconst isUpdate = change.operationType === 'update';\n\n\t\t// Get fullDocument if available (for insert or with updateLookup option)\n\t\tconst fullDocument = 'fullDocument' in change ? change.fullDocument : undefined;\n\t\tconst isPendingStatus = fullDocument?.['status'] === JobStatus.PENDING;\n\n\t\t// For inserts: always trigger since new pending jobs need processing\n\t\t// For updates: trigger if status changed to pending (retry/release scenario)\n\t\tconst shouldTrigger = isInsert || (isUpdate && isPendingStatus);\n\n\t\tif (shouldTrigger) {\n\t\t\t// Debounce poll triggers to avoid claim storms\n\t\t\tif (this.debounceTimer) {\n\t\t\t\tclearTimeout(this.debounceTimer);\n\t\t\t}\n\n\t\t\tthis.debounceTimer = setTimeout(() => {\n\t\t\t\tthis.debounceTimer = null;\n\t\t\t\tthis.onPoll().catch((error: unknown) => {\n\t\t\t\t\tthis.ctx.emit('job:error', { error: error as Error });\n\t\t\t\t});\n\t\t\t}, 100);\n\t\t}\n\t}\n\n\t/**\n\t * Handle change stream errors with exponential backoff reconnection.\n\t *\n\t * Attempts to reconnect up to `maxReconnectAttempts` times with\n\t * exponential backoff (base 1000ms). After exhausting retries, falls back to\n\t * polling-only mode.\n\t *\n\t * @param error - The error that caused the change stream failure\n\t */\n\thandleError(error: Error): void {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.reconnectAttempts++;\n\n\t\tif (this.reconnectAttempts > this.maxReconnectAttempts) {\n\t\t\t// Fall back to polling-only mode\n\t\t\tthis.usingChangeStreams = false;\n\n\t\t\tif (this.reconnectTimer) {\n\t\t\t\tclearTimeout(this.reconnectTimer);\n\t\t\t\tthis.reconnectTimer = null;\n\t\t\t}\n\n\t\t\tif (this.changeStream) {\n\t\t\t\tthis.changeStream.close().catch(() => {});\n\t\t\t\tthis.changeStream = null;\n\t\t\t}\n\n\t\t\tthis.ctx.emit('changestream:fallback', {\n\t\t\t\treason: `Exhausted ${this.maxReconnectAttempts} reconnection attempts: ${error.message}`,\n\t\t\t});\n\n\t\t\treturn;\n\t\t}\n\n\t\t// Exponential backoff: 1s, 2s, 4s\n\t\tconst delay = 2 ** (this.reconnectAttempts - 1) * 1000;\n\n\t\t// Clear any existing reconnect timer before scheduling a new one\n\t\tif (this.reconnectTimer) {\n\t\t\tclearTimeout(this.reconnectTimer);\n\t\t}\n\n\t\tthis.reconnectTimer = setTimeout(() => {\n\t\t\tthis.reconnectTimer = null;\n\t\t\tif (this.ctx.isRunning()) {\n\t\t\t\t// Close existing change stream before reconnecting\n\t\t\t\tif (this.changeStream) {\n\t\t\t\t\tthis.changeStream.close().catch(() => {});\n\t\t\t\t\tthis.changeStream = null;\n\t\t\t\t}\n\t\t\t\tthis.setup();\n\t\t\t}\n\t\t}, delay);\n\t}\n\n\t/**\n\t * Close the change stream cursor and emit closed event.\n\t */\n\tasync close(): Promise<void> {\n\t\t// Clear debounce timer\n\t\tif (this.debounceTimer) {\n\t\t\tclearTimeout(this.debounceTimer);\n\t\t\tthis.debounceTimer = null;\n\t\t}\n\n\t\t// Clear reconnection timer\n\t\tif (this.reconnectTimer) {\n\t\t\tclearTimeout(this.reconnectTimer);\n\t\t\tthis.reconnectTimer = null;\n\t\t}\n\n\t\tif (this.changeStream) {\n\t\t\ttry {\n\t\t\t\tawait this.changeStream.close();\n\t\t\t} catch {\n\t\t\t\t// Ignore close errors during shutdown\n\t\t\t}\n\t\t\tthis.changeStream = null;\n\n\t\t\tif (this.usingChangeStreams) {\n\t\t\t\tthis.ctx.emit('changestream:closed', undefined);\n\t\t\t}\n\t\t}\n\n\t\tthis.usingChangeStreams = false;\n\t\tthis.reconnectAttempts = 0;\n\t}\n\n\t/**\n\t * Check if change streams are currently active.\n\t */\n\tisActive(): boolean {\n\t\treturn this.usingChangeStreams;\n\t}\n}\n","import { ObjectId, type WithId } from 'mongodb';\n\nimport {\n\ttype BulkOperationResult,\n\ttype Job,\n\ttype JobSelector,\n\tJobStatus,\n\ttype PersistedJob,\n} from '@/jobs';\nimport { buildSelectorQuery } from '@/scheduler';\nimport { JobStateError } from '@/shared';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job lifecycle management operations.\n *\n * Provides atomic state transitions (cancel, retry, reschedule) and deletion.\n * Emits appropriate events on each operation.\n *\n * @internal Not part of public API - use Monque class methods instead.\n */\nexport class JobManager {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Cancel a pending or scheduled job.\n\t *\n\t * Sets the job status to 'cancelled' and emits a 'job:cancelled' event.\n\t * If the job is already cancelled, this is a no-op and returns the job.\n\t * Cannot cancel jobs that are currently 'processing', 'completed', or 'failed'.\n\t *\n\t * @param jobId - The ID of the job to cancel\n\t * @returns The cancelled job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for cancellation\n\t *\n\t * @example Cancel a pending job\n\t * ```typescript\n\t * const job = await monque.enqueue('report', { type: 'daily' });\n\t * await monque.cancelJob(job._id.toString());\n\t * ```\n\t */\n\tasync cancelJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tif (!ObjectId.isValid(jobId)) return null;\n\n\t\tconst _id = new ObjectId(jobId);\n\n\t\t// Fetch job first to allow emitting the full job object in the event\n\t\tconst jobDoc = await this.ctx.collection.findOne({ _id });\n\t\tif (!jobDoc) return null;\n\n\t\tconst currentJob = jobDoc as unknown as WithId<Job>;\n\n\t\tif (currentJob.status === JobStatus.CANCELLED) {\n\t\t\treturn this.ctx.documentToPersistedJob(currentJob);\n\t\t}\n\n\t\tif (currentJob.status !== JobStatus.PENDING) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t`Cannot cancel job in status '${currentJob.status}'`,\n\t\t\t\tjobId,\n\t\t\t\tcurrentJob.status,\n\t\t\t\t'cancel',\n\t\t\t);\n\t\t}\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{ _id, status: JobStatus.PENDING },\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.CANCELLED,\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ returnDocument: 'after' },\n\t\t);\n\n\t\tif (!result) {\n\t\t\t// Race condition: job changed state between check and update\n\t\t\tthrow new JobStateError(\n\t\t\t\t'Job status changed during cancellation attempt',\n\t\t\t\tjobId,\n\t\t\t\t'unknown',\n\t\t\t\t'cancel',\n\t\t\t);\n\t\t}\n\n\t\tconst job = this.ctx.documentToPersistedJob(result);\n\t\tthis.ctx.emit('job:cancelled', { job });\n\t\treturn job;\n\t}\n\n\t/**\n\t * Retry a failed or cancelled job.\n\t *\n\t * Resets the job to 'pending' status, clears failure count/reason, and sets\n\t * nextRunAt to now (immediate retry). Emits a 'job:retried' event.\n\t *\n\t * @param jobId - The ID of the job to retry\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for retry (must be failed or cancelled)\n\t *\n\t * @example Retry a failed job\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * console.log(`Job ${job._id} failed, retrying manually...`);\n\t * await monque.retryJob(job._id.toString());\n\t * });\n\t * ```\n\t */\n\tasync retryJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tif (!ObjectId.isValid(jobId)) return null;\n\n\t\tconst _id = new ObjectId(jobId);\n\t\tconst currentJob = await this.ctx.collection.findOne({ _id });\n\n\t\tif (!currentJob) return null;\n\n\t\tif (currentJob['status'] !== JobStatus.FAILED && currentJob['status'] !== JobStatus.CANCELLED) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t`Cannot retry job in status '${currentJob['status']}'`,\n\t\t\t\tjobId,\n\t\t\t\tcurrentJob['status'],\n\t\t\t\t'retry',\n\t\t\t);\n\t\t}\n\n\t\tconst previousStatus = currentJob['status'] as 'failed' | 'cancelled';\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id,\n\t\t\t\tstatus: { $in: [JobStatus.FAILED, JobStatus.CANCELLED] },\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\tfailCount: 0,\n\t\t\t\t\tnextRunAt: new Date(),\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t\t$unset: {\n\t\t\t\t\tfailReason: '',\n\t\t\t\t\tlockedAt: '',\n\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ returnDocument: 'after' },\n\t\t);\n\n\t\tif (!result) {\n\t\t\tthrow new JobStateError('Job status changed during retry attempt', jobId, 'unknown', 'retry');\n\t\t}\n\n\t\tconst job = this.ctx.documentToPersistedJob(result);\n\t\tthis.ctx.emit('job:retried', { job, previousStatus });\n\t\treturn job;\n\t}\n\n\t/**\n\t * Reschedule a pending job to run at a different time.\n\t *\n\t * Only works for jobs in 'pending' status.\n\t *\n\t * @param jobId - The ID of the job to reschedule\n\t * @param runAt - The new Date when the job should run\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is not in pending state\n\t *\n\t * @example Delay a job by 1 hour\n\t * ```typescript\n\t * const nextHour = new Date(Date.now() + 60 * 60 * 1000);\n\t * await monque.rescheduleJob(jobId, nextHour);\n\t * ```\n\t */\n\tasync rescheduleJob(jobId: string, runAt: Date): Promise<PersistedJob<unknown> | null> {\n\t\tif (!ObjectId.isValid(jobId)) return null;\n\n\t\tconst _id = new ObjectId(jobId);\n\t\tconst currentJobDoc = await this.ctx.collection.findOne({ _id });\n\n\t\tif (!currentJobDoc) return null;\n\n\t\tconst currentJob = currentJobDoc as unknown as WithId<Job>;\n\n\t\tif (currentJob.status !== JobStatus.PENDING) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t`Cannot reschedule job in status '${currentJob.status}'`,\n\t\t\t\tjobId,\n\t\t\t\tcurrentJob.status,\n\t\t\t\t'reschedule',\n\t\t\t);\n\t\t}\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{ _id, status: JobStatus.PENDING },\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tnextRunAt: runAt,\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ returnDocument: 'after' },\n\t\t);\n\n\t\tif (!result) {\n\t\t\tthrow new JobStateError(\n\t\t\t\t'Job status changed during reschedule attempt',\n\t\t\t\tjobId,\n\t\t\t\t'unknown',\n\t\t\t\t'reschedule',\n\t\t\t);\n\t\t}\n\n\t\treturn this.ctx.documentToPersistedJob(result);\n\t}\n\n\t/**\n\t * Permanently delete a job.\n\t *\n\t * This action is irreversible. Emits a 'job:deleted' event upon success.\n\t * Can delete a job in any state.\n\t *\n\t * @param jobId - The ID of the job to delete\n\t * @returns true if deleted, false if job not found\n\t *\n\t * @example Delete a cleanup job\n\t * ```typescript\n\t * const deleted = await monque.deleteJob(jobId);\n\t * if (deleted) {\n\t * console.log('Job permanently removed');\n\t * }\n\t * ```\n\t */\n\tasync deleteJob(jobId: string): Promise<boolean> {\n\t\tif (!ObjectId.isValid(jobId)) return false;\n\n\t\tconst _id = new ObjectId(jobId);\n\n\t\tconst result = await this.ctx.collection.deleteOne({ _id });\n\n\t\tif (result.deletedCount > 0) {\n\t\t\tthis.ctx.emit('job:deleted', { jobId });\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Bulk Operations\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Cancel multiple jobs matching the given filter.\n\t *\n\t * Only cancels jobs in 'pending' status. Jobs in other states are collected\n\t * as errors in the result. Emits a 'jobs:cancelled' event with the IDs of\n\t * successfully cancelled jobs.\n\t *\n\t * @param filter - Selector for which jobs to cancel (name, status, date range)\n\t * @returns Result with count of cancelled jobs and any errors encountered\n\t *\n\t * @example Cancel all pending jobs for a queue\n\t * ```typescript\n\t * const result = await monque.cancelJobs({\n\t * name: 'email-queue',\n\t * status: 'pending'\n\t * });\n\t * console.log(`Cancelled ${result.count} jobs`);\n\t * ```\n\t */\n\tasync cancelJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tconst baseQuery = buildSelectorQuery(filter);\n\t\tconst errors: Array<{ jobId: string; error: string }> = [];\n\t\tconst cancelledIds: string[] = [];\n\n\t\t// Find all matching jobs and stream them to avoid memory pressure\n\t\tconst cursor = this.ctx.collection.find(baseQuery);\n\n\t\tfor await (const doc of cursor) {\n\t\t\tconst job = doc as unknown as WithId<Job>;\n\t\t\tconst jobId = job._id.toString();\n\n\t\t\tif (job.status !== JobStatus.PENDING && job.status !== JobStatus.CANCELLED) {\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: `Cannot cancel job in status '${job.status}'`,\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Skip already cancelled jobs (idempotent)\n\t\t\tif (job.status === JobStatus.CANCELLED) {\n\t\t\t\tcancelledIds.push(jobId);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Atomically update to cancelled\n\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t{ _id: job._id, status: JobStatus.PENDING },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.CANCELLED,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ returnDocument: 'after' },\n\t\t\t);\n\n\t\t\tif (result) {\n\t\t\t\tcancelledIds.push(jobId);\n\t\t\t} else {\n\t\t\t\t// Race condition: status changed\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: 'Job status changed during cancellation',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (cancelledIds.length > 0) {\n\t\t\tthis.ctx.emit('jobs:cancelled', {\n\t\t\t\tjobIds: cancelledIds,\n\t\t\t\tcount: cancelledIds.length,\n\t\t\t});\n\t\t}\n\n\t\treturn {\n\t\t\tcount: cancelledIds.length,\n\t\t\terrors,\n\t\t};\n\t}\n\n\t/**\n\t * Retry multiple jobs matching the given filter.\n\t *\n\t * Only retries jobs in 'failed' or 'cancelled' status. Jobs in other states\n\t * are collected as errors in the result. Emits a 'jobs:retried' event with\n\t * the IDs of successfully retried jobs.\n\t *\n\t * @param filter - Selector for which jobs to retry (name, status, date range)\n\t * @returns Result with count of retried jobs and any errors encountered\n\t *\n\t * @example Retry all failed jobs\n\t * ```typescript\n\t * const result = await monque.retryJobs({\n\t * status: 'failed'\n\t * });\n\t * console.log(`Retried ${result.count} jobs`);\n\t * ```\n\t */\n\tasync retryJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tconst baseQuery = buildSelectorQuery(filter);\n\t\tconst errors: Array<{ jobId: string; error: string }> = [];\n\t\tconst retriedIds: string[] = [];\n\n\t\tconst cursor = this.ctx.collection.find(baseQuery);\n\n\t\tfor await (const doc of cursor) {\n\t\t\tconst job = doc as unknown as WithId<Job>;\n\t\t\tconst jobId = job._id.toString();\n\n\t\t\tif (job.status !== JobStatus.FAILED && job.status !== JobStatus.CANCELLED) {\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: `Cannot retry job in status '${job.status}'`,\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t{\n\t\t\t\t\t_id: job._id,\n\t\t\t\t\tstatus: { $in: [JobStatus.FAILED, JobStatus.CANCELLED] },\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\t\tfailCount: 0,\n\t\t\t\t\t\tnextRunAt: new Date(),\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tfailReason: '',\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ returnDocument: 'after' },\n\t\t\t);\n\n\t\t\tif (result) {\n\t\t\t\tretriedIds.push(jobId);\n\t\t\t} else {\n\t\t\t\terrors.push({\n\t\t\t\t\tjobId,\n\t\t\t\t\terror: 'Job status changed during retry attempt',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (retriedIds.length > 0) {\n\t\t\tthis.ctx.emit('jobs:retried', {\n\t\t\t\tjobIds: retriedIds,\n\t\t\t\tcount: retriedIds.length,\n\t\t\t});\n\t\t}\n\n\t\treturn {\n\t\t\tcount: retriedIds.length,\n\t\t\terrors,\n\t\t};\n\t}\n\n\t/**\n\t * Delete multiple jobs matching the given filter.\n\t *\n\t * Deletes jobs in any status. Uses a batch delete for efficiency.\n\t * Emits a 'jobs:deleted' event with the count of deleted jobs.\n\t * Does not emit individual 'job:deleted' events to avoid noise.\n\t *\n\t * @param filter - Selector for which jobs to delete (name, status, date range)\n\t * @returns Result with count of deleted jobs (errors array always empty for delete)\n\t *\n\t * @example Delete old completed jobs\n\t * ```typescript\n\t * const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);\n\t * const result = await monque.deleteJobs({\n\t * status: 'completed',\n\t * olderThan: weekAgo\n\t * });\n\t * console.log(`Deleted ${result.count} jobs`);\n\t * ```\n\t */\n\tasync deleteJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tconst query = buildSelectorQuery(filter);\n\n\t\t// Use deleteMany for efficiency\n\t\tconst result = await this.ctx.collection.deleteMany(query);\n\n\t\tif (result.deletedCount > 0) {\n\t\t\tthis.ctx.emit('jobs:deleted', { count: result.deletedCount });\n\t\t}\n\n\t\treturn {\n\t\t\tcount: result.deletedCount,\n\t\t\terrors: [],\n\t\t};\n\t}\n}\n","import { isPersistedJob, type Job, JobStatus, type PersistedJob } from '@/jobs';\nimport { calculateBackoff, getNextCronDate } from '@/shared';\nimport type { WorkerRegistration } from '@/workers';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job processing and execution.\n *\n * Manages the poll loop, atomic job acquisition, handler execution,\n * and job completion/failure with exponential backoff retry logic.\n *\n * @internal Not part of public API.\n */\nexport class JobProcessor {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Poll for available jobs and process them.\n\t *\n\t * Called at regular intervals (configured by `pollInterval`). For each registered worker,\n\t * attempts to acquire jobs up to the worker's available concurrency slots.\n\t * Aborts early if the scheduler is stopping (`isRunning` is false).\n\t */\n\tasync poll(): Promise<void> {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\tfor (const [name, worker] of this.ctx.workers) {\n\t\t\t// Check if worker has capacity\n\t\t\tconst availableSlots = worker.concurrency - worker.activeJobs.size;\n\n\t\t\tif (availableSlots <= 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Try to acquire jobs up to available slots\n\t\t\tfor (let i = 0; i < availableSlots; i++) {\n\t\t\t\tif (!this.ctx.isRunning()) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst job = await this.acquireJob(name);\n\n\t\t\t\tif (job) {\n\t\t\t\t\tthis.processJob(job, worker).catch((error: unknown) => {\n\t\t\t\t\t\tthis.ctx.emit('job:error', { error: error as Error, job });\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\t// No more jobs available for this worker\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Atomically acquire a pending job for processing using the claimedBy pattern.\n\t *\n\t * Uses MongoDB's `findOneAndUpdate` with atomic operations to ensure only one scheduler\n\t * instance can claim a job. The query ensures the job is:\n\t * - In pending status\n\t * - Has nextRunAt <= now\n\t * - Is not claimed by another instance (claimedBy is null/undefined)\n\t *\n\t * Returns `null` immediately if scheduler is stopping (`isRunning` is false).\n\t *\n\t * @param name - The job type to acquire\n\t * @returns The acquired job with updated status, claimedBy, and heartbeat info, or `null` if no jobs available\n\t */\n\tasync acquireJob(name: string): Promise<PersistedJob | null> {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst now = new Date();\n\n\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t{\n\t\t\t\tname,\n\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\tnextRunAt: { $lte: now },\n\t\t\t\t$or: [{ claimedBy: null }, { claimedBy: { $exists: false } }],\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.PROCESSING,\n\t\t\t\t\tclaimedBy: this.ctx.instanceId,\n\t\t\t\t\tlockedAt: now,\n\t\t\t\t\tlastHeartbeat: now,\n\t\t\t\t\theartbeatInterval: this.ctx.options.heartbeatInterval,\n\t\t\t\t\tupdatedAt: now,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tsort: { nextRunAt: 1 },\n\t\t\t\treturnDocument: 'after',\n\t\t\t},\n\t\t);\n\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (!result) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn this.ctx.documentToPersistedJob(result);\n\t}\n\n\t/**\n\t * Execute a job using its registered worker handler.\n\t *\n\t * Tracks the job as active during processing, emits lifecycle events, and handles\n\t * both success and failure cases. On success, calls `completeJob()`. On failure,\n\t * calls `failJob()` which implements exponential backoff retry logic.\n\t *\n\t * @param job - The job to process\n\t * @param worker - The worker registration containing the handler and active job tracking\n\t */\n\tasync processJob(job: PersistedJob, worker: WorkerRegistration): Promise<void> {\n\t\tconst jobId = job._id.toString();\n\t\tworker.activeJobs.set(jobId, job);\n\n\t\tconst startTime = Date.now();\n\t\tthis.ctx.emit('job:start', job);\n\n\t\ttry {\n\t\t\tawait worker.handler(job);\n\n\t\t\t// Job completed successfully\n\t\t\tconst duration = Date.now() - startTime;\n\t\t\tawait this.completeJob(job);\n\t\t\tthis.ctx.emit('job:complete', { job, duration });\n\t\t} catch (error) {\n\t\t\t// Job failed\n\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\tawait this.failJob(job, err);\n\n\t\t\tconst willRetry = job.failCount + 1 < this.ctx.options.maxRetries;\n\t\t\tthis.ctx.emit('job:fail', { job, error: err, willRetry });\n\t\t} finally {\n\t\t\tworker.activeJobs.delete(jobId);\n\t\t}\n\t}\n\n\t/**\n\t * Mark a job as completed successfully.\n\t *\n\t * For recurring jobs (with `repeatInterval`), schedules the next run based on the cron\n\t * expression and resets `failCount` to 0. For one-time jobs, sets status to `completed`.\n\t * Clears `lockedAt` and `failReason` fields in both cases.\n\t *\n\t * @param job - The job that completed successfully\n\t */\n\tasync completeJob(job: Job): Promise<void> {\n\t\tif (!isPersistedJob(job)) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (job.repeatInterval) {\n\t\t\t// Recurring job - schedule next run\n\t\t\tconst nextRunAt = getNextCronDate(job.repeatInterval);\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\t\tnextRunAt,\n\t\t\t\t\t\tfailCount: 0,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t\tfailReason: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t} else {\n\t\t\t// One-time job - mark as completed\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.COMPLETED,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t\tfailReason: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t\tjob.status = JobStatus.COMPLETED;\n\t\t}\n\t}\n\n\t/**\n\t * Handle job failure with exponential backoff retry logic.\n\t *\n\t * Increments `failCount` and calculates next retry time using exponential backoff:\n\t * `nextRunAt = 2^failCount × baseRetryInterval` (capped by optional `maxBackoffDelay`).\n\t *\n\t * If `failCount >= maxRetries`, marks job as permanently `failed`. Otherwise, resets\n\t * to `pending` status for retry. Stores error message in `failReason` field.\n\t *\n\t * @param job - The job that failed\n\t * @param error - The error that caused the failure\n\t */\n\tasync failJob(job: Job, error: Error): Promise<void> {\n\t\tif (!isPersistedJob(job)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst newFailCount = job.failCount + 1;\n\n\t\tif (newFailCount >= this.ctx.options.maxRetries) {\n\t\t\t// Permanent failure\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.FAILED,\n\t\t\t\t\t\tfailCount: newFailCount,\n\t\t\t\t\t\tfailReason: error.message,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t} else {\n\t\t\t// Schedule retry with exponential backoff\n\t\t\tconst nextRunAt = calculateBackoff(\n\t\t\t\tnewFailCount,\n\t\t\t\tthis.ctx.options.baseRetryInterval,\n\t\t\t\tthis.ctx.options.maxBackoffDelay,\n\t\t\t);\n\n\t\t\tawait this.ctx.collection.updateOne(\n\t\t\t\t{ _id: job._id },\n\t\t\t\t{\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\t\tfailCount: newFailCount,\n\t\t\t\t\t\tfailReason: error.message,\n\t\t\t\t\t\tnextRunAt,\n\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t},\n\t\t\t\t\t$unset: {\n\t\t\t\t\t\tlockedAt: '',\n\t\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Update heartbeats for all jobs claimed by this scheduler instance.\n\t *\n\t * This method runs periodically while the scheduler is running to indicate\n\t * that jobs are still being actively processed.\n\t *\n\t * `lastHeartbeat` is primarily an observability signal (monitoring/debugging).\n\t * Stale recovery is based on `lockedAt` + `lockTimeout`.\n\t */\n\tasync updateHeartbeats(): Promise<void> {\n\t\tif (!this.ctx.isRunning()) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst now = new Date();\n\n\t\tawait this.ctx.collection.updateMany(\n\t\t\t{\n\t\t\t\tclaimedBy: this.ctx.instanceId,\n\t\t\t\tstatus: JobStatus.PROCESSING,\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tlastHeartbeat: now,\n\t\t\t\t\tupdatedAt: now,\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t}\n}\n","import type { Document, Filter, ObjectId, WithId } from 'mongodb';\n\nimport {\n\tCursorDirection,\n\ttype CursorDirectionType,\n\ttype CursorOptions,\n\ttype CursorPage,\n\ttype GetJobsFilter,\n\ttype JobSelector,\n\tJobStatus,\n\ttype PersistedJob,\n\ttype QueueStats,\n} from '@/jobs';\nimport { AggregationTimeoutError, ConnectionError } from '@/shared';\n\nimport { buildSelectorQuery, decodeCursor, encodeCursor } from '../helpers.js';\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job query operations.\n *\n * Provides read-only access to jobs with filtering and cursor-based pagination.\n * All queries use efficient index-backed access patterns.\n *\n * @internal Not part of public API - use Monque class methods instead.\n */\nexport class JobQueryService {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Get a single job by its MongoDB ObjectId.\n\t *\n\t * Useful for retrieving job details when you have a job ID from events,\n\t * logs, or stored references.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param id - The job's ObjectId\n\t * @returns Promise resolving to the job if found, null otherwise\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Look up job from event\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * // Later, retrieve the job to check its status\n\t * const currentJob = await monque.getJob(job._id);\n\t * console.log(`Job status: ${currentJob?.status}`);\n\t * });\n\t * ```\n\t *\n\t * @example Admin endpoint\n\t * ```typescript\n\t * app.get('/jobs/:id', async (req, res) => {\n\t * const job = await monque.getJob(new ObjectId(req.params.id));\n\t * if (!job) {\n\t * return res.status(404).json({ error: 'Job not found' });\n\t * }\n\t * res.json(job);\n\t * });\n\t * ```\n\t */\n\tasync getJob<T = unknown>(id: ObjectId): Promise<PersistedJob<T> | null> {\n\t\ttry {\n\t\t\tconst doc = await this.ctx.collection.findOne({ _id: id });\n\t\t\tif (!doc) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn this.ctx.documentToPersistedJob<T>(doc as WithId<Document>);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during getJob';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to get job: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Query jobs from the queue with optional filters.\n\t *\n\t * Provides read-only access to job data for monitoring, debugging, and\n\t * administrative purposes. Results are ordered by `nextRunAt` ascending.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param filter - Optional filter criteria\n\t * @returns Promise resolving to array of matching jobs\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Get all pending jobs\n\t * ```typescript\n\t * const pendingJobs = await monque.getJobs({ status: JobStatus.PENDING });\n\t * console.log(`${pendingJobs.length} jobs waiting`);\n\t * ```\n\t *\n\t * @example Get failed email jobs\n\t * ```typescript\n\t * const failedEmails = await monque.getJobs({\n\t * name: 'send-email',\n\t * status: JobStatus.FAILED,\n\t * });\n\t * for (const job of failedEmails) {\n\t * console.error(`Job ${job._id} failed: ${job.failReason}`);\n\t * }\n\t * ```\n\t *\n\t * @example Paginated job listing\n\t * ```typescript\n\t * const page1 = await monque.getJobs({ limit: 50, skip: 0 });\n\t * const page2 = await monque.getJobs({ limit: 50, skip: 50 });\n\t * ```\n\t *\n\t * @example Use with type guards from @monque/core\n\t * ```typescript\n\t * import { isPendingJob, isRecurringJob } from '@monque/core';\n\t *\n\t * const jobs = await monque.getJobs();\n\t * const pendingRecurring = jobs.filter(job => isPendingJob(job) && isRecurringJob(job));\n\t * ```\n\t */\n\tasync getJobs<T = unknown>(filter: GetJobsFilter = {}): Promise<PersistedJob<T>[]> {\n\t\tconst query: Document = {};\n\n\t\tif (filter.name !== undefined) {\n\t\t\tquery['name'] = filter.name;\n\t\t}\n\n\t\tif (filter.status !== undefined) {\n\t\t\tif (Array.isArray(filter.status)) {\n\t\t\t\tquery['status'] = { $in: filter.status };\n\t\t\t} else {\n\t\t\t\tquery['status'] = filter.status;\n\t\t\t}\n\t\t}\n\n\t\tconst limit = filter.limit ?? 100;\n\t\tconst skip = filter.skip ?? 0;\n\n\t\ttry {\n\t\t\tconst cursor = this.ctx.collection.find(query).sort({ nextRunAt: 1 }).skip(skip).limit(limit);\n\n\t\t\tconst docs = await cursor.toArray();\n\t\t\treturn docs.map((doc) => this.ctx.documentToPersistedJob<T>(doc));\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during getJobs';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to query jobs: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Get a paginated list of jobs using opaque cursors.\n\t *\n\t * Provides stable pagination for large job lists. Supports forward and backward\n\t * navigation, filtering, and efficient database access via index-based cursor queries.\n\t *\n\t * @template T - The job data payload type\n\t * @param options - Pagination options (cursor, limit, direction, filter)\n\t * @returns Page of jobs with next/prev cursors\n\t * @throws {InvalidCursorError} If the provided cursor is malformed\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example List pending jobs\n\t * ```typescript\n\t * const page = await monque.getJobsWithCursor({\n\t * limit: 20,\n\t * filter: { status: 'pending' }\n\t * });\n\t * const jobs = page.jobs;\n\t *\n\t * // Get next page\n\t * if (page.hasNextPage) {\n\t * const page2 = await monque.getJobsWithCursor({\n\t * cursor: page.cursor,\n\t * limit: 20\n\t * });\n\t * }\n\t * ```\n\t */\n\tasync getJobsWithCursor<T = unknown>(options: CursorOptions = {}): Promise<CursorPage<T>> {\n\t\tconst limit = options.limit ?? 50;\n\t\t// Default to forward if not specified.\n\t\tconst direction: CursorDirectionType = options.direction ?? CursorDirection.FORWARD;\n\t\tlet anchorId: ObjectId | null = null;\n\n\t\tif (options.cursor) {\n\t\t\tconst decoded = decodeCursor(options.cursor);\n\t\t\tanchorId = decoded.id;\n\t\t}\n\n\t\t// Build base query from filters\n\t\tconst query: Filter<Document> = options.filter ? buildSelectorQuery(options.filter) : {};\n\n\t\t// Add cursor condition to query\n\t\tconst sortDir = direction === CursorDirection.FORWARD ? 1 : -1;\n\n\t\tif (anchorId) {\n\t\t\tif (direction === CursorDirection.FORWARD) {\n\t\t\t\tquery._id = { ...query._id, $gt: anchorId };\n\t\t\t} else {\n\t\t\t\tquery._id = { ...query._id, $lt: anchorId };\n\t\t\t}\n\t\t}\n\n\t\t// Fetch limit + 1 to detect hasNext/hasPrev\n\t\tconst fetchLimit = limit + 1;\n\n\t\t// Sort: Always deterministic.\n\t\tlet docs: WithId<Document>[];\n\t\ttry {\n\t\t\tdocs = await this.ctx.collection\n\t\t\t\t.find(query)\n\t\t\t\t.sort({ _id: sortDir })\n\t\t\t\t.limit(fetchLimit)\n\t\t\t\t.toArray();\n\t\t} catch (error) {\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : 'Unknown error during getJobsWithCursor';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to query jobs with cursor: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\n\t\tlet hasMore = false;\n\t\tif (docs.length > limit) {\n\t\t\thasMore = true;\n\t\t\tdocs.pop(); // Remove the extra item\n\t\t}\n\n\t\tif (direction === CursorDirection.BACKWARD) {\n\t\t\tdocs.reverse();\n\t\t}\n\n\t\tconst jobs = docs.map((doc) => this.ctx.documentToPersistedJob<T>(doc as WithId<Document>));\n\n\t\tlet nextCursor: string | null = null;\n\n\t\tif (jobs.length > 0) {\n\t\t\tconst lastJob = jobs[jobs.length - 1];\n\t\t\t// Check for existence to satisfy strict null checks/noUncheckedIndexedAccess\n\t\t\tif (lastJob) {\n\t\t\t\tnextCursor = encodeCursor(lastJob._id, direction);\n\t\t\t}\n\t\t}\n\n\t\tlet hasNextPage = false;\n\t\tlet hasPreviousPage = false;\n\n\t\t// Determine availability of next/prev pages\n\t\tif (direction === CursorDirection.FORWARD) {\n\t\t\thasNextPage = hasMore;\n\t\t\thasPreviousPage = !!anchorId;\n\t\t} else {\n\t\t\thasNextPage = !!anchorId;\n\t\t\thasPreviousPage = hasMore;\n\t\t}\n\n\t\treturn {\n\t\t\tjobs,\n\t\t\tcursor: nextCursor,\n\t\t\thasNextPage,\n\t\t\thasPreviousPage,\n\t\t};\n\t}\n\n\t/**\n\t * Get aggregate statistics for the job queue.\n\t *\n\t * Uses MongoDB aggregation pipeline for efficient server-side calculation.\n\t * Returns counts per status and optional average processing duration for completed jobs.\n\t *\n\t * @param filter - Optional filter to scope statistics by job name\n\t * @returns Promise resolving to queue statistics\n\t * @throws {AggregationTimeoutError} If aggregation exceeds 30 second timeout\n\t * @throws {ConnectionError} If database operation fails\n\t *\n\t * @example Get overall queue statistics\n\t * ```typescript\n\t * const stats = await monque.getQueueStats();\n\t * console.log(`Pending: ${stats.pending}, Failed: ${stats.failed}`);\n\t * ```\n\t *\n\t * @example Get statistics for a specific job type\n\t * ```typescript\n\t * const emailStats = await monque.getQueueStats({ name: 'send-email' });\n\t * console.log(`${emailStats.total} email jobs in queue`);\n\t * ```\n\t */\n\tasync getQueueStats(filter?: Pick<JobSelector, 'name'>): Promise<QueueStats> {\n\t\tconst matchStage: Document = {};\n\n\t\tif (filter?.name) {\n\t\t\tmatchStage['name'] = filter.name;\n\t\t}\n\n\t\tconst pipeline: Document[] = [\n\t\t\t// Optional match stage for filtering by name\n\t\t\t...(Object.keys(matchStage).length > 0 ? [{ $match: matchStage }] : []),\n\t\t\t// Facet to calculate counts and avg processing duration in parallel\n\t\t\t{\n\t\t\t\t$facet: {\n\t\t\t\t\t// Count by status\n\t\t\t\t\tstatusCounts: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t_id: '$status',\n\t\t\t\t\t\t\t\tcount: { $sum: 1 },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\t// Calculate average job lifetime for completed jobs.\n\t\t\t\t\t// Uses createdAt → updatedAt (total lifetime = queue wait + processing)\n\t\t\t\t\t// since completeJob() unsets lockedAt, making pure processing time unavailable.\n\t\t\t\t\tavgDuration: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\tstatus: JobStatus.COMPLETED,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t_id: null,\n\t\t\t\t\t\t\t\tavgMs: {\n\t\t\t\t\t\t\t\t\t$avg: {\n\t\t\t\t\t\t\t\t\t\t$subtract: ['$updatedAt', '$createdAt'],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\t// Total count\n\t\t\t\t\ttotal: [{ $count: 'count' }],\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\ttry {\n\t\t\tconst results = await this.ctx.collection.aggregate(pipeline, { maxTimeMS: 30000 }).toArray();\n\n\t\t\tconst result = results[0];\n\n\t\t\t// Initialize with zeros\n\t\t\tconst stats: QueueStats = {\n\t\t\t\tpending: 0,\n\t\t\t\tprocessing: 0,\n\t\t\t\tcompleted: 0,\n\t\t\t\tfailed: 0,\n\t\t\t\tcancelled: 0,\n\t\t\t\ttotal: 0,\n\t\t\t};\n\n\t\t\tif (!result) {\n\t\t\t\treturn stats;\n\t\t\t}\n\n\t\t\t// Map status counts to stats\n\t\t\tconst statusCounts = result['statusCounts'] as Array<{ _id: string; count: number }>;\n\t\t\tfor (const entry of statusCounts) {\n\t\t\t\tconst status = entry._id;\n\t\t\t\tconst count = entry.count;\n\n\t\t\t\tswitch (status) {\n\t\t\t\t\tcase JobStatus.PENDING:\n\t\t\t\t\t\tstats.pending = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.PROCESSING:\n\t\t\t\t\t\tstats.processing = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.COMPLETED:\n\t\t\t\t\t\tstats.completed = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.FAILED:\n\t\t\t\t\t\tstats.failed = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase JobStatus.CANCELLED:\n\t\t\t\t\t\tstats.cancelled = count;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Extract total\n\t\t\tconst totalResult = result['total'] as Array<{ count: number }>;\n\t\t\tif (totalResult.length > 0 && totalResult[0]) {\n\t\t\t\tstats.total = totalResult[0].count;\n\t\t\t}\n\n\t\t\t// Extract average processing duration\n\t\t\tconst avgDurationResult = result['avgDuration'] as Array<{ avgMs: number }>;\n\t\t\tif (avgDurationResult.length > 0 && avgDurationResult[0]) {\n\t\t\t\tconst avgMs = avgDurationResult[0].avgMs;\n\t\t\t\tif (typeof avgMs === 'number' && !Number.isNaN(avgMs)) {\n\t\t\t\t\tstats.avgProcessingDurationMs = Math.round(avgMs);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn stats;\n\t\t} catch (error) {\n\t\t\t// Check for timeout error\n\t\t\tif (error instanceof Error && error.message.includes('exceeded time limit')) {\n\t\t\t\tthrow new AggregationTimeoutError();\n\t\t\t}\n\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during getQueueStats';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to get queue stats: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n}\n","import type { Document } from 'mongodb';\n\nimport {\n\ttype EnqueueOptions,\n\ttype Job,\n\tJobStatus,\n\ttype PersistedJob,\n\ttype ScheduleOptions,\n} from '@/jobs';\nimport { ConnectionError, getNextCronDate, MonqueError } from '@/shared';\n\nimport type { SchedulerContext } from './types.js';\n\n/**\n * Internal service for job scheduling operations.\n *\n * Handles enqueueing new jobs, immediate dispatch, and cron scheduling.\n * All operations are atomic and support deduplication via uniqueKey.\n *\n * @internal Not part of public API - use Monque class methods instead.\n */\nexport class JobScheduler {\n\tconstructor(private readonly ctx: SchedulerContext) {}\n\n\t/**\n\t * Enqueue a job for processing.\n\t *\n\t * Jobs are stored in MongoDB and processed by registered workers. Supports\n\t * delayed execution via `runAt` and deduplication via `uniqueKey`.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. Completed or failed jobs don't block new jobs with the same key.\n\t *\n\t * Failed jobs are automatically retried with exponential backoff up to `maxRetries`\n\t * (default: 10 attempts). The delay between retries is calculated as `2^failCount × baseRetryInterval`.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @param options - Scheduling and deduplication options\n\t * @returns Promise resolving to the created or existing job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Basic job enqueueing\n\t * ```typescript\n\t * await monque.enqueue('send-email', {\n\t * to: 'user@example.com',\n\t * subject: 'Welcome!',\n\t * body: 'Thanks for signing up.'\n\t * });\n\t * ```\n\t *\n\t * @example Delayed execution\n\t * ```typescript\n\t * const oneHourLater = new Date(Date.now() + 3600000);\n\t * await monque.enqueue('reminder', { message: 'Check in!' }, {\n\t * runAt: oneHourLater\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicates with unique key\n\t * ```typescript\n\t * await monque.enqueue('sync-user', { userId: '123' }, {\n\t * uniqueKey: 'sync-user-123'\n\t * });\n\t * // Subsequent enqueues with same uniqueKey return existing pending/processing job\n\t * ```\n\t */\n\tasync enqueue<T>(name: string, data: T, options: EnqueueOptions = {}): Promise<PersistedJob<T>> {\n\t\tconst now = new Date();\n\t\tconst job: Omit<Job<T>, '_id'> = {\n\t\t\tname,\n\t\t\tdata,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tnextRunAt: options.runAt ?? now,\n\t\t\tfailCount: 0,\n\t\t\tcreatedAt: now,\n\t\t\tupdatedAt: now,\n\t\t};\n\n\t\tif (options.uniqueKey) {\n\t\t\tjob.uniqueKey = options.uniqueKey;\n\t\t}\n\n\t\ttry {\n\t\t\tif (options.uniqueKey) {\n\t\t\t\t// Use upsert with $setOnInsert for deduplication (scoped by name + uniqueKey)\n\t\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t\t{\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tuniqueKey: options.uniqueKey,\n\t\t\t\t\t\tstatus: { $in: [JobStatus.PENDING, JobStatus.PROCESSING] },\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$setOnInsert: job,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tupsert: true,\n\t\t\t\t\t\treturnDocument: 'after',\n\t\t\t\t\t},\n\t\t\t\t);\n\n\t\t\t\tif (!result) {\n\t\t\t\t\tthrow new ConnectionError('Failed to enqueue job: findOneAndUpdate returned no document');\n\t\t\t\t}\n\n\t\t\t\treturn this.ctx.documentToPersistedJob<T>(result);\n\t\t\t}\n\n\t\t\tconst result = await this.ctx.collection.insertOne(job as Document);\n\n\t\t\treturn { ...job, _id: result.insertedId } as PersistedJob<T>;\n\t\t} catch (error) {\n\t\t\tif (error instanceof ConnectionError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during enqueue';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to enqueue job: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Enqueue a job for immediate processing.\n\t *\n\t * Convenience method equivalent to `enqueue(name, data, { runAt: new Date() })`.\n\t * Jobs are picked up on the next poll cycle (typically within 1 second based on `pollInterval`).\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @returns Promise resolving to the created job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Send email immediately\n\t * ```typescript\n\t * await monque.now('send-email', {\n\t * to: 'admin@example.com',\n\t * subject: 'Alert',\n\t * body: 'Immediate attention required'\n\t * });\n\t * ```\n\t *\n\t * @example Process order in background\n\t * ```typescript\n\t * const order = await createOrder(data);\n\t * await monque.now('process-order', { orderId: order.id });\n\t * return order; // Return immediately, processing happens async\n\t * ```\n\t */\n\tasync now<T>(name: string, data: T): Promise<PersistedJob<T>> {\n\t\treturn this.enqueue(name, data, { runAt: new Date() });\n\t}\n\n\t/**\n\t * Schedule a recurring job with a cron expression.\n\t *\n\t * Creates a job that automatically re-schedules itself based on the cron pattern.\n\t * Uses standard 5-field cron format: minute, hour, day of month, month, day of week.\n\t * Also supports predefined expressions like `@daily`, `@weekly`, `@monthly`, etc.\n\t * After successful completion, the job is reset to `pending` status and scheduled\n\t * for its next run based on the cron expression.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. This prevents duplicate scheduled jobs on application restart.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param cron - Cron expression (5 fields or predefined expression)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler on each run\n\t * @param options - Scheduling options (uniqueKey for deduplication)\n\t * @returns Promise resolving to the created job document with `repeatInterval` set\n\t * @throws {InvalidCronError} If cron expression is invalid\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Hourly cleanup job\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'cleanup-temp-files', {\n\t * directory: '/tmp/uploads'\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicate scheduled jobs with unique key\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'hourly-report', { type: 'sales' }, {\n\t * uniqueKey: 'hourly-report-sales'\n\t * });\n\t * // Subsequent calls with same uniqueKey return existing pending/processing job\n\t * ```\n\t *\n\t * @example Daily report at midnight (using predefined expression)\n\t * ```typescript\n\t * await monque.schedule('@daily', 'daily-report', {\n\t * reportType: 'sales',\n\t * recipients: ['analytics@example.com']\n\t * });\n\t * ```\n\t */\n\tasync schedule<T>(\n\t\tcron: string,\n\t\tname: string,\n\t\tdata: T,\n\t\toptions: ScheduleOptions = {},\n\t): Promise<PersistedJob<T>> {\n\t\t// Validate cron and get next run date (throws InvalidCronError if invalid)\n\t\tconst nextRunAt = getNextCronDate(cron);\n\n\t\tconst now = new Date();\n\t\tconst job: Omit<Job<T>, '_id'> = {\n\t\t\tname,\n\t\t\tdata,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tnextRunAt,\n\t\t\trepeatInterval: cron,\n\t\t\tfailCount: 0,\n\t\t\tcreatedAt: now,\n\t\t\tupdatedAt: now,\n\t\t};\n\n\t\tif (options.uniqueKey) {\n\t\t\tjob.uniqueKey = options.uniqueKey;\n\t\t}\n\n\t\ttry {\n\t\t\tif (options.uniqueKey) {\n\t\t\t\t// Use upsert with $setOnInsert for deduplication (scoped by name + uniqueKey)\n\t\t\t\tconst result = await this.ctx.collection.findOneAndUpdate(\n\t\t\t\t\t{\n\t\t\t\t\t\tname,\n\t\t\t\t\t\tuniqueKey: options.uniqueKey,\n\t\t\t\t\t\tstatus: { $in: [JobStatus.PENDING, JobStatus.PROCESSING] },\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$setOnInsert: job,\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tupsert: true,\n\t\t\t\t\t\treturnDocument: 'after',\n\t\t\t\t\t},\n\t\t\t\t);\n\n\t\t\t\tif (!result) {\n\t\t\t\t\tthrow new ConnectionError(\n\t\t\t\t\t\t'Failed to schedule job: findOneAndUpdate returned no document',\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn this.ctx.documentToPersistedJob<T>(result);\n\t\t\t}\n\n\t\t\tconst result = await this.ctx.collection.insertOne(job as Document);\n\n\t\t\treturn { ...job, _id: result.insertedId } as PersistedJob<T>;\n\t\t} catch (error) {\n\t\t\tif (error instanceof MonqueError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst message = error instanceof Error ? error.message : 'Unknown error during schedule';\n\t\t\tthrow new ConnectionError(\n\t\t\t\t`Failed to schedule job: ${message}`,\n\t\t\t\terror instanceof Error ? { cause: error } : undefined,\n\t\t\t);\n\t\t}\n\t}\n}\n","import { randomUUID } from 'node:crypto';\nimport { EventEmitter } from 'node:events';\nimport type { Collection, Db, DeleteResult, Document, ObjectId, WithId } from 'mongodb';\n\nimport type { MonqueEventMap } from '@/events';\nimport {\n\ttype BulkOperationResult,\n\ttype CursorOptions,\n\ttype CursorPage,\n\ttype EnqueueOptions,\n\ttype GetJobsFilter,\n\ttype Job,\n\ttype JobHandler,\n\ttype JobSelector,\n\tJobStatus,\n\ttype JobStatusType,\n\ttype PersistedJob,\n\ttype QueueStats,\n\ttype ScheduleOptions,\n} from '@/jobs';\nimport { ConnectionError, ShutdownTimeoutError, WorkerRegistrationError } from '@/shared';\nimport type { WorkerOptions, WorkerRegistration } from '@/workers';\n\nimport {\n\tChangeStreamHandler,\n\tJobManager,\n\tJobProcessor,\n\tJobQueryService,\n\tJobScheduler,\n\ttype ResolvedMonqueOptions,\n\ttype SchedulerContext,\n} from './services/index.js';\nimport type { MonqueOptions } from './types.js';\n\n/**\n * Default configuration values\n */\nconst DEFAULTS = {\n\tcollectionName: 'monque_jobs',\n\tpollInterval: 1000,\n\tmaxRetries: 10,\n\tbaseRetryInterval: 1000,\n\tshutdownTimeout: 30000,\n\tdefaultConcurrency: 5,\n\tlockTimeout: 1_800_000, // 30 minutes\n\trecoverStaleJobs: true,\n\theartbeatInterval: 30000, // 30 seconds\n\tretentionInterval: 3600_000, // 1 hour\n} as const;\n\n/**\n * Monque - MongoDB-backed job scheduler\n *\n * A type-safe job scheduler with atomic locking, exponential backoff, cron scheduling,\n * stale job recovery, and event-driven observability. Built on native MongoDB driver.\n *\n * @example Complete lifecycle\n * ```typescript\n * import { Monque } from '@monque/core';\n * import { MongoClient } from 'mongodb';\n *\n * const client = new MongoClient('mongodb://localhost:27017');\n * await client.connect();\n * const db = client.db('myapp');\n *\n * // Create instance with options\n * const monque = new Monque(db, {\n * collectionName: 'jobs',\n * pollInterval: 1000,\n * maxRetries: 10,\n * shutdownTimeout: 30000,\n * });\n *\n * // Initialize (sets up indexes and recovers stale jobs)\n * await monque.initialize();\n *\n * // Register workers with type safety\n * type EmailJob = {\n * to: string;\n * subject: string;\n * body: string;\n * };\n *\n * monque.register<EmailJob>('send-email', async (job) => {\n * await emailService.send(job.data.to, job.data.subject, job.data.body);\n * });\n *\n * // Monitor events for observability\n * monque.on('job:complete', ({ job, duration }) => {\n * logger.info(`Job ${job.name} completed in ${duration}ms`);\n * });\n *\n * monque.on('job:fail', ({ job, error, willRetry }) => {\n * logger.error(`Job ${job.name} failed:`, error);\n * });\n *\n * // Start processing\n * monque.start();\n *\n * // Enqueue jobs\n * await monque.enqueue('send-email', {\n * to: 'user@example.com',\n * subject: 'Welcome!',\n * body: 'Thanks for signing up.'\n * });\n *\n * // Graceful shutdown\n * process.on('SIGTERM', async () => {\n * await monque.stop();\n * await client.close();\n * process.exit(0);\n * });\n * ```\n */\nexport class Monque extends EventEmitter {\n\tprivate readonly db: Db;\n\tprivate readonly options: ResolvedMonqueOptions;\n\tprivate collection: Collection<Document> | null = null;\n\tprivate workers: Map<string, WorkerRegistration> = new Map();\n\tprivate pollIntervalId: ReturnType<typeof setInterval> | null = null;\n\tprivate heartbeatIntervalId: ReturnType<typeof setInterval> | null = null;\n\tprivate cleanupIntervalId: ReturnType<typeof setInterval> | null = null;\n\tprivate isRunning = false;\n\tprivate isInitialized = false;\n\n\t// Internal services (initialized in initialize())\n\tprivate _scheduler: JobScheduler | null = null;\n\tprivate _manager: JobManager | null = null;\n\tprivate _query: JobQueryService | null = null;\n\tprivate _processor: JobProcessor | null = null;\n\tprivate _changeStreamHandler: ChangeStreamHandler | null = null;\n\n\tconstructor(db: Db, options: MonqueOptions = {}) {\n\t\tsuper();\n\t\tthis.db = db;\n\t\tthis.options = {\n\t\t\tcollectionName: options.collectionName ?? DEFAULTS.collectionName,\n\t\t\tpollInterval: options.pollInterval ?? DEFAULTS.pollInterval,\n\t\t\tmaxRetries: options.maxRetries ?? DEFAULTS.maxRetries,\n\t\t\tbaseRetryInterval: options.baseRetryInterval ?? DEFAULTS.baseRetryInterval,\n\t\t\tshutdownTimeout: options.shutdownTimeout ?? DEFAULTS.shutdownTimeout,\n\t\t\tdefaultConcurrency: options.defaultConcurrency ?? DEFAULTS.defaultConcurrency,\n\t\t\tlockTimeout: options.lockTimeout ?? DEFAULTS.lockTimeout,\n\t\t\trecoverStaleJobs: options.recoverStaleJobs ?? DEFAULTS.recoverStaleJobs,\n\t\t\tmaxBackoffDelay: options.maxBackoffDelay,\n\t\t\tschedulerInstanceId: options.schedulerInstanceId ?? randomUUID(),\n\t\t\theartbeatInterval: options.heartbeatInterval ?? DEFAULTS.heartbeatInterval,\n\t\t\tjobRetention: options.jobRetention,\n\t\t};\n\t}\n\n\t/**\n\t * Initialize the scheduler by setting up the MongoDB collection and indexes.\n\t * Must be called before start().\n\t *\n\t * @throws {ConnectionError} If collection or index creation fails\n\t */\n\tasync initialize(): Promise<void> {\n\t\tif (this.isInitialized) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tthis.collection = this.db.collection(this.options.collectionName);\n\n\t\t\t// Create indexes for efficient queries\n\t\t\tawait this.createIndexes();\n\n\t\t\t// Recover stale jobs if enabled\n\t\t\tif (this.options.recoverStaleJobs) {\n\t\t\t\tawait this.recoverStaleJobs();\n\t\t\t}\n\n\t\t\t// Initialize services with shared context\n\t\t\tconst ctx = this.buildContext();\n\t\t\tthis._scheduler = new JobScheduler(ctx);\n\t\t\tthis._manager = new JobManager(ctx);\n\t\t\tthis._query = new JobQueryService(ctx);\n\t\t\tthis._processor = new JobProcessor(ctx);\n\t\t\tthis._changeStreamHandler = new ChangeStreamHandler(ctx, () => this.processor.poll());\n\n\t\t\tthis.isInitialized = true;\n\t\t} catch (error) {\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : 'Unknown error during initialization';\n\t\t\tthrow new ConnectionError(`Failed to initialize Monque: ${message}`);\n\t\t}\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Service Accessors (throw if not initialized)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get scheduler(): JobScheduler {\n\t\tif (!this._scheduler) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._scheduler;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get manager(): JobManager {\n\t\tif (!this._manager) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._manager;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get query(): JobQueryService {\n\t\tif (!this._query) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._query;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get processor(): JobProcessor {\n\t\tif (!this._processor) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._processor;\n\t}\n\n\t/** @throws {ConnectionError} if not initialized */\n\tprivate get changeStreamHandler(): ChangeStreamHandler {\n\t\tif (!this._changeStreamHandler) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\n\t\treturn this._changeStreamHandler;\n\t}\n\n\t/**\n\t * Build the shared context for internal services.\n\t */\n\tprivate buildContext(): SchedulerContext {\n\t\tif (!this.collection) {\n\t\t\tthrow new ConnectionError('Collection not initialized');\n\t\t}\n\n\t\treturn {\n\t\t\tcollection: this.collection,\n\t\t\toptions: this.options,\n\t\t\tinstanceId: this.options.schedulerInstanceId,\n\t\t\tworkers: this.workers,\n\t\t\tisRunning: () => this.isRunning,\n\t\t\temit: <K extends keyof MonqueEventMap>(event: K, payload: MonqueEventMap[K]) =>\n\t\t\t\tthis.emit(event, payload),\n\t\t\tdocumentToPersistedJob: <T>(doc: WithId<Document>) => this.documentToPersistedJob<T>(doc),\n\t\t};\n\t}\n\t/**\n\t * Create required MongoDB indexes for efficient job processing.\n\t *\n\t * The following indexes are created:\n\t * - `{status, nextRunAt}` - For efficient job polling queries\n\t * - `{name, uniqueKey}` - Partial unique index for deduplication (pending/processing only)\n\t * - `{name, status}` - For job lookup by type\n\t * - `{claimedBy, status}` - For finding jobs owned by a specific scheduler instance\n\t * - `{lastHeartbeat, status}` - For monitoring/debugging queries (e.g., inspecting heartbeat age)\n\t * - `{status, nextRunAt, claimedBy}` - For atomic claim queries (find unclaimed pending jobs)\n\t * - `{lockedAt, lastHeartbeat, status}` - Supports recovery scans and monitoring access patterns\n\t */\n\tprivate async createIndexes(): Promise<void> {\n\t\tif (!this.collection) {\n\t\t\tthrow new ConnectionError('Collection not initialized');\n\t\t}\n\n\t\t// Compound index for job polling - status + nextRunAt for efficient queries\n\t\tawait this.collection.createIndex({ status: 1, nextRunAt: 1 }, { background: true });\n\n\t\t// Partial unique index for deduplication - scoped by name + uniqueKey\n\t\t// Only enforced where uniqueKey exists and status is pending/processing\n\t\tawait this.collection.createIndex(\n\t\t\t{ name: 1, uniqueKey: 1 },\n\t\t\t{\n\t\t\t\tunique: true,\n\t\t\t\tpartialFilterExpression: {\n\t\t\t\t\tuniqueKey: { $exists: true },\n\t\t\t\t\tstatus: { $in: [JobStatus.PENDING, JobStatus.PROCESSING] },\n\t\t\t\t},\n\t\t\t\tbackground: true,\n\t\t\t},\n\t\t);\n\n\t\t// Index for job lookup by name\n\t\tawait this.collection.createIndex({ name: 1, status: 1 }, { background: true });\n\n\t\t// Compound index for finding jobs claimed by a specific scheduler instance.\n\t\t// Used for heartbeat updates and cleanup on shutdown.\n\t\tawait this.collection.createIndex({ claimedBy: 1, status: 1 }, { background: true });\n\n\t\t// Compound index for monitoring/debugging via heartbeat timestamps.\n\t\t// Note: stale recovery uses lockedAt + lockTimeout as the source of truth.\n\t\tawait this.collection.createIndex({ lastHeartbeat: 1, status: 1 }, { background: true });\n\n\t\t// Compound index for atomic claim queries.\n\t\t// Optimizes the findOneAndUpdate query that claims unclaimed pending jobs.\n\t\tawait this.collection.createIndex(\n\t\t\t{ status: 1, nextRunAt: 1, claimedBy: 1 },\n\t\t\t{ background: true },\n\t\t);\n\n\t\t// Expanded index that supports recovery scans (status + lockedAt) plus heartbeat monitoring patterns.\n\t\tawait this.collection.createIndex(\n\t\t\t{ status: 1, lockedAt: 1, lastHeartbeat: 1 },\n\t\t\t{ background: true },\n\t\t);\n\t}\n\n\t/**\n\t * Recover stale jobs that were left in 'processing' status.\n\t * A job is considered stale if its `lockedAt` timestamp exceeds the configured `lockTimeout`.\n\t * Stale jobs are reset to 'pending' so they can be picked up by workers again.\n\t */\n\tprivate async recoverStaleJobs(): Promise<void> {\n\t\tif (!this.collection) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst staleThreshold = new Date(Date.now() - this.options.lockTimeout);\n\n\t\tconst result = await this.collection.updateMany(\n\t\t\t{\n\t\t\t\tstatus: JobStatus.PROCESSING,\n\t\t\t\tlockedAt: { $lt: staleThreshold },\n\t\t\t},\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t},\n\t\t\t\t$unset: {\n\t\t\t\t\tlockedAt: '',\n\t\t\t\t\tclaimedBy: '',\n\t\t\t\t\tlastHeartbeat: '',\n\t\t\t\t\theartbeatInterval: '',\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (result.modifiedCount > 0) {\n\t\t\t// Emit event for recovered jobs\n\t\t\tthis.emit('stale:recovered', {\n\t\t\t\tcount: result.modifiedCount,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Clean up old completed and failed jobs based on retention policy.\n\t *\n\t * - Removes completed jobs older than `jobRetention.completed`\n\t * - Removes failed jobs older than `jobRetention.failed`\n\t *\n\t * The cleanup runs concurrently for both statuses if configured.\n\t *\n\t * @returns Promise resolving when all deletion operations complete\n\t */\n\tprivate async cleanupJobs(): Promise<void> {\n\t\tif (!this.collection || !this.options.jobRetention) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { completed, failed } = this.options.jobRetention;\n\t\tconst now = Date.now();\n\t\tconst deletions: Promise<DeleteResult>[] = [];\n\n\t\tif (completed) {\n\t\t\tconst cutoff = new Date(now - completed);\n\t\t\tdeletions.push(\n\t\t\t\tthis.collection.deleteMany({\n\t\t\t\t\tstatus: JobStatus.COMPLETED,\n\t\t\t\t\tupdatedAt: { $lt: cutoff },\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\tif (failed) {\n\t\t\tconst cutoff = new Date(now - failed);\n\t\t\tdeletions.push(\n\t\t\t\tthis.collection.deleteMany({\n\t\t\t\t\tstatus: JobStatus.FAILED,\n\t\t\t\t\tupdatedAt: { $lt: cutoff },\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\tif (deletions.length > 0) {\n\t\t\tawait Promise.all(deletions);\n\t\t}\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Job Scheduling (delegates to JobScheduler)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Enqueue a job for processing.\n\t *\n\t * Jobs are stored in MongoDB and processed by registered workers. Supports\n\t * delayed execution via `runAt` and deduplication via `uniqueKey`.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. Completed or failed jobs don't block new jobs with the same key.\n\t *\n\t * Failed jobs are automatically retried with exponential backoff up to `maxRetries`\n\t * (default: 10 attempts). The delay between retries is calculated as `2^failCount × baseRetryInterval`.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @param options - Scheduling and deduplication options\n\t * @returns Promise resolving to the created or existing job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Basic job enqueueing\n\t * ```typescript\n\t * await monque.enqueue('send-email', {\n\t * to: 'user@example.com',\n\t * subject: 'Welcome!',\n\t * body: 'Thanks for signing up.'\n\t * });\n\t * ```\n\t *\n\t * @example Delayed execution\n\t * ```typescript\n\t * const oneHourLater = new Date(Date.now() + 3600000);\n\t * await monque.enqueue('reminder', { message: 'Check in!' }, {\n\t * runAt: oneHourLater\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicates with unique key\n\t * ```typescript\n\t * await monque.enqueue('sync-user', { userId: '123' }, {\n\t * uniqueKey: 'sync-user-123'\n\t * });\n\t * // Subsequent enqueues with same uniqueKey return existing pending/processing job\n\t * ```\n\t */\n\tasync enqueue<T>(name: string, data: T, options: EnqueueOptions = {}): Promise<PersistedJob<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.scheduler.enqueue(name, data, options);\n\t}\n\n\t/**\n\t * Enqueue a job for immediate processing.\n\t *\n\t * Convenience method equivalent to `enqueue(name, data, { runAt: new Date() })`.\n\t * Jobs are picked up on the next poll cycle (typically within 1 second based on `pollInterval`).\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler\n\t * @returns Promise resolving to the created job document\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Send email immediately\n\t * ```typescript\n\t * await monque.now('send-email', {\n\t * to: 'admin@example.com',\n\t * subject: 'Alert',\n\t * body: 'Immediate attention required'\n\t * });\n\t * ```\n\t *\n\t * @example Process order in background\n\t * ```typescript\n\t * const order = await createOrder(data);\n\t * await monque.now('process-order', { orderId: order.id });\n\t * return order; // Return immediately, processing happens async\n\t * ```\n\t */\n\tasync now<T>(name: string, data: T): Promise<PersistedJob<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.scheduler.now(name, data);\n\t}\n\n\t/**\n\t * Schedule a recurring job with a cron expression.\n\t *\n\t * Creates a job that automatically re-schedules itself based on the cron pattern.\n\t * Uses standard 5-field cron format: minute, hour, day of month, month, day of week.\n\t * Also supports predefined expressions like `@daily`, `@weekly`, `@monthly`, etc.\n\t * After successful completion, the job is reset to `pending` status and scheduled\n\t * for its next run based on the cron expression.\n\t *\n\t * When a `uniqueKey` is provided, only one pending or processing job with that key\n\t * can exist. This prevents duplicate scheduled jobs on application restart.\n\t *\n\t * @template T - The job data payload type (must be JSON-serializable)\n\t * @param cron - Cron expression (5 fields or predefined expression)\n\t * @param name - Job type identifier, must match a registered worker\n\t * @param data - Job payload, will be passed to the worker handler on each run\n\t * @param options - Scheduling options (uniqueKey for deduplication)\n\t * @returns Promise resolving to the created job document with `repeatInterval` set\n\t * @throws {InvalidCronError} If cron expression is invalid\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example Hourly cleanup job\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'cleanup-temp-files', {\n\t * directory: '/tmp/uploads'\n\t * });\n\t * ```\n\t *\n\t * @example Prevent duplicate scheduled jobs with unique key\n\t * ```typescript\n\t * await monque.schedule('0 * * * *', 'hourly-report', { type: 'sales' }, {\n\t * uniqueKey: 'hourly-report-sales'\n\t * });\n\t * // Subsequent calls with same uniqueKey return existing pending/processing job\n\t * ```\n\t *\n\t * @example Daily report at midnight (using predefined expression)\n\t * ```typescript\n\t * await monque.schedule('@daily', 'daily-report', {\n\t * reportType: 'sales',\n\t * recipients: ['analytics@example.com']\n\t * });\n\t * ```\n\t */\n\tasync schedule<T>(\n\t\tcron: string,\n\t\tname: string,\n\t\tdata: T,\n\t\toptions: ScheduleOptions = {},\n\t): Promise<PersistedJob<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.scheduler.schedule(cron, name, data, options);\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Job Management (delegates to JobManager)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Cancel a pending or scheduled job.\n\t *\n\t * Sets the job status to 'cancelled' and emits a 'job:cancelled' event.\n\t * If the job is already cancelled, this is a no-op and returns the job.\n\t * Cannot cancel jobs that are currently 'processing', 'completed', or 'failed'.\n\t *\n\t * @param jobId - The ID of the job to cancel\n\t * @returns The cancelled job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for cancellation\n\t *\n\t * @example Cancel a pending job\n\t * ```typescript\n\t * const job = await monque.enqueue('report', { type: 'daily' });\n\t * await monque.cancelJob(job._id.toString());\n\t * ```\n\t */\n\tasync cancelJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.cancelJob(jobId);\n\t}\n\n\t/**\n\t * Retry a failed or cancelled job.\n\t *\n\t * Resets the job to 'pending' status, clears failure count/reason, and sets\n\t * nextRunAt to now (immediate retry). Emits a 'job:retried' event.\n\t *\n\t * @param jobId - The ID of the job to retry\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is in an invalid state for retry (must be failed or cancelled)\n\t *\n\t * @example Retry a failed job\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * console.log(`Job ${job._id} failed, retrying manually...`);\n\t * await monque.retryJob(job._id.toString());\n\t * });\n\t * ```\n\t */\n\tasync retryJob(jobId: string): Promise<PersistedJob<unknown> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.retryJob(jobId);\n\t}\n\n\t/**\n\t * Reschedule a pending job to run at a different time.\n\t *\n\t * Only works for jobs in 'pending' status.\n\t *\n\t * @param jobId - The ID of the job to reschedule\n\t * @param runAt - The new Date when the job should run\n\t * @returns The updated job, or null if not found\n\t * @throws {JobStateError} If job is not in pending state\n\t *\n\t * @example Delay a job by 1 hour\n\t * ```typescript\n\t * const nextHour = new Date(Date.now() + 60 * 60 * 1000);\n\t * await monque.rescheduleJob(jobId, nextHour);\n\t * ```\n\t */\n\tasync rescheduleJob(jobId: string, runAt: Date): Promise<PersistedJob<unknown> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.rescheduleJob(jobId, runAt);\n\t}\n\n\t/**\n\t * Permanently delete a job.\n\t *\n\t * This action is irreversible. Emits a 'job:deleted' event upon success.\n\t * Can delete a job in any state.\n\t *\n\t * @param jobId - The ID of the job to delete\n\t * @returns true if deleted, false if job not found\n\t *\n\t * @example Delete a cleanup job\n\t * ```typescript\n\t * const deleted = await monque.deleteJob(jobId);\n\t * if (deleted) {\n\t * console.log('Job permanently removed');\n\t * }\n\t * ```\n\t */\n\tasync deleteJob(jobId: string): Promise<boolean> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.deleteJob(jobId);\n\t}\n\n\t/**\n\t * Cancel multiple jobs matching the given filter.\n\t *\n\t * Only cancels jobs in 'pending' status. Jobs in other states are collected\n\t * as errors in the result. Emits a 'jobs:cancelled' event with the IDs of\n\t * successfully cancelled jobs.\n\t *\n\t * @param filter - Selector for which jobs to cancel (name, status, date range)\n\t * @returns Result with count of cancelled jobs and any errors encountered\n\t *\n\t * @example Cancel all pending jobs for a queue\n\t * ```typescript\n\t * const result = await monque.cancelJobs({\n\t * name: 'email-queue',\n\t * status: 'pending'\n\t * });\n\t * console.log(`Cancelled ${result.count} jobs`);\n\t * ```\n\t */\n\tasync cancelJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.cancelJobs(filter);\n\t}\n\n\t/**\n\t * Retry multiple jobs matching the given filter.\n\t *\n\t * Only retries jobs in 'failed' or 'cancelled' status. Jobs in other states\n\t * are collected as errors in the result. Emits a 'jobs:retried' event with\n\t * the IDs of successfully retried jobs.\n\t *\n\t * @param filter - Selector for which jobs to retry (name, status, date range)\n\t * @returns Result with count of retried jobs and any errors encountered\n\t *\n\t * @example Retry all failed jobs\n\t * ```typescript\n\t * const result = await monque.retryJobs({\n\t * status: 'failed'\n\t * });\n\t * console.log(`Retried ${result.count} jobs`);\n\t * ```\n\t */\n\tasync retryJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.retryJobs(filter);\n\t}\n\n\t/**\n\t * Delete multiple jobs matching the given filter.\n\t *\n\t * Deletes jobs in any status. Uses a batch delete for efficiency.\n\t * Does not emit individual 'job:deleted' events to avoid noise.\n\t *\n\t * @param filter - Selector for which jobs to delete (name, status, date range)\n\t * @returns Result with count of deleted jobs (errors array always empty for delete)\n\t *\n\t * @example Delete old completed jobs\n\t * ```typescript\n\t * const weekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);\n\t * const result = await monque.deleteJobs({\n\t * status: 'completed',\n\t * olderThan: weekAgo\n\t * });\n\t * console.log(`Deleted ${result.count} jobs`);\n\t * ```\n\t */\n\tasync deleteJobs(filter: JobSelector): Promise<BulkOperationResult> {\n\t\tthis.ensureInitialized();\n\t\treturn this.manager.deleteJobs(filter);\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Job Queries (delegates to JobQueryService)\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Get a single job by its MongoDB ObjectId.\n\t *\n\t * Useful for retrieving job details when you have a job ID from events,\n\t * logs, or stored references.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param id - The job's ObjectId\n\t * @returns Promise resolving to the job if found, null otherwise\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Look up job from event\n\t * ```typescript\n\t * monque.on('job:fail', async ({ job }) => {\n\t * // Later, retrieve the job to check its status\n\t * const currentJob = await monque.getJob(job._id);\n\t * console.log(`Job status: ${currentJob?.status}`);\n\t * });\n\t * ```\n\t *\n\t * @example Admin endpoint\n\t * ```typescript\n\t * app.get('/jobs/:id', async (req, res) => {\n\t * const job = await monque.getJob(new ObjectId(req.params.id));\n\t * if (!job) {\n\t * return res.status(404).json({ error: 'Job not found' });\n\t * }\n\t * res.json(job);\n\t * });\n\t * ```\n\t */\n\tasync getJob<T = unknown>(id: ObjectId): Promise<PersistedJob<T> | null> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getJob<T>(id);\n\t}\n\n\t/**\n\t * Query jobs from the queue with optional filters.\n\t *\n\t * Provides read-only access to job data for monitoring, debugging, and\n\t * administrative purposes. Results are ordered by `nextRunAt` ascending.\n\t *\n\t * @template T - The expected type of the job data payload\n\t * @param filter - Optional filter criteria\n\t * @returns Promise resolving to array of matching jobs\n\t * @throws {ConnectionError} If scheduler not initialized\n\t *\n\t * @example Get all pending jobs\n\t * ```typescript\n\t * const pendingJobs = await monque.getJobs({ status: JobStatus.PENDING });\n\t * console.log(`${pendingJobs.length} jobs waiting`);\n\t * ```\n\t *\n\t * @example Get failed email jobs\n\t * ```typescript\n\t * const failedEmails = await monque.getJobs({\n\t * name: 'send-email',\n\t * status: JobStatus.FAILED,\n\t * });\n\t * for (const job of failedEmails) {\n\t * console.error(`Job ${job._id} failed: ${job.failReason}`);\n\t * }\n\t * ```\n\t *\n\t * @example Paginated job listing\n\t * ```typescript\n\t * const page1 = await monque.getJobs({ limit: 50, skip: 0 });\n\t * const page2 = await monque.getJobs({ limit: 50, skip: 50 });\n\t * ```\n\t *\n\t * @example Use with type guards from @monque/core\n\t * ```typescript\n\t * import { isPendingJob, isRecurringJob } from '@monque/core';\n\t *\n\t * const jobs = await monque.getJobs();\n\t * const pendingRecurring = jobs.filter(job => isPendingJob(job) && isRecurringJob(job));\n\t * ```\n\t */\n\tasync getJobs<T = unknown>(filter: GetJobsFilter = {}): Promise<PersistedJob<T>[]> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getJobs<T>(filter);\n\t}\n\n\t/**\n\t * Get a paginated list of jobs using opaque cursors.\n\t *\n\t * Provides stable pagination for large job lists. Supports forward and backward\n\t * navigation, filtering, and efficient database access via index-based cursor queries.\n\t *\n\t * @template T - The job data payload type\n\t * @param options - Pagination options (cursor, limit, direction, filter)\n\t * @returns Page of jobs with next/prev cursors\n\t * @throws {InvalidCursorError} If the provided cursor is malformed\n\t * @throws {ConnectionError} If database operation fails or scheduler not initialized\n\t *\n\t * @example List pending jobs\n\t * ```typescript\n\t * const page = await monque.getJobsWithCursor({\n\t * limit: 20,\n\t * filter: { status: 'pending' }\n\t * });\n\t * const jobs = page.jobs;\n\t *\n\t * // Get next page\n\t * if (page.hasNextPage) {\n\t * const page2 = await monque.getJobsWithCursor({\n\t * cursor: page.cursor,\n\t * limit: 20\n\t * });\n\t * }\n\t * ```\n\t */\n\tasync getJobsWithCursor<T = unknown>(options: CursorOptions = {}): Promise<CursorPage<T>> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getJobsWithCursor<T>(options);\n\t}\n\n\t/**\n\t * Get aggregate statistics for the job queue.\n\t *\n\t * Uses MongoDB aggregation pipeline for efficient server-side calculation.\n\t * Returns counts per status and optional average processing duration for completed jobs.\n\t *\n\t * @param filter - Optional filter to scope statistics by job name\n\t * @returns Promise resolving to queue statistics\n\t * @throws {AggregationTimeoutError} If aggregation exceeds 30 second timeout\n\t * @throws {ConnectionError} If database operation fails\n\t *\n\t * @example Get overall queue statistics\n\t * ```typescript\n\t * const stats = await monque.getQueueStats();\n\t * console.log(`Pending: ${stats.pending}, Failed: ${stats.failed}`);\n\t * ```\n\t *\n\t * @example Get statistics for a specific job type\n\t * ```typescript\n\t * const emailStats = await monque.getQueueStats({ name: 'send-email' });\n\t * console.log(`${emailStats.total} email jobs in queue`);\n\t * ```\n\t */\n\tasync getQueueStats(filter?: Pick<JobSelector, 'name'>): Promise<QueueStats> {\n\t\tthis.ensureInitialized();\n\t\treturn this.query.getQueueStats(filter);\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Worker Registration\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Register a worker to process jobs of a specific type.\n\t *\n\t * Workers can be registered before or after calling `start()`. Each worker\n\t * processes jobs concurrently up to its configured concurrency limit (default: 5).\n\t *\n\t * The handler function receives the full job object including metadata (`_id`, `status`,\n\t * `failCount`, etc.). If the handler throws an error, the job is retried with exponential\n\t * backoff up to `maxRetries` times. After exhausting retries, the job is marked as `failed`.\n\t *\n\t * Events are emitted during job processing: `job:start`, `job:complete`, `job:fail`, and `job:error`.\n\t *\n\t * **Duplicate Registration**: By default, registering a worker for a job name that already has\n\t * a worker will throw a `WorkerRegistrationError`. This fail-fast behavior prevents accidental\n\t * replacement of handlers. To explicitly replace a worker, pass `{ replace: true }`.\n\t *\n\t * @template T - The job data payload type for type-safe access to `job.data`\n\t * @param name - Job type identifier to handle\n\t * @param handler - Async function to execute for each job\n\t * @param options - Worker configuration\n\t * @param options.concurrency - Maximum concurrent jobs for this worker (default: `defaultConcurrency`)\n\t * @param options.replace - When `true`, replace existing worker instead of throwing error\n\t * @throws {WorkerRegistrationError} When a worker is already registered for `name` and `replace` is not `true`\n\t *\n\t * @example Basic email worker\n\t * ```typescript\n\t * interface EmailJob {\n\t * to: string;\n\t * subject: string;\n\t * body: string;\n\t * }\n\t *\n\t * monque.register<EmailJob>('send-email', async (job) => {\n\t * await emailService.send(job.data.to, job.data.subject, job.data.body);\n\t * });\n\t * ```\n\t *\n\t * @example Worker with custom concurrency\n\t * ```typescript\n\t * // Limit to 2 concurrent video processing jobs (resource-intensive)\n\t * monque.register('process-video', async (job) => {\n\t * await videoProcessor.transcode(job.data.videoId);\n\t * }, { concurrency: 2 });\n\t * ```\n\t *\n\t * @example Replacing an existing worker\n\t * ```typescript\n\t * // Replace the existing handler for 'send-email'\n\t * monque.register('send-email', newEmailHandler, { replace: true });\n\t * ```\n\t *\n\t * @example Worker with error handling\n\t * ```typescript\n\t * monque.register('sync-user', async (job) => {\n\t * try {\n\t * await externalApi.syncUser(job.data.userId);\n\t * } catch (error) {\n\t * // Job will retry with exponential backoff\n\t * // Delay = 2^failCount × baseRetryInterval (default: 1000ms)\n\t * throw new Error(`Sync failed: ${error.message}`);\n\t * }\n\t * });\n\t * ```\n\t */\n\tregister<T>(name: string, handler: JobHandler<T>, options: WorkerOptions = {}): void {\n\t\tconst concurrency = options.concurrency ?? this.options.defaultConcurrency;\n\n\t\t// Check for existing worker and throw unless replace is explicitly true\n\t\tif (this.workers.has(name) && options.replace !== true) {\n\t\t\tthrow new WorkerRegistrationError(\n\t\t\t\t`Worker already registered for job name \"${name}\". Use { replace: true } to replace.`,\n\t\t\t\tname,\n\t\t\t);\n\t\t}\n\n\t\tthis.workers.set(name, {\n\t\t\thandler: handler as JobHandler,\n\t\t\tconcurrency,\n\t\t\tactiveJobs: new Map(),\n\t\t});\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Public API - Lifecycle\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Start polling for and processing jobs.\n\t *\n\t * Begins polling MongoDB at the configured interval (default: 1 second) to pick up\n\t * pending jobs and dispatch them to registered workers. Must call `initialize()` first.\n\t * Workers can be registered before or after calling `start()`.\n\t *\n\t * Jobs are processed concurrently up to each worker's configured concurrency limit.\n\t * The scheduler continues running until `stop()` is called.\n\t *\n\t * @example Basic startup\n\t * ```typescript\n\t * const monque = new Monque(db);\n\t * await monque.initialize();\n\t *\n\t * monque.register('send-email', emailHandler);\n\t * monque.register('process-order', orderHandler);\n\t *\n\t * monque.start(); // Begin processing jobs\n\t * ```\n\t *\n\t * @example With event monitoring\n\t * ```typescript\n\t * monque.on('job:start', (job) => {\n\t * logger.info(`Starting job ${job.name}`);\n\t * });\n\t *\n\t * monque.on('job:complete', ({ job, duration }) => {\n\t * metrics.recordJobDuration(job.name, duration);\n\t * });\n\t *\n\t * monque.on('job:fail', ({ job, error, willRetry }) => {\n\t * logger.error(`Job ${job.name} failed:`, error);\n\t * if (!willRetry) {\n\t * alerting.sendAlert(`Job permanently failed: ${job.name}`);\n\t * }\n\t * });\n\t *\n\t * monque.start();\n\t * ```\n\t *\n\t * @throws {ConnectionError} If scheduler not initialized (call `initialize()` first)\n\t */\n\tstart(): void {\n\t\tif (this.isRunning) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.isInitialized) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() before start().');\n\t\t}\n\n\t\tthis.isRunning = true;\n\n\t\t// Set up change streams as the primary notification mechanism\n\t\tthis.changeStreamHandler.setup();\n\n\t\t// Set up polling as backup (runs at configured interval)\n\t\tthis.pollIntervalId = setInterval(() => {\n\t\t\tthis.processor.poll().catch((error: unknown) => {\n\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t});\n\t\t}, this.options.pollInterval);\n\n\t\t// Start heartbeat interval for claimed jobs\n\t\tthis.heartbeatIntervalId = setInterval(() => {\n\t\t\tthis.processor.updateHeartbeats().catch((error: unknown) => {\n\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t});\n\t\t}, this.options.heartbeatInterval);\n\n\t\t// Start cleanup interval if retention is configured\n\t\tif (this.options.jobRetention) {\n\t\t\tconst interval = this.options.jobRetention.interval ?? DEFAULTS.retentionInterval;\n\n\t\t\t// Run immediately on start\n\t\t\tthis.cleanupJobs().catch((error: unknown) => {\n\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t});\n\n\t\t\tthis.cleanupIntervalId = setInterval(() => {\n\t\t\t\tthis.cleanupJobs().catch((error: unknown) => {\n\t\t\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t\t\t});\n\t\t\t}, interval);\n\t\t}\n\n\t\t// Run initial poll immediately to pick up any existing jobs\n\t\tthis.processor.poll().catch((error: unknown) => {\n\t\t\tthis.emit('job:error', { error: error as Error });\n\t\t});\n\t}\n\n\t/**\n\t * Stop the scheduler gracefully, waiting for in-progress jobs to complete.\n\t *\n\t * Stops polling for new jobs and waits for all active jobs to finish processing.\n\t * Times out after the configured `shutdownTimeout` (default: 30 seconds), emitting\n\t * a `job:error` event with a `ShutdownTimeoutError` containing incomplete jobs.\n\t * On timeout, jobs still in progress are left as `processing` for stale job recovery.\n\t *\n\t * It's safe to call `stop()` multiple times - subsequent calls are no-ops if already stopped.\n\t *\n\t * @returns Promise that resolves when all jobs complete or timeout is reached\n\t *\n\t * @example Graceful application shutdown\n\t * ```typescript\n\t * process.on('SIGTERM', async () => {\n\t * console.log('Shutting down gracefully...');\n\t * await monque.stop(); // Wait for jobs to complete\n\t * await mongoClient.close();\n\t * process.exit(0);\n\t * });\n\t * ```\n\t *\n\t * @example With timeout handling\n\t * ```typescript\n\t * monque.on('job:error', ({ error }) => {\n\t * if (error.name === 'ShutdownTimeoutError') {\n\t * logger.warn('Forced shutdown after timeout:', error.incompleteJobs);\n\t * }\n\t * });\n\t *\n\t * await monque.stop();\n\t * ```\n\t */\n\n\tasync stop(): Promise<void> {\n\t\tif (!this.isRunning) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.isRunning = false;\n\n\t\t// Close change stream\n\t\tawait this.changeStreamHandler.close();\n\n\t\tif (this.cleanupIntervalId) {\n\t\t\tclearInterval(this.cleanupIntervalId);\n\t\t\tthis.cleanupIntervalId = null;\n\t\t}\n\n\t\t// Clear polling interval\n\t\tif (this.pollIntervalId) {\n\t\t\tclearInterval(this.pollIntervalId);\n\t\t\tthis.pollIntervalId = null;\n\t\t}\n\n\t\t// Clear heartbeat interval\n\t\tif (this.heartbeatIntervalId) {\n\t\t\tclearInterval(this.heartbeatIntervalId);\n\t\t\tthis.heartbeatIntervalId = null;\n\t\t}\n\n\t\t// Wait for all active jobs to complete (with timeout)\n\t\tconst activeJobs = this.getActiveJobs();\n\t\tif (activeJobs.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Create a promise that resolves when all jobs are done\n\t\tlet checkInterval: ReturnType<typeof setInterval> | undefined;\n\t\tconst waitForJobs = new Promise<undefined>((resolve) => {\n\t\t\tcheckInterval = setInterval(() => {\n\t\t\t\tif (this.getActiveJobs().length === 0) {\n\t\t\t\t\tclearInterval(checkInterval);\n\t\t\t\t\tresolve(undefined);\n\t\t\t\t}\n\t\t\t}, 100);\n\t\t});\n\n\t\t// Race between job completion and timeout\n\t\tconst timeout = new Promise<'timeout'>((resolve) => {\n\t\t\tsetTimeout(() => resolve('timeout'), this.options.shutdownTimeout);\n\t\t});\n\n\t\tlet result: undefined | 'timeout';\n\n\t\ttry {\n\t\t\tresult = await Promise.race([waitForJobs, timeout]);\n\t\t} finally {\n\t\t\tif (checkInterval) {\n\t\t\t\tclearInterval(checkInterval);\n\t\t\t}\n\t\t}\n\n\t\tif (result === 'timeout') {\n\t\t\tconst incompleteJobs = this.getActiveJobsList();\n\n\t\t\tconst error = new ShutdownTimeoutError(\n\t\t\t\t`Shutdown timed out after ${this.options.shutdownTimeout}ms with ${incompleteJobs.length} incomplete jobs`,\n\t\t\t\tincompleteJobs,\n\t\t\t);\n\t\t\tthis.emit('job:error', { error });\n\t\t}\n\t}\n\n\t/**\n\t * Check if the scheduler is healthy (running and connected).\n\t *\n\t * Returns `true` when the scheduler is started, initialized, and has an active\n\t * MongoDB collection reference. Useful for health check endpoints and monitoring.\n\t *\n\t * A healthy scheduler:\n\t * - Has called `initialize()` successfully\n\t * - Has called `start()` and is actively polling\n\t * - Has a valid MongoDB collection reference\n\t *\n\t * @returns `true` if scheduler is running and connected, `false` otherwise\n\t *\n\t * @example Express health check endpoint\n\t * ```typescript\n\t * app.get('/health', (req, res) => {\n\t * const healthy = monque.isHealthy();\n\t * res.status(healthy ? 200 : 503).json({\n\t * status: healthy ? 'ok' : 'unavailable',\n\t * scheduler: healthy,\n\t * timestamp: new Date().toISOString()\n\t * });\n\t * });\n\t * ```\n\t *\n\t * @example Kubernetes readiness probe\n\t * ```typescript\n\t * app.get('/readyz', (req, res) => {\n\t * if (monque.isHealthy() && dbConnected) {\n\t * res.status(200).send('ready');\n\t * } else {\n\t * res.status(503).send('not ready');\n\t * }\n\t * });\n\t * ```\n\t *\n\t * @example Periodic health monitoring\n\t * ```typescript\n\t * setInterval(() => {\n\t * if (!monque.isHealthy()) {\n\t * logger.error('Scheduler unhealthy');\n\t * metrics.increment('scheduler.unhealthy');\n\t * }\n\t * }, 60000); // Check every minute\n\t * ```\n\t */\n\tisHealthy(): boolean {\n\t\treturn this.isRunning && this.isInitialized && this.collection !== null;\n\t}\n\n\t// ─────────────────────────────────────────────────────────────────────────────\n\t// Private Helpers\n\t// ─────────────────────────────────────────────────────────────────────────────\n\n\t/**\n\t * Ensure the scheduler is initialized before operations.\n\t *\n\t * @private\n\t * @throws {ConnectionError} If scheduler not initialized or collection unavailable\n\t */\n\tprivate ensureInitialized(): void {\n\t\tif (!this.isInitialized || !this.collection) {\n\t\t\tthrow new ConnectionError('Monque not initialized. Call initialize() first.');\n\t\t}\n\t}\n\n\t/**\n\t * Get array of active job IDs across all workers.\n\t *\n\t * @private\n\t * @returns Array of job ID strings currently being processed\n\t */\n\tprivate getActiveJobs(): string[] {\n\t\tconst activeJobs: string[] = [];\n\t\tfor (const worker of this.workers.values()) {\n\t\t\tactiveJobs.push(...worker.activeJobs.keys());\n\t\t}\n\t\treturn activeJobs;\n\t}\n\n\t/**\n\t * Get list of active job documents (for shutdown timeout error).\n\t *\n\t * @private\n\t * @returns Array of active Job objects\n\t */\n\tprivate getActiveJobsList(): Job[] {\n\t\tconst activeJobs: Job[] = [];\n\t\tfor (const worker of this.workers.values()) {\n\t\t\tactiveJobs.push(...worker.activeJobs.values());\n\t\t}\n\t\treturn activeJobs;\n\t}\n\n\t/**\n\t * Convert a MongoDB document to a typed PersistedJob object.\n\t *\n\t * Maps raw MongoDB document fields to the strongly-typed `PersistedJob<T>` interface,\n\t * ensuring type safety and handling optional fields (`lockedAt`, `failReason`, etc.).\n\t *\n\t * @private\n\t * @template T - The job data payload type\n\t * @param doc - The raw MongoDB document with `_id`\n\t * @returns A strongly-typed PersistedJob object with guaranteed `_id`\n\t */\n\tprivate documentToPersistedJob<T>(doc: WithId<Document>): PersistedJob<T> {\n\t\tconst job: PersistedJob<T> = {\n\t\t\t_id: doc._id,\n\t\t\tname: doc['name'] as string,\n\t\t\tdata: doc['data'] as T,\n\t\t\tstatus: doc['status'] as JobStatusType,\n\t\t\tnextRunAt: doc['nextRunAt'] as Date,\n\t\t\tfailCount: doc['failCount'] as number,\n\t\t\tcreatedAt: doc['createdAt'] as Date,\n\t\t\tupdatedAt: doc['updatedAt'] as Date,\n\t\t};\n\n\t\t// Only set optional properties if they exist\n\t\tif (doc['lockedAt'] !== undefined) {\n\t\t\tjob.lockedAt = doc['lockedAt'] as Date | null;\n\t\t}\n\t\tif (doc['claimedBy'] !== undefined) {\n\t\t\tjob.claimedBy = doc['claimedBy'] as string | null;\n\t\t}\n\t\tif (doc['lastHeartbeat'] !== undefined) {\n\t\t\tjob.lastHeartbeat = doc['lastHeartbeat'] as Date | null;\n\t\t}\n\t\tif (doc['heartbeatInterval'] !== undefined) {\n\t\t\tjob.heartbeatInterval = doc['heartbeatInterval'] as number;\n\t\t}\n\t\tif (doc['failReason'] !== undefined) {\n\t\t\tjob.failReason = doc['failReason'] as string;\n\t\t}\n\t\tif (doc['repeatInterval'] !== undefined) {\n\t\t\tjob.repeatInterval = doc['repeatInterval'] as string;\n\t\t}\n\t\tif (doc['uniqueKey'] !== undefined) {\n\t\t\tjob.uniqueKey = doc['uniqueKey'] as string;\n\t\t}\n\n\t\treturn job;\n\t}\n\n\t/**\n\t * Type-safe event emitter methods\n\t */\n\toverride emit<K extends keyof MonqueEventMap>(event: K, payload: MonqueEventMap[K]): boolean {\n\t\treturn super.emit(event, payload);\n\t}\n\n\toverride on<K extends keyof MonqueEventMap>(\n\t\tevent: K,\n\t\tlistener: (payload: MonqueEventMap[K]) => void,\n\t): this {\n\t\treturn super.on(event, listener);\n\t}\n\n\toverride once<K extends keyof MonqueEventMap>(\n\t\tevent: K,\n\t\tlistener: (payload: MonqueEventMap[K]) => void,\n\t): this {\n\t\treturn super.once(event, listener);\n\t}\n\n\toverride off<K extends keyof MonqueEventMap>(\n\t\tevent: K,\n\t\tlistener: (payload: MonqueEventMap[K]) => void,\n\t): this {\n\t\treturn super.off(event, listener);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAmBA,MAAa,YAAY;CAExB,SAAS;CAET,YAAY;CAEZ,WAAW;CAEX,QAAQ;CAER,WAAW;CACX;;;;;;;;;AAoMD,MAAa,kBAAkB;CAC9B,SAAS;CACT,UAAU;CACV;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACjMD,SAAgB,eAAkB,KAAqC;AACtE,QAAO,SAAS,OAAO,IAAI,QAAQ,UAAa,IAAI,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmC7D,SAAgB,iBAAiB,OAAwC;AACxE,QAAO,OAAO,UAAU,YAAY,OAAO,OAAO,UAAU,CAAC,SAAS,MAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;AA2B9F,SAAgB,aAAgB,KAAsB;AACrD,QAAO,IAAI,WAAW,UAAU;;;;;;;;;;;;;;;;;;;AAoBjC,SAAgB,gBAAmB,KAAsB;AACxD,QAAO,IAAI,WAAW,UAAU;;;;;;;;;;;;;;;;;;;AAoBjC,SAAgB,eAAkB,KAAsB;AACvD,QAAO,IAAI,WAAW,UAAU;;;;;;;;;;;;;;;;;;;;;;;AAwBjC,SAAgB,YAAe,KAAsB;AACpD,QAAO,IAAI,WAAW,UAAU;;;;;;;;;;;;;;;;;;;AAoBjC,SAAgB,eAAkB,KAAsB;AACvD,QAAO,IAAI,WAAW,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BjC,SAAgB,eAAkB,KAAsB;AACvD,QAAO,IAAI,mBAAmB,UAAa,IAAI,mBAA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