@contractspec/lib.jobs 1.46.1 → 1.47.0
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/dist/contracts/index.d.ts +139 -139
- package/dist/contracts/index.d.ts.map +1 -1
- package/dist/entities/index.d.ts +116 -116
- package/dist/entities/index.d.ts.map +1 -1
- package/dist/entities/index.js.map +1 -1
- package/dist/events.d.ts +98 -98
- package/dist/events.d.ts.map +1 -1
- package/dist/handlers/ping-job.js.map +1 -1
- package/dist/jobs.capability.d.ts +8 -0
- package/dist/jobs.capability.d.ts.map +1 -0
- package/dist/jobs.capability.js +33 -0
- package/dist/jobs.capability.js.map +1 -0
- package/dist/jobs.feature.d.ts +4 -4
- package/dist/jobs.feature.d.ts.map +1 -1
- package/dist/jobs.feature.js +11 -4
- package/dist/jobs.feature.js.map +1 -1
- package/dist/queue/gcp-cloud-tasks.js.map +1 -1
- package/dist/queue/gcp-pubsub.js.map +1 -1
- package/dist/queue/memory-queue.js.map +1 -1
- package/dist/queue/register-defined-job.js.map +1 -1
- package/dist/queue/scaleway-sqs-queue.js.map +1 -1
- package/dist/scheduler/index.js.map +1 -1
- package/package.json +10 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gcp-pubsub.js","names":["
|
|
1
|
+
{"version":3,"file":"gcp-pubsub.js","names":["DEFAULT_RETRY_POLICY"],"sources":["../../src/queue/gcp-pubsub.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\n\nimport {\n DEFAULT_RETRY_POLICY,\n type EnqueueOptions,\n type Job,\n type JobHandler,\n type JobQueue,\n} from './types';\n\ninterface PubSubClientLike {\n topic(name: string): {\n publishMessage(message: { data: Buffer }): Promise<string>;\n };\n}\n\nexport interface GcpPubSubQueueOptions {\n client: PubSubClientLike;\n topicName: string;\n}\n\nexport class GcpPubSubQueue implements JobQueue {\n private readonly handlers = new Map<string, JobHandler>();\n\n constructor(private readonly options: GcpPubSubQueueOptions) {}\n\n async enqueue<TPayload>(\n jobType: string,\n payload: TPayload,\n options: EnqueueOptions = {}\n ): Promise<Job<TPayload>> {\n const now = new Date();\n await this.options.client.topic(this.options.topicName).publishMessage({\n data: Buffer.from(\n JSON.stringify({\n id: randomUUID(),\n type: jobType,\n payload,\n }),\n 'utf-8'\n ),\n });\n\n return {\n id: randomUUID(),\n type: jobType,\n version: '1.0.0',\n payload,\n status: 'pending',\n priority: options.priority ?? 0,\n attempts: 0,\n maxRetries: options.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,\n createdAt: now,\n updatedAt: now,\n scheduledAt: options.delaySeconds\n ? new Date(now.getTime() + options.delaySeconds * 1000)\n : now,\n dedupeKey: options.dedupeKey,\n tenantId: options.tenantId,\n userId: options.userId,\n traceId: options.traceId,\n metadata: options.metadata,\n };\n }\n\n register<TPayload, TResult = void>(\n jobType: string,\n handler: JobHandler<TPayload, TResult>\n ): void {\n this.handlers.set(jobType, handler as JobHandler);\n }\n\n start(): void {\n // Message consumption handled externally via Pub/Sub subscription.\n }\n\n async stop(): Promise<void> {\n this.handlers.clear();\n }\n}\n"],"mappings":";;;;AAqBA,IAAa,iBAAb,MAAgD;CAC9C,AAAiB,2BAAW,IAAI,KAAyB;CAEzD,YAAY,AAAiB,SAAgC;EAAhC;;CAE7B,MAAM,QACJ,SACA,SACA,UAA0B,EAAE,EACJ;EACxB,MAAM,sBAAM,IAAI,MAAM;AACtB,QAAM,KAAK,QAAQ,OAAO,MAAM,KAAK,QAAQ,UAAU,CAAC,eAAe,EACrE,MAAM,OAAO,KACX,KAAK,UAAU;GACb,IAAI,YAAY;GAChB,MAAM;GACN;GACD,CAAC,EACF,QACD,EACF,CAAC;AAEF,SAAO;GACL,IAAI,YAAY;GAChB,MAAM;GACN,SAAS;GACT;GACA,QAAQ;GACR,UAAU,QAAQ,YAAY;GAC9B,UAAU;GACV,YAAY,QAAQ,cAAcA,mCAAqB;GACvD,WAAW;GACX,WAAW;GACX,aAAa,QAAQ,eACjB,IAAI,KAAK,IAAI,SAAS,GAAG,QAAQ,eAAe,IAAK,GACrD;GACJ,WAAW,QAAQ;GACnB,UAAU,QAAQ;GAClB,QAAQ,QAAQ;GAChB,SAAS,QAAQ;GACjB,UAAU,QAAQ;GACnB;;CAGH,SACE,SACA,SACM;AACN,OAAK,SAAS,IAAI,SAAS,QAAsB;;CAGnD,QAAc;CAId,MAAM,OAAsB;AAC1B,OAAK,SAAS,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-queue.js","names":["DEFAULT_RETRY_POLICY"
|
|
1
|
+
{"version":3,"file":"memory-queue.js","names":["DEFAULT_RETRY_POLICY"],"sources":["../../src/queue/memory-queue.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\nimport type {\n Job,\n JobHandler,\n JobQueue,\n EnqueueOptions,\n QueueStats,\n RetryPolicy,\n} from './types';\nimport { calculateBackoff, DEFAULT_RETRY_POLICY } from './types';\n\nexport interface MemoryQueueOptions {\n /** Poll interval in milliseconds */\n pollIntervalMs?: number;\n /** Maximum concurrent jobs */\n concurrency?: number;\n /** Default retry policy */\n retryPolicy?: RetryPolicy;\n}\n\n/**\n * In-memory job queue for development and testing.\n */\nexport class MemoryJobQueue implements JobQueue {\n private readonly jobs = new Map<string, Job>();\n private readonly handlers = new Map<string, JobHandler>();\n private timer?: ReturnType<typeof setInterval>;\n private activeCount = 0;\n private readonly pollIntervalMs: number;\n private readonly concurrency: number;\n private readonly retryPolicy: RetryPolicy;\n\n constructor(options: MemoryQueueOptions = {}) {\n this.pollIntervalMs = options.pollIntervalMs ?? 200;\n this.concurrency = options.concurrency ?? 5;\n this.retryPolicy = options.retryPolicy ?? DEFAULT_RETRY_POLICY;\n }\n\n async enqueue<TPayload>(\n jobType: string,\n payload: TPayload,\n options: EnqueueOptions = {}\n ): Promise<Job<TPayload>> {\n // Check for duplicate\n if (options.dedupeKey) {\n const existing = Array.from(this.jobs.values()).find(\n (j) => j.dedupeKey === options.dedupeKey && j.status === 'pending'\n );\n if (existing) {\n return existing as Job<TPayload>;\n }\n }\n\n const now = new Date();\n const scheduledAt = options.delaySeconds\n ? new Date(now.getTime() + options.delaySeconds * 1000)\n : now;\n\n const job: Job<TPayload> = {\n id: randomUUID(),\n type: jobType,\n version: '1.0.0',\n payload,\n status: 'pending',\n priority: options.priority ?? 0,\n attempts: 0,\n maxRetries: options.maxRetries ?? this.retryPolicy.maxRetries,\n createdAt: now,\n updatedAt: now,\n scheduledAt,\n dedupeKey: options.dedupeKey,\n tenantId: options.tenantId,\n userId: options.userId,\n traceId: options.traceId,\n metadata: options.metadata,\n };\n\n if (options.timeoutMs) {\n job.timeoutAt = new Date(now.getTime() + options.timeoutMs);\n }\n\n this.jobs.set(job.id, job);\n return job;\n }\n\n register<TPayload, TResult = void>(\n jobType: string,\n handler: JobHandler<TPayload, TResult>\n ): void {\n this.handlers.set(jobType, handler as JobHandler);\n }\n\n start(): void {\n if (this.timer) return;\n this.timer = setInterval(() => {\n void this.processNext();\n }, this.pollIntervalMs);\n }\n\n async stop(): Promise<void> {\n if (this.timer) {\n clearInterval(this.timer);\n this.timer = undefined;\n }\n // Wait for active jobs to complete\n while (this.activeCount > 0) {\n await new Promise((resolve) => setTimeout(resolve, 50));\n }\n }\n\n async getJob(jobId: string): Promise<Job | null> {\n return this.jobs.get(jobId) ?? null;\n }\n\n async cancelJob(jobId: string): Promise<boolean> {\n const job = this.jobs.get(jobId);\n if (!job || job.status !== 'pending') {\n return false;\n }\n job.status = 'cancelled';\n job.updatedAt = new Date();\n return true;\n }\n\n async getStats(): Promise<QueueStats> {\n const stats: QueueStats = {\n pending: 0,\n running: 0,\n completed: 0,\n failed: 0,\n deadLetter: 0,\n };\n\n for (const job of this.jobs.values()) {\n switch (job.status) {\n case 'pending':\n stats.pending++;\n break;\n case 'running':\n stats.running++;\n break;\n case 'completed':\n stats.completed++;\n break;\n case 'failed':\n stats.failed++;\n break;\n case 'dead_letter':\n stats.deadLetter++;\n break;\n }\n }\n\n return stats;\n }\n\n private async processNext(): Promise<void> {\n if (this.activeCount >= this.concurrency) return;\n\n const now = new Date();\n const pendingJobs = Array.from(this.jobs.values())\n .filter(\n (j) =>\n j.status === 'pending' && (!j.scheduledAt || j.scheduledAt <= now)\n )\n .sort((a, b) => {\n // Higher priority first\n if (a.priority !== b.priority) {\n return b.priority - a.priority;\n }\n // Earlier scheduled first\n return (\n (a.scheduledAt?.getTime() ?? 0) - (b.scheduledAt?.getTime() ?? 0)\n );\n });\n\n const job = pendingJobs[0];\n if (!job) return;\n\n const handler = this.handlers.get(job.type);\n if (!handler) return;\n\n this.activeCount++;\n job.status = 'running';\n job.startedAt = new Date();\n job.updatedAt = new Date();\n job.attempts += 1;\n\n try {\n const result = await handler(job);\n job.status = 'completed';\n job.completedAt = new Date();\n job.result = result;\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : 'Unknown error';\n job.lastError = errorMessage;\n\n if (job.attempts >= job.maxRetries) {\n job.status = 'dead_letter';\n } else {\n // Schedule retry with backoff\n const backoff = calculateBackoff(job.attempts, this.retryPolicy);\n job.status = 'pending';\n job.scheduledAt = new Date(Date.now() + backoff);\n }\n } finally {\n job.updatedAt = new Date();\n this.activeCount--;\n }\n }\n}\n"],"mappings":";;;;;;;AAuBA,IAAa,iBAAb,MAAgD;CAC9C,AAAiB,uBAAO,IAAI,KAAkB;CAC9C,AAAiB,2BAAW,IAAI,KAAyB;CACzD,AAAQ;CACR,AAAQ,cAAc;CACtB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,UAA8B,EAAE,EAAE;AAC5C,OAAK,iBAAiB,QAAQ,kBAAkB;AAChD,OAAK,cAAc,QAAQ,eAAe;AAC1C,OAAK,cAAc,QAAQ,eAAeA;;CAG5C,MAAM,QACJ,SACA,SACA,UAA0B,EAAE,EACJ;AAExB,MAAI,QAAQ,WAAW;GACrB,MAAM,WAAW,MAAM,KAAK,KAAK,KAAK,QAAQ,CAAC,CAAC,MAC7C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4BAAY,IAAI,MAAM;AAC1B,QAAK"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"register-defined-job.js","names":[
|
|
1
|
+
{"version":3,"file":"register-defined-job.js","names":[],"sources":["../../src/queue/register-defined-job.ts"],"sourcesContent":["import type { DefinedJob } from '@contractspec/lib.contracts/jobs/define-job';\nimport type {\n Job,\n JobHandler,\n JobQueue,\n} from '@contractspec/lib.contracts/jobs/queue';\n\nexport function registerDefinedJob<TPayload>(\n queue: JobQueue,\n def: DefinedJob<TPayload>\n): void {\n const wrapped: JobHandler<unknown> = async (job) => {\n const payload = def.schema.parse(job.payload);\n const typedJob: Job<TPayload> = {\n ...(job as Job<unknown>),\n payload,\n } as Job<TPayload>;\n\n await def.handler(payload, typedJob);\n };\n\n queue.register<TPayload>(def.type, wrapped as JobHandler<TPayload>);\n}\n"],"mappings":";AAOA,SAAgB,mBACd,OACA,KACM;CACN,MAAM,UAA+B,OAAO,QAAQ;EAClD,MAAM,UAAU,IAAI,OAAO,MAAM,IAAI,QAAQ;EAC7C,MAAM,WAA0B;GAC9B,GAAI;GACJ;GACD;AAED,QAAM,IAAI,QAAQ,SAAS,SAAS;;AAGtC,OAAM,SAAmB,IAAI,MAAM,QAAgC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scaleway-sqs-queue.js","names":["envelope: RawJobEnvelope<TPayload>","DEFAULT_RETRY_POLICY","envelope: RawJobEnvelope","job: Job"],"sources":["../../src/queue/scaleway-sqs-queue.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\nimport {\n DeleteMessageCommand,\n ReceiveMessageCommand,\n SendMessageCommand,\n SQSClient,\n} from '@aws-sdk/client-sqs';\nimport type { Logger } from '@contractspec/lib.logger';\nimport { DEFAULT_RETRY_POLICY } from './types';\nimport type { EnqueueOptions, Job, JobHandler, JobQueue } from './types';\n\nexport interface ScalewaySqsQueueCredentials {\n accessKeyId: string;\n secretAccessKey: string;\n}\n\nexport interface ScalewaySqsQueueConfig {\n queueUrl: string;\n region?: string;\n endpoint?: string;\n waitTimeSeconds?: number;\n maxNumberOfMessages?: number;\n visibilityTimeoutSeconds?: number;\n credentials?: ScalewaySqsQueueCredentials;\n logger?: Logger;\n}\n\ninterface RawJobEnvelope<TPayload = unknown> {\n id: string;\n type: string;\n payload: TPayload;\n}\n\nexport class ScalewaySqsJobQueue implements JobQueue {\n private readonly sqs: SQSClient;\n private readonly queueUrl: string;\n private readonly waitTimeSeconds: number;\n private readonly maxNumberOfMessages: number;\n private readonly visibilityTimeoutSeconds: number;\n private readonly handlers = new Map<string, JobHandler>();\n private readonly logger?: Logger;\n private running = false;\n\n constructor(config: ScalewaySqsQueueConfig) {\n this.logger = config.logger;\n\n const accessKeyId =\n config.credentials?.accessKeyId ?? process.env.SCALEWAY_ACCESS_KEY_QUEUE;\n const secretAccessKey =\n config.credentials?.secretAccessKey ??\n process.env.SCALEWAY_SECRET_KEY_QUEUE;\n\n if (!accessKeyId || !secretAccessKey) {\n throw new Error(\n 'Missing SCALEWAY_ACCESS_KEY_QUEUE / SCALEWAY_SECRET_KEY_QUEUE in env'\n );\n }\n\n const region = config.region ?? process.env.SCALEWAY_REGION ?? 'par';\n const endpoint = config.endpoint ?? 'https://sqs.mnq.fr-par.scaleway.com';\n\n this.sqs = new SQSClient({\n region,\n endpoint,\n credentials: {\n accessKeyId,\n secretAccessKey,\n },\n });\n\n this.queueUrl = config.queueUrl;\n this.waitTimeSeconds = config.waitTimeSeconds ?? 20;\n this.maxNumberOfMessages = config.maxNumberOfMessages ?? 5;\n this.visibilityTimeoutSeconds = config.visibilityTimeoutSeconds ?? 60;\n }\n\n async enqueue<TPayload>(\n jobType: string,\n payload: TPayload,\n options: EnqueueOptions = {}\n ): Promise<Job<TPayload>> {\n const id = randomUUID();\n const now = new Date();\n const scheduledAt = options.delaySeconds\n ? new Date(now.getTime() + options.delaySeconds * 1000)\n : now;\n\n const envelope: RawJobEnvelope<TPayload> = {\n id,\n type: jobType,\n payload,\n };\n\n await this.sqs.send(\n new SendMessageCommand({\n QueueUrl: this.queueUrl,\n MessageBody: JSON.stringify(envelope),\n DelaySeconds: options.delaySeconds ?? 0,\n // If you use FIFO queues later, you'd set MessageGroupId / MessageDeduplicationId here.\n })\n );\n\n return {\n id,\n type: jobType,\n version: '1.0.0',\n payload,\n status: 'pending',\n priority: options.priority ?? 0,\n attempts: 0,\n maxRetries: options.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,\n createdAt: now,\n updatedAt: now,\n scheduledAt,\n dedupeKey: options.dedupeKey,\n tenantId: options.tenantId,\n userId: options.userId,\n traceId: options.traceId,\n metadata: options.metadata,\n };\n }\n\n register<TPayload, TResult = void>(\n jobType: string,\n handler: JobHandler<TPayload, TResult>\n ): void {\n if (this.handlers.has(jobType)) {\n throw new Error(`Handler already registered for job type \"${jobType}\"`);\n }\n this.handlers.set(jobType, handler as JobHandler);\n }\n\n start(): void {\n if (this.running) return;\n this.running = true;\n void this.pollLoop().catch((error) => {\n this.logger?.error?.('jobs.queue.scaleway_sqs.poll_loop_fatal', {\n error: error instanceof Error ? error.message : String(error),\n });\n this.running = false;\n });\n }\n\n async stop(): Promise<void> {\n this.running = false;\n // Worst-case we wait for the current ReceiveMessage to finish (<= waitTimeSeconds)\n }\n\n private async pollLoop(): Promise<void> {\n this.logger?.info?.('jobs.queue.scaleway_sqs.started', {\n queueUrl: this.queueUrl,\n });\n\n while (this.running) {\n try {\n const res = await this.sqs.send(\n new ReceiveMessageCommand({\n QueueUrl: this.queueUrl,\n MaxNumberOfMessages: this.maxNumberOfMessages,\n WaitTimeSeconds: this.waitTimeSeconds,\n VisibilityTimeout: this.visibilityTimeoutSeconds,\n MessageSystemAttributeNames: ['ApproximateReceiveCount'],\n })\n );\n\n const messages = res.Messages ?? [];\n\n if (messages.length === 0) {\n continue;\n }\n\n for (const msg of messages) {\n if (!msg.Body || !msg.ReceiptHandle) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.invalid_message', {\n messageId: msg.MessageId,\n reason: 'missing_body_or_receipt',\n });\n continue;\n }\n\n let envelope: RawJobEnvelope;\n\n try {\n envelope = JSON.parse(msg.Body) as RawJobEnvelope;\n } catch (err) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.parse_failed', {\n messageId: msg.MessageId,\n error: err instanceof Error ? err.message : String(err),\n });\n await this.deleteMessage(msg.ReceiptHandle);\n continue;\n }\n\n const handler = this.handlers.get(envelope.type);\n if (!handler) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.missing_handler', {\n jobType: envelope.type,\n messageId: msg.MessageId,\n });\n await this.deleteMessage(msg.ReceiptHandle);\n continue;\n }\n\n const now = new Date();\n const attempts = parseInt(\n (msg.Attributes?.ApproximateReceiveCount as string | undefined) ??\n '1',\n 10\n );\n\n const job: Job = {\n id: envelope.id,\n type: envelope.type,\n version: '1.0.0',\n payload: envelope.payload,\n status: 'pending',\n priority: 0,\n attempts,\n maxRetries: DEFAULT_RETRY_POLICY.maxRetries,\n createdAt: now,\n updatedAt: now,\n };\n\n job.status = 'running';\n job.updatedAt = new Date();\n\n try {\n await handler(job);\n job.status = 'completed';\n job.updatedAt = new Date();\n await this.deleteMessage(msg.ReceiptHandle);\n } catch (err) {\n job.status = 'failed';\n job.lastError =\n err instanceof Error ? err.message : 'Unknown job error';\n job.updatedAt = new Date();\n\n this.logger?.error?.('jobs.queue.scaleway_sqs.job_failed', {\n jobType: job.type,\n jobId: job.id,\n error: err instanceof Error ? err.message : String(err),\n });\n // Do NOT delete message on failure:\n // - SQS/Scaleway will redeliver until MaxReceiveCount, then DLQ takes over.\n }\n }\n } catch (err) {\n this.logger?.error?.('jobs.queue.scaleway_sqs.poll_error', {\n error: err instanceof Error ? err.message : String(err),\n });\n await this.sleep(5000);\n }\n }\n\n this.logger?.info?.('jobs.queue.scaleway_sqs.stopped', {\n queueUrl: this.queueUrl,\n });\n }\n\n private async deleteMessage(receiptHandle: string): Promise<void> {\n try {\n await this.sqs.send(\n new DeleteMessageCommand({\n QueueUrl: this.queueUrl,\n ReceiptHandle: receiptHandle,\n })\n );\n } catch (err) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.delete_failed', {\n error: err instanceof Error ? err.message : String(err),\n });\n }\n }\n\n private async sleep(ms: number): Promise<void> {\n await new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n"],"mappings":";;;;;AAiCA,IAAa,sBAAb,MAAqD;CACnD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB,2BAAW,IAAI,KAAyB;CACzD,AAAiB;CACjB,AAAQ,UAAU;CAElB,YAAY,QAAgC;AAC1C,OAAK,SAAS,OAAO;EAErB,MAAM,cACJ,OAAO,aAAa,eAAe,QAAQ,IAAI;EACjD,MAAM,kBACJ,OAAO,aAAa,mBACpB,QAAQ,IAAI;AAEd,MAAI,CAAC,eAAe,CAAC,gBACnB,OAAM,IAAI,MACR,uEACD;AAMH,OAAK,MAAM,IAAI,UAAU;GACvB,QAJa,OAAO,UAAU,QAAQ,IAAI,mBAAmB;GAK7D,UAJe,OAAO,YAAY;GAKlC,aAAa;IACX;IACA;IACD;GACF,CAAC;AAEF,OAAK,WAAW,OAAO;AACvB,OAAK,kBAAkB,OAAO,mBAAmB;AACjD,OAAK,sBAAsB,OAAO,uBAAuB;AACzD,OAAK,2BAA2B,OAAO,4BAA4B;;CAGrE,MAAM,QACJ,SACA,SACA,UAA0B,EAAE,EACJ;EACxB,MAAM,KAAK,YAAY;EACvB,MAAM,sBAAM,IAAI,MAAM;EACtB,MAAM,cAAc,QAAQ,eACxB,IAAI,KAAK,IAAI,SAAS,GAAG,QAAQ,eAAe,IAAK,GACrD;EAEJ,MAAMA,WAAqC;GACzC;GACA,MAAM;GACN;GACD;AAED,QAAM,KAAK,IAAI,KACb,IAAI,mBAAmB;GACrB,UAAU,KAAK;GACf,aAAa,KAAK,UAAU,SAAS;GACrC,cAAc,QAAQ,gBAAgB;GAEvC,CAA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Ac,cAAc,eAAsC;AAChE,MAAI;AACF,SAAM,KAAK,IAAI,KACb,IAAI,qBAAqB;IACvB,UAAU,KAAK;IACf,eAAe;IAChB,CAAC,CACH;WACM,KAAK;AACZ,QAAK,QAAQ,OAAO,yCAAyC,EAC3D,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACxD,CAAC;;;CAIN,MAAc,MAAM,IAA2B;AAC7C,QAAM,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
|
1
|
+
{"version":3,"file":"scaleway-sqs-queue.js","names":["DEFAULT_RETRY_POLICY"],"sources":["../../src/queue/scaleway-sqs-queue.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto';\nimport {\n DeleteMessageCommand,\n ReceiveMessageCommand,\n SendMessageCommand,\n SQSClient,\n} from '@aws-sdk/client-sqs';\nimport type { Logger } from '@contractspec/lib.logger';\nimport { DEFAULT_RETRY_POLICY } from './types';\nimport type { EnqueueOptions, Job, JobHandler, JobQueue } from './types';\n\nexport interface ScalewaySqsQueueCredentials {\n accessKeyId: string;\n secretAccessKey: string;\n}\n\nexport interface ScalewaySqsQueueConfig {\n queueUrl: string;\n region?: string;\n endpoint?: string;\n waitTimeSeconds?: number;\n maxNumberOfMessages?: number;\n visibilityTimeoutSeconds?: number;\n credentials?: ScalewaySqsQueueCredentials;\n logger?: Logger;\n}\n\ninterface RawJobEnvelope<TPayload = unknown> {\n id: string;\n type: string;\n payload: TPayload;\n}\n\nexport class ScalewaySqsJobQueue implements JobQueue {\n private readonly sqs: SQSClient;\n private readonly queueUrl: string;\n private readonly waitTimeSeconds: number;\n private readonly maxNumberOfMessages: number;\n private readonly visibilityTimeoutSeconds: number;\n private readonly handlers = new Map<string, JobHandler>();\n private readonly logger?: Logger;\n private running = false;\n\n constructor(config: ScalewaySqsQueueConfig) {\n this.logger = config.logger;\n\n const accessKeyId =\n config.credentials?.accessKeyId ?? process.env.SCALEWAY_ACCESS_KEY_QUEUE;\n const secretAccessKey =\n config.credentials?.secretAccessKey ??\n process.env.SCALEWAY_SECRET_KEY_QUEUE;\n\n if (!accessKeyId || !secretAccessKey) {\n throw new Error(\n 'Missing SCALEWAY_ACCESS_KEY_QUEUE / SCALEWAY_SECRET_KEY_QUEUE in env'\n );\n }\n\n const region = config.region ?? process.env.SCALEWAY_REGION ?? 'par';\n const endpoint = config.endpoint ?? 'https://sqs.mnq.fr-par.scaleway.com';\n\n this.sqs = new SQSClient({\n region,\n endpoint,\n credentials: {\n accessKeyId,\n secretAccessKey,\n },\n });\n\n this.queueUrl = config.queueUrl;\n this.waitTimeSeconds = config.waitTimeSeconds ?? 20;\n this.maxNumberOfMessages = config.maxNumberOfMessages ?? 5;\n this.visibilityTimeoutSeconds = config.visibilityTimeoutSeconds ?? 60;\n }\n\n async enqueue<TPayload>(\n jobType: string,\n payload: TPayload,\n options: EnqueueOptions = {}\n ): Promise<Job<TPayload>> {\n const id = randomUUID();\n const now = new Date();\n const scheduledAt = options.delaySeconds\n ? new Date(now.getTime() + options.delaySeconds * 1000)\n : now;\n\n const envelope: RawJobEnvelope<TPayload> = {\n id,\n type: jobType,\n payload,\n };\n\n await this.sqs.send(\n new SendMessageCommand({\n QueueUrl: this.queueUrl,\n MessageBody: JSON.stringify(envelope),\n DelaySeconds: options.delaySeconds ?? 0,\n // If you use FIFO queues later, you'd set MessageGroupId / MessageDeduplicationId here.\n })\n );\n\n return {\n id,\n type: jobType,\n version: '1.0.0',\n payload,\n status: 'pending',\n priority: options.priority ?? 0,\n attempts: 0,\n maxRetries: options.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,\n createdAt: now,\n updatedAt: now,\n scheduledAt,\n dedupeKey: options.dedupeKey,\n tenantId: options.tenantId,\n userId: options.userId,\n traceId: options.traceId,\n metadata: options.metadata,\n };\n }\n\n register<TPayload, TResult = void>(\n jobType: string,\n handler: JobHandler<TPayload, TResult>\n ): void {\n if (this.handlers.has(jobType)) {\n throw new Error(`Handler already registered for job type \"${jobType}\"`);\n }\n this.handlers.set(jobType, handler as JobHandler);\n }\n\n start(): void {\n if (this.running) return;\n this.running = true;\n void this.pollLoop().catch((error) => {\n this.logger?.error?.('jobs.queue.scaleway_sqs.poll_loop_fatal', {\n error: error instanceof Error ? error.message : String(error),\n });\n this.running = false;\n });\n }\n\n async stop(): Promise<void> {\n this.running = false;\n // Worst-case we wait for the current ReceiveMessage to finish (<= waitTimeSeconds)\n }\n\n private async pollLoop(): Promise<void> {\n this.logger?.info?.('jobs.queue.scaleway_sqs.started', {\n queueUrl: this.queueUrl,\n });\n\n while (this.running) {\n try {\n const res = await this.sqs.send(\n new ReceiveMessageCommand({\n QueueUrl: this.queueUrl,\n MaxNumberOfMessages: this.maxNumberOfMessages,\n WaitTimeSeconds: this.waitTimeSeconds,\n VisibilityTimeout: this.visibilityTimeoutSeconds,\n MessageSystemAttributeNames: ['ApproximateReceiveCount'],\n })\n );\n\n const messages = res.Messages ?? [];\n\n if (messages.length === 0) {\n continue;\n }\n\n for (const msg of messages) {\n if (!msg.Body || !msg.ReceiptHandle) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.invalid_message', {\n messageId: msg.MessageId,\n reason: 'missing_body_or_receipt',\n });\n continue;\n }\n\n let envelope: RawJobEnvelope;\n\n try {\n envelope = JSON.parse(msg.Body) as RawJobEnvelope;\n } catch (err) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.parse_failed', {\n messageId: msg.MessageId,\n error: err instanceof Error ? err.message : String(err),\n });\n await this.deleteMessage(msg.ReceiptHandle);\n continue;\n }\n\n const handler = this.handlers.get(envelope.type);\n if (!handler) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.missing_handler', {\n jobType: envelope.type,\n messageId: msg.MessageId,\n });\n await this.deleteMessage(msg.ReceiptHandle);\n continue;\n }\n\n const now = new Date();\n const attempts = parseInt(\n (msg.Attributes?.ApproximateReceiveCount as string | undefined) ??\n '1',\n 10\n );\n\n const job: Job = {\n id: envelope.id,\n type: envelope.type,\n version: '1.0.0',\n payload: envelope.payload,\n status: 'pending',\n priority: 0,\n attempts,\n maxRetries: DEFAULT_RETRY_POLICY.maxRetries,\n createdAt: now,\n updatedAt: now,\n };\n\n job.status = 'running';\n job.updatedAt = new Date();\n\n try {\n await handler(job);\n job.status = 'completed';\n job.updatedAt = new Date();\n await this.deleteMessage(msg.ReceiptHandle);\n } catch (err) {\n job.status = 'failed';\n job.lastError =\n err instanceof Error ? err.message : 'Unknown job error';\n job.updatedAt = new Date();\n\n this.logger?.error?.('jobs.queue.scaleway_sqs.job_failed', {\n jobType: job.type,\n jobId: job.id,\n error: err instanceof Error ? err.message : String(err),\n });\n // Do NOT delete message on failure:\n // - SQS/Scaleway will redeliver until MaxReceiveCount, then DLQ takes over.\n }\n }\n } catch (err) {\n this.logger?.error?.('jobs.queue.scaleway_sqs.poll_error', {\n error: err instanceof Error ? err.message : String(err),\n });\n await this.sleep(5000);\n }\n }\n\n this.logger?.info?.('jobs.queue.scaleway_sqs.stopped', {\n queueUrl: this.queueUrl,\n });\n }\n\n private async deleteMessage(receiptHandle: string): Promise<void> {\n try {\n await this.sqs.send(\n new DeleteMessageCommand({\n QueueUrl: this.queueUrl,\n ReceiptHandle: receiptHandle,\n })\n );\n } catch (err) {\n this.logger?.warn?.('jobs.queue.scaleway_sqs.delete_failed', {\n error: err instanceof Error ? err.message : String(err),\n });\n }\n }\n\n private async sleep(ms: number): Promise<void> {\n await new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n"],"mappings":";;;;;AAiCA,IAAa,sBAAb,MAAqD;CACnD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB,2BAAW,IAAI,KAAyB;CACzD,AAAiB;CACjB,AAAQ,UAAU;CAElB,YAAY,QAAgC;AAC1C,OAAK,SAAS,OAAO;EAErB,MAAM,cACJ,OAAO,aAAa,eAAe,QAAQ,IAAI;EACjD,MAAM,kBACJ,OAAO,aAAa,mBACpB,QAAQ,IA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ClB,QAAQ;KACR,UAAU;KACV;KACA,YAAYA,mCAAqB;KACjC,WAAW;KACX,WAAW;KACZ;AAED,QAAI,SAAS;AACb,QAAI,4BAAY,IAAI,MAAM;AAE1B,QAAI;AACF,WAAM,QAAQ,IAAI;AAClB,SAAI,SAAS;AACb,SAAI,4BAAY,IAAI,MAAM;AAC1B,WAAM,KAAK,cAAc,IAAI,cAAc;aACpC,KAAK;AACZ,SAAI,SAAS;AACb,SAAI,YACF,eAAe,QAAQ,IAAI,UAAU;AACvC,SAAI,4BAAY,IAAI,MAAM;AAE1B,UAAK,QAAQ,QAAQ,sCAAsC;MACzD,SAAS,IAAI;MACb,OAAO,IAAI;MACX,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI;MACxD,CAAC;;;WAKC,KAAK;AACZ,QAAK,QAAQ,QAAQ,sCAAsC,EACzD,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACxD,CAAC;AACF,SAAM,KAAK,MAAM,IAAK;;AAI1B,OAAK,QAAQ,OAAO,mCAAmC,EACrD,UAAU,KAAK,UAChB,CAAC;;CAGJ,MAAc,cAAc,eAAsC;AAChE,MAAI;AACF,SAAM,KAAK,IAAI,KACb,IAAI,qBAAqB;IACvB,UAAU,KAAK;IACf,eAAe;IAChB,CAAC,CACH;WACM,KAAK;AACZ,QAAK,QAAQ,OAAO,yCAAyC,EAC3D,OAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACxD,CAAC;;;CAIN,MAAc,MAAM,IAA2B;AAC7C,QAAM,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/scheduler/index.ts"],"sourcesContent":["import type { JobQueue, EnqueueOptions } from '../queue/types';\n\n/**\n * Scheduled job configuration.\n */\nexport interface ScheduledJobConfig {\n /** Unique name for the schedule */\n name: string;\n /** Cron expression (e.g., '0 0 * * *' for daily at midnight) */\n cronExpression: string;\n /** Job type to enqueue */\n jobType: string;\n /** Job payload (can be a function for dynamic payloads) */\n payload?: unknown | (() => unknown | Promise<unknown>);\n /** Enqueue options */\n options?: EnqueueOptions;\n /** Timezone for cron evaluation (default: UTC) */\n timezone?: string;\n /** Whether the schedule is enabled */\n enabled?: boolean;\n /** Description */\n description?: string;\n}\n\n/**\n * Active scheduled job with next run time.\n */\nexport interface ActiveSchedule extends ScheduledJobConfig {\n nextRun: Date | null;\n lastRun: Date | null;\n}\n\n/**\n * Parse a cron expression to get the next run time.\n * Simple implementation supporting: minute hour day month weekday\n */\nfunction getNextCronRun(\n cronExpression: string,\n after: Date = new Date()\n): Date | null {\n try {\n // Dynamically import cron-parser\n // This is a simplified fallback if cron-parser isn't available\n const parts = cronExpression.trim().split(/\\s+/);\n if (parts.length !== 5) {\n console.warn(`Invalid cron expression: ${cronExpression}`);\n return null;\n }\n\n // Simple parsing for common patterns\n const minute = parts[0];\n const hour = parts[1];\n const dayOfMonth = parts[2];\n const month = parts[3];\n const next = new Date(after);\n next.setSeconds(0);\n next.setMilliseconds(0);\n\n // Handle simple cases\n if (\n minute &&\n hour &&\n minute !== '*' &&\n hour !== '*' &&\n dayOfMonth === '*' &&\n month === '*'\n ) {\n // Daily at specific time\n const targetMinute = Number.parseInt(minute, 10);\n const targetHour = Number.parseInt(hour, 10);\n\n next.setHours(targetHour, targetMinute, 0, 0);\n if (next <= after) {\n next.setDate(next.getDate() + 1);\n }\n return next;\n }\n\n // For other patterns, add 1 minute as fallback\n next.setMinutes(next.getMinutes() + 1);\n return next;\n } catch {\n return null;\n }\n}\n\n/**\n * Job scheduler for recurring jobs.\n */\nexport class JobScheduler {\n private readonly schedules = new Map<string, ActiveSchedule>();\n private timer?: ReturnType<typeof setInterval>;\n private readonly checkIntervalMs: number;\n\n constructor(\n private readonly queue: JobQueue,\n options: { checkIntervalMs?: number } = {}\n ) {\n this.checkIntervalMs = options.checkIntervalMs ?? 60000; // 1 minute default\n }\n\n /**\n * Add a scheduled job.\n */\n schedule(config: ScheduledJobConfig): void {\n const nextRun =\n config.enabled !== false ? getNextCronRun(config.cronExpression) : null;\n\n this.schedules.set(config.name, {\n ...config,\n enabled: config.enabled ?? true,\n nextRun,\n lastRun: null,\n });\n }\n\n /**\n * Remove a scheduled job.\n */\n unschedule(name: string): boolean {\n return this.schedules.delete(name);\n }\n\n /**\n * Enable a scheduled job.\n */\n enable(name: string): boolean {\n const schedule = this.schedules.get(name);\n if (!schedule) return false;\n\n schedule.enabled = true;\n schedule.nextRun = getNextCronRun(schedule.cronExpression);\n return true;\n }\n\n /**\n * Disable a scheduled job.\n */\n disable(name: string): boolean {\n const schedule = this.schedules.get(name);\n if (!schedule) return false;\n\n schedule.enabled = false;\n schedule.nextRun = null;\n return true;\n }\n\n /**\n * Get all schedules.\n */\n getSchedules(): ActiveSchedule[] {\n return Array.from(this.schedules.values());\n }\n\n /**\n * Get a specific schedule.\n */\n getSchedule(name: string): ActiveSchedule | undefined {\n return this.schedules.get(name);\n }\n\n /**\n * Start the scheduler.\n */\n start(): void {\n if (this.timer) return;\n\n // Initial check\n void this.checkSchedules();\n\n // Periodic check\n this.timer = setInterval(() => {\n void this.checkSchedules();\n }, this.checkIntervalMs);\n }\n\n /**\n * Stop the scheduler.\n */\n stop(): void {\n if (this.timer) {\n clearInterval(this.timer);\n this.timer = undefined;\n }\n }\n\n /**\n * Check and enqueue due schedules.\n */\n private async checkSchedules(): Promise<void> {\n const now = new Date();\n\n for (const schedule of this.schedules.values()) {\n if (!schedule.enabled || !schedule.nextRun) continue;\n\n if (schedule.nextRun <= now) {\n try {\n // Resolve payload if it's a function\n const payload =\n typeof schedule.payload === 'function'\n ? await schedule.payload()\n : schedule.payload;\n\n // Enqueue the job\n await this.queue.enqueue(schedule.jobType, payload, schedule.options);\n\n // Update schedule\n schedule.lastRun = now;\n schedule.nextRun = getNextCronRun(schedule.cronExpression, now);\n } catch (error) {\n console.error(\n `Failed to enqueue scheduled job ${schedule.name}:`,\n error\n );\n }\n }\n }\n }\n}\n\n/**\n * Create a job scheduler instance.\n */\nexport function createScheduler(\n queue: JobQueue,\n options?: { checkIntervalMs?: number }\n): JobScheduler {\n return new JobScheduler(queue, options);\n}\n\n/**\n * Helper to define a scheduled job configuration.\n */\nexport function defineSchedule(config: ScheduledJobConfig): ScheduledJobConfig {\n return config;\n}\n"],"mappings":";;;;;AAoCA,SAAS,eACP,gBACA,wBAAc,IAAI,MAAM,EACX;AACb,KAAI;EAGF,MAAM,QAAQ,eAAe,MAAM,CAAC,MAAM,MAAM;AAChD,MAAI,MAAM,WAAW,GAAG;AACtB,WAAQ,KAAK,4BAA4B,iBAAiB;AAC1D,UAAO;;EAIT,MAAM,SAAS,MAAM;EACrB,MAAM,OAAO,MAAM;EACnB,MAAM,aAAa,MAAM;EACzB,MAAM,QAAQ,MAAM;EACpB,MAAM,OAAO,IAAI,KAAK,MAAM;AAC5B,OAAK,WAAW,EAAE;AAClB,OAAK,gBAAgB,EAAE;AAGvB,MACE,UACA,QACA,WAAW,OACX,SAAS,OACT,eAAe,OACf,UAAU,KACV;GAEA,MAAM,eAAe,OAAO,SAAS,QAAQ,GAAG;GAChD,MAAM,aAAa,OAAO,SAAS,MAAM,GAAG;AAE5C,QAAK,SAAS,YAAY,cAAc,GAAG,EAAE;AAC7C,OAAI,QAAQ,MACV,MAAK,QAAQ,KAAK,SAAS,GAAG,EAAE;AAElC,UAAO;;AAIT,OAAK,WAAW,KAAK,YAAY,GAAG,EAAE;AACtC,SAAO;SACD;AACN,SAAO;;;;;;AAOX,IAAa,eAAb,MAA0B;CACxB,AAAiB,4BAAY,IAAI,KAA6B;CAC9D,AAAQ;CACR,AAAiB;CAEjB,YACE,AAAiB,OACjB,UAAwC,EAAE,EAC1C;EAFiB;AAGjB,OAAK,kBAAkB,QAAQ,mBAAmB;;;;;CAMpD,SAAS,QAAkC;EACzC,MAAM,UACJ,OAAO,YAAY,QAAQ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U,eAAe,SAAS,gBAAgB,IAAI;YACxD,OAAO;AACd,YAAQ,MACN,mCAAmC,SAAS,KAAK,IACjD,MACD;;;;;;;;AAUX,SAAgB,gBACd,OACA,SACc;AACd,QAAO,IAAI,aAAa,OAAO,QAAQ;;;;;AAMzC,SAAgB,eAAe,QAAgD;AAC7E,QAAO"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contractspec/lib.jobs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.47.0",
|
|
4
4
|
"description": "Background jobs and scheduler module for ContractSpec applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contractspec",
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
"background",
|
|
11
11
|
"typescript"
|
|
12
12
|
],
|
|
13
|
-
"main": "./dist/index.js",
|
|
14
13
|
"types": "./dist/index.d.ts",
|
|
15
14
|
"type": "module",
|
|
16
15
|
"scripts": {
|
|
@@ -26,17 +25,17 @@
|
|
|
26
25
|
"lint:check": "eslint src"
|
|
27
26
|
},
|
|
28
27
|
"dependencies": {
|
|
29
|
-
"@contractspec/lib.schema": "1.
|
|
30
|
-
"@contractspec/lib.contracts": "1.
|
|
31
|
-
"@contractspec/lib.logger": "1.
|
|
32
|
-
"@contractspec/lib.knowledge": "1.
|
|
33
|
-
"@aws-sdk/client-sqs": "^3.
|
|
34
|
-
"zod": "^4.
|
|
28
|
+
"@contractspec/lib.schema": "1.47.0",
|
|
29
|
+
"@contractspec/lib.contracts": "1.47.0",
|
|
30
|
+
"@contractspec/lib.logger": "1.47.0",
|
|
31
|
+
"@contractspec/lib.knowledge": "1.47.0",
|
|
32
|
+
"@aws-sdk/client-sqs": "^3.966.0",
|
|
33
|
+
"zod": "^4.3.5",
|
|
35
34
|
"cron-parser": "^5.4.0"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
|
-
"@contractspec/tool.typescript": "1.
|
|
39
|
-
"@contractspec/tool.tsdown": "1.
|
|
37
|
+
"@contractspec/tool.typescript": "1.47.0",
|
|
38
|
+
"@contractspec/tool.tsdown": "1.47.0",
|
|
40
39
|
"typescript": "^5.9.3"
|
|
41
40
|
},
|
|
42
41
|
"exports": {
|
|
@@ -48,6 +47,7 @@
|
|
|
48
47
|
"./handlers/gmail-sync-handler": "./dist/handlers/gmail-sync-handler.js",
|
|
49
48
|
"./handlers/ping-job": "./dist/handlers/ping-job.js",
|
|
50
49
|
"./handlers/storage-document-handler": "./dist/handlers/storage-document-handler.js",
|
|
50
|
+
"./jobs.capability": "./dist/jobs.capability.js",
|
|
51
51
|
"./jobs.feature": "./dist/jobs.feature.js",
|
|
52
52
|
"./queue": "./dist/queue/index.js",
|
|
53
53
|
"./queue/gcp-cloud-tasks": "./dist/queue/gcp-cloud-tasks.js",
|
|
@@ -59,7 +59,6 @@
|
|
|
59
59
|
"./scheduler": "./dist/scheduler/index.js",
|
|
60
60
|
"./*": "./*"
|
|
61
61
|
},
|
|
62
|
-
"module": "./dist/index.js",
|
|
63
62
|
"files": [
|
|
64
63
|
"dist",
|
|
65
64
|
"README.md"
|