@pikku/queue-pg-boss 0.12.10 → 0.12.12

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.
@@ -1,85 +0,0 @@
1
- import { PgBoss, type ConstructorOptions } from 'pg-boss'
2
- import { PgBossQueueService } from './pg-boss-queue-service.js'
3
- import { PgBossQueueWorkers } from './pg-boss-queue-worker.js'
4
- import { PgBossSchedulerService } from './pg-boss-scheduler-service.js'
5
-
6
- /**
7
- * Factory class for pg-boss services
8
- * Manages a single PgBoss instance shared across queue, worker, and scheduler services
9
- */
10
- export class PgBossServiceFactory {
11
- private pgBoss: PgBoss
12
- private queueService?: PgBossQueueService
13
- private queueWorkers?: PgBossQueueWorkers
14
- private schedulerService?: PgBossSchedulerService
15
- private initialized = false
16
-
17
- constructor(optionsOrInstance: ConstructorOptions | string | PgBoss) {
18
- if (optionsOrInstance instanceof PgBoss) {
19
- this.pgBoss = optionsOrInstance
20
- } else {
21
- const options =
22
- typeof optionsOrInstance === 'string'
23
- ? { connectionString: optionsOrInstance }
24
- : optionsOrInstance
25
- this.pgBoss = new PgBoss(options)
26
- }
27
- }
28
-
29
- /**
30
- * Initialize the pg-boss instance
31
- */
32
- async init(): Promise<void> {
33
- if (!this.initialized) {
34
- await this.pgBoss.start()
35
- this.initialized = true
36
- }
37
- }
38
-
39
- /**
40
- * Get the queue service for publishing jobs
41
- */
42
- getQueueService(): PgBossQueueService {
43
- if (!this.queueService) {
44
- this.queueService = new PgBossQueueService(this.pgBoss)
45
- }
46
- return this.queueService
47
- }
48
-
49
- /**
50
- * Get the queue workers for processing jobs
51
- */
52
- getQueueWorkers(): PgBossQueueWorkers {
53
- if (!this.queueWorkers) {
54
- this.queueWorkers = new PgBossQueueWorkers(this.pgBoss)
55
- }
56
- return this.queueWorkers
57
- }
58
-
59
- /**
60
- * Get the scheduler service for managing recurring tasks
61
- */
62
- getSchedulerService(): PgBossSchedulerService {
63
- if (!this.schedulerService) {
64
- this.schedulerService = new PgBossSchedulerService(this.pgBoss)
65
- }
66
- return this.schedulerService
67
- }
68
-
69
- /**
70
- * Close all services and the pg-boss connection
71
- */
72
- async close(): Promise<void> {
73
- if (this.initialized) {
74
- await this.pgBoss.stop()
75
- this.initialized = false
76
- }
77
- }
78
-
79
- /**
80
- * Alias for close() - used by stopSingletonServices
81
- */
82
- async stop(): Promise<void> {
83
- await this.close()
84
- }
85
- }
package/src/utils.ts DELETED
@@ -1,87 +0,0 @@
1
- import type { QueueJob, QueueJobStatus } from '@pikku/core/queue'
2
- import type { PgBoss, JobWithMetadata } from 'pg-boss'
3
-
4
- /**
5
- * Map pg-boss job state to our queue job status
6
- */
7
- const mapPgBossStateToStatus = (state: string): QueueJobStatus => {
8
- switch (state) {
9
- case 'created':
10
- case 'retry':
11
- return 'waiting'
12
- case 'active':
13
- return 'active'
14
- case 'completed':
15
- return 'completed'
16
- case 'failed':
17
- return 'failed'
18
- case 'cancelled':
19
- return 'failed'
20
- default:
21
- return 'waiting'
22
- }
23
- }
24
-
25
- /**
26
- * Map pg-boss job to QueueJob interface
27
- */
28
- export const mapPgBossJobToQueueJob = <In, Out>(
29
- pgBossJob: JobWithMetadata<In>,
30
- pgBossInstance: PgBoss
31
- ): QueueJob<In, Out> => {
32
- return {
33
- queueName: pgBossJob.name,
34
- id: pgBossJob.id || '',
35
- data: pgBossJob.data,
36
- status: async () => {
37
- const jobWithState = await pgBossInstance.getJobById(
38
- pgBossJob.name,
39
- pgBossJob.id
40
- )
41
- return mapPgBossStateToStatus(jobWithState?.state || 'created')
42
- },
43
- waitForCompletion: async (ttl?: number): Promise<Out> => {
44
- // pg-boss doesn't have a built-in waitForCompletion, so we'll poll
45
- const startTime = Date.now()
46
- const timeout = ttl || 30000 // 30 seconds default
47
-
48
- while (Date.now() - startTime < timeout) {
49
- const job = await pgBossInstance.getJobById(
50
- pgBossJob.name,
51
- pgBossJob.id
52
- )
53
- if (job?.state === 'completed') {
54
- return job.output as Out
55
- }
56
- if (job?.state === 'failed' || job?.state === 'cancelled') {
57
- throw new Error(`Job failed: ${JSON.stringify(job.output)}`)
58
- }
59
- // Wait 100ms before polling again
60
- await new Promise((resolve) => setTimeout(resolve, 100))
61
- }
62
-
63
- throw new Error('Job did not complete within timeout')
64
- },
65
- metadata: () => {
66
- return {
67
- progress: 0, // pg-boss doesn't have built-in progress tracking
68
- attemptsMade: pgBossJob.retryCount || 0,
69
- maxAttempts: pgBossJob.retryLimit || 1,
70
- createdAt: pgBossJob.createdOn
71
- ? new Date(pgBossJob.createdOn)
72
- : new Date(),
73
- processedAt: pgBossJob.startedOn
74
- ? new Date(pgBossJob.startedOn)
75
- : undefined,
76
- completedAt: pgBossJob.completedOn
77
- ? new Date(pgBossJob.completedOn)
78
- : undefined,
79
- failedAt:
80
- pgBossJob.state === 'failed' && pgBossJob.completedOn
81
- ? new Date(pgBossJob.completedOn)
82
- : undefined,
83
- error: (pgBossJob.data as any).error,
84
- }
85
- },
86
- }
87
- }
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "module": "Node18",
6
- "outDir": "dist",
7
- "target": "esnext",
8
- "declaration": true
9
- },
10
- "include": ["src/**/*.ts"],
11
- "exclude": ["**/*.test.ts", "node_modules"],
12
- "references": [
13
- {
14
- "path": "../../core/tsconfig.json"
15
- }
16
- ]
17
- }
@@ -1 +0,0 @@
1
- {"root":["./src/index.ts","./src/pg-boss-queue-service.ts","./src/pg-boss-queue-worker.ts","./src/pg-boss-scheduler-service.ts","./src/pg-boss-service-factory.ts","./src/utils.ts"],"version":"6.0.3"}