@wisemen/pgboss-nestjs-job 3.0.0 → 3.0.1
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/README.md +26 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,6 +17,9 @@ Make sure that the env variable `DATABASE_URI` is defined.
|
|
|
17
17
|
fetchRefreshThreshold, // Refresh threshold to fetch jobs
|
|
18
18
|
pollInterval, // The interval (in milliseconds) to poll for new jobs
|
|
19
19
|
bouncerModule // An optional bouncer which will prevent jobs from being fetched
|
|
20
|
+
databaseOptions: {
|
|
21
|
+
// ...
|
|
22
|
+
}
|
|
20
23
|
})
|
|
21
24
|
]
|
|
22
25
|
})
|
|
@@ -35,7 +38,7 @@ const _worker = new Worker()
|
|
|
35
38
|
|
|
36
39
|
```ts
|
|
37
40
|
export interface MyJobData extends BaseJobData {
|
|
38
|
-
uuid: string
|
|
41
|
+
uuid: string;
|
|
39
42
|
// other data here
|
|
40
43
|
}
|
|
41
44
|
```
|
|
@@ -43,7 +46,7 @@ export interface MyJobData extends BaseJobData {
|
|
|
43
46
|
3. Create a job definition
|
|
44
47
|
|
|
45
48
|
```ts
|
|
46
|
-
@PgBossJob(
|
|
49
|
+
@PgBossJob("queue-name")
|
|
47
50
|
export class MyJob extends BaseJob<MyJobData> {}
|
|
48
51
|
```
|
|
49
52
|
|
|
@@ -53,14 +56,14 @@ export class MyJob extends BaseJob<MyJobData> {}
|
|
|
53
56
|
@Injectable()
|
|
54
57
|
@PgBossJobHandler(MyJob)
|
|
55
58
|
export class MyJobHandler extends JobHandler<MyJob> {
|
|
56
|
-
public async run
|
|
59
|
+
public async run(data: MyJobData): Promise<void> {
|
|
57
60
|
// Do stuff
|
|
58
61
|
}
|
|
59
62
|
}
|
|
60
63
|
```
|
|
61
64
|
|
|
62
|
-
|
|
63
65
|
## QueueBouncer
|
|
66
|
+
|
|
64
67
|
Some workers / queues only need to run when some external service is online. The `QueueBouncer` base class is used by workers to determine wether they should poll for jobs or not by calling the `canProceed` method on the bouncer. This method typically performs the health check on an external service.
|
|
65
68
|
|
|
66
69
|
The queuebouncer is provided to the worker by creating and exportin a provider for the `QueueBouncer` class. An example module can be:
|
|
@@ -81,43 +84,41 @@ export class CuoptWorkerBouncerModule {}
|
|
|
81
84
|
When no bouncer is set, the package will default to `AllowBouncer` which never blocks a worker / queue from polling for jobs.
|
|
82
85
|
|
|
83
86
|
An example of a bouncer for an external cuopt system.
|
|
87
|
+
|
|
84
88
|
```typescript
|
|
85
89
|
@Injectable()
|
|
86
90
|
export class CuoptWorkerBouncer extends QueueBouncer {
|
|
87
|
-
private isCuoptRunning: boolean
|
|
88
|
-
private lastPolledAt: Date
|
|
89
|
-
private pollPromise: Promise<boolean> | undefined
|
|
90
|
-
|
|
91
|
-
constructor
|
|
92
|
-
|
|
93
|
-
) {
|
|
94
|
-
super()
|
|
91
|
+
private isCuoptRunning: boolean;
|
|
92
|
+
private lastPolledAt: Date;
|
|
93
|
+
private pollPromise: Promise<boolean> | undefined;
|
|
94
|
+
|
|
95
|
+
constructor(private cuopt: CuoptClient) {
|
|
96
|
+
super();
|
|
95
97
|
}
|
|
96
98
|
|
|
97
|
-
async canProceed
|
|
98
|
-
if (dayjs().diff(this.lastPolledAt,
|
|
99
|
-
await this.pollCuopt()
|
|
99
|
+
async canProceed(): Promise<boolean> {
|
|
100
|
+
if (dayjs().diff(this.lastPolledAt, "seconds") > 2) {
|
|
101
|
+
await this.pollCuopt();
|
|
100
102
|
}
|
|
101
103
|
|
|
102
|
-
return this.isCuoptRunning
|
|
104
|
+
return this.isCuoptRunning;
|
|
103
105
|
}
|
|
104
106
|
|
|
105
|
-
private async pollCuopt
|
|
107
|
+
private async pollCuopt() {
|
|
106
108
|
if (this.pollPromise !== undefined) {
|
|
107
|
-
await this.pollPromise
|
|
108
|
-
return
|
|
109
|
+
await this.pollPromise;
|
|
110
|
+
return;
|
|
109
111
|
}
|
|
110
112
|
|
|
111
|
-
this.pollPromise = this.cuopt.isReady()
|
|
113
|
+
this.pollPromise = this.cuopt.isReady();
|
|
112
114
|
|
|
113
115
|
try {
|
|
114
|
-
this.isCuoptRunning = await this.pollPromise
|
|
116
|
+
this.isCuoptRunning = await this.pollPromise;
|
|
115
117
|
} catch {
|
|
116
|
-
this.isCuoptRunning = false
|
|
118
|
+
this.isCuoptRunning = false;
|
|
117
119
|
} finally {
|
|
118
|
-
this.lastPolledAt = new Date()
|
|
120
|
+
this.lastPolledAt = new Date();
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
}
|
|
122
|
-
|
|
123
|
-
```
|
|
124
|
+
```
|