@epztickets/common 1.25.0 → 1.27.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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JetStreamClient, JsMsg } from "nats";
|
|
1
|
+
import { JetStreamClient, JetStreamManager, JsMsg } from "nats";
|
|
2
2
|
import { Subjects } from "./subjects";
|
|
3
3
|
interface Event {
|
|
4
4
|
subject: Subjects;
|
|
@@ -9,8 +9,10 @@ export declare abstract class JetStreamListener<T extends Event> {
|
|
|
9
9
|
abstract durableName: string;
|
|
10
10
|
abstract queueGroup: string;
|
|
11
11
|
protected js: JetStreamClient;
|
|
12
|
-
|
|
12
|
+
protected jsm: JetStreamManager;
|
|
13
|
+
constructor(js: JetStreamClient, jsm: JetStreamManager);
|
|
13
14
|
listen(): Promise<void>;
|
|
14
|
-
|
|
15
|
+
private ensureConsumer;
|
|
16
|
+
abstract onMessage(data: T["data"], msg: JsMsg): Promise<void>;
|
|
15
17
|
}
|
|
16
18
|
export {};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// import {
|
|
3
|
+
// JetStreamClient,
|
|
4
|
+
// consumerOpts,
|
|
5
|
+
// JsMsg,
|
|
6
|
+
// } from "nats";
|
|
7
|
+
// import { Subjects } from "./subjects";
|
|
2
8
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
9
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
10
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -17,32 +23,67 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
|
17
23
|
};
|
|
18
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
25
|
exports.JetStreamListener = void 0;
|
|
26
|
+
// interface Event {
|
|
27
|
+
// subject: Subjects;
|
|
28
|
+
// data: any;
|
|
29
|
+
// }
|
|
30
|
+
// export abstract class JetStreamListener<T extends Event> {
|
|
31
|
+
// abstract subject: T["subject"];
|
|
32
|
+
// abstract durableName: string;
|
|
33
|
+
// abstract queueGroup: string;
|
|
34
|
+
// protected js: JetStreamClient;
|
|
35
|
+
// constructor(js: JetStreamClient) {
|
|
36
|
+
// this.js = js;
|
|
37
|
+
// }
|
|
38
|
+
// async listen() {
|
|
39
|
+
// const opts = consumerOpts();
|
|
40
|
+
// opts.durable(this.durableName);
|
|
41
|
+
// opts.manualAck();
|
|
42
|
+
// opts.ackExplicit();
|
|
43
|
+
// opts.deliverAll();
|
|
44
|
+
// opts.maxDeliver(5);
|
|
45
|
+
// opts.filterSubject(this.subject);
|
|
46
|
+
// opts.deliverTo(`${this.queueGroup}.${this.durableName}`);
|
|
47
|
+
// const sub = await this.js.subscribe(this.subject, opts);
|
|
48
|
+
// console.log(`Listening for ${this.subject} with durable ${this.durableName}`);
|
|
49
|
+
// for await (const msg of sub) {
|
|
50
|
+
// try {
|
|
51
|
+
// const data = JSON.parse(msg.data.toString());
|
|
52
|
+
// await this.onMessage(data, msg);
|
|
53
|
+
// console.log(`Listening for ${this.subject} with durable ${this.durableName}`);
|
|
54
|
+
// } catch (err) {
|
|
55
|
+
// console.error("Listener error", err);
|
|
56
|
+
// }
|
|
57
|
+
// }
|
|
58
|
+
// }
|
|
59
|
+
// abstract onMessage(data: T["data"], msg: JsMsg): void;
|
|
60
|
+
// }
|
|
20
61
|
const nats_1 = require("nats");
|
|
21
62
|
class JetStreamListener {
|
|
22
|
-
constructor(js) {
|
|
63
|
+
constructor(js, jsm) {
|
|
23
64
|
this.js = js;
|
|
65
|
+
this.jsm = jsm;
|
|
24
66
|
}
|
|
25
67
|
listen() {
|
|
26
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
27
69
|
var _a, e_1, _b, _c;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
opts.ackExplicit();
|
|
32
|
-
opts.deliverAll();
|
|
33
|
-
opts.maxDeliver(5);
|
|
34
|
-
opts.filterSubject(this.subject);
|
|
35
|
-
opts.deliverTo(`${this.queueGroup}.${this.durableName}`);
|
|
36
|
-
const sub = yield this.js.subscribe(this.subject, opts);
|
|
70
|
+
yield this.ensureConsumer();
|
|
71
|
+
// Get or bind to the durable consumer
|
|
72
|
+
const consumer = yield this.js.consumers.get("events", this.durableName);
|
|
37
73
|
console.log(`Listening for ${this.subject} with durable ${this.durableName}`);
|
|
74
|
+
// Long-running pull-based consumption
|
|
75
|
+
const messages = yield consumer.consume({
|
|
76
|
+
max_messages: 1, // backpressure control
|
|
77
|
+
});
|
|
38
78
|
try {
|
|
39
|
-
for (var _d = true,
|
|
40
|
-
_c =
|
|
79
|
+
for (var _d = true, messages_1 = __asyncValues(messages), messages_1_1; messages_1_1 = yield messages_1.next(), _a = messages_1_1.done, !_a; _d = true) {
|
|
80
|
+
_c = messages_1_1.value;
|
|
41
81
|
_d = false;
|
|
42
82
|
const msg = _c;
|
|
43
83
|
try {
|
|
44
84
|
const data = JSON.parse(msg.data.toString());
|
|
45
85
|
yield this.onMessage(data, msg);
|
|
86
|
+
msg.ack();
|
|
46
87
|
}
|
|
47
88
|
catch (err) {
|
|
48
89
|
console.error("Listener error", err);
|
|
@@ -52,11 +93,30 @@ class JetStreamListener {
|
|
|
52
93
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
53
94
|
finally {
|
|
54
95
|
try {
|
|
55
|
-
if (!_d && !_a && (_b =
|
|
96
|
+
if (!_d && !_a && (_b = messages_1.return)) yield _b.call(messages_1);
|
|
56
97
|
}
|
|
57
98
|
finally { if (e_1) throw e_1.error; }
|
|
58
99
|
}
|
|
59
100
|
});
|
|
60
101
|
}
|
|
102
|
+
ensureConsumer() {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
try {
|
|
105
|
+
yield this.jsm.consumers.info("events", this.durableName);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
if (err.code !== "404")
|
|
109
|
+
throw err;
|
|
110
|
+
console.log(`Creating consumer ${this.durableName} for ${this.subject}`);
|
|
111
|
+
yield this.jsm.consumers.add("events", {
|
|
112
|
+
durable_name: this.durableName,
|
|
113
|
+
filter_subject: this.subject,
|
|
114
|
+
ack_policy: nats_1.AckPolicy.Explicit,
|
|
115
|
+
deliver_policy: nats_1.DeliverPolicy.All,
|
|
116
|
+
max_deliver: 5,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
61
121
|
}
|
|
62
122
|
exports.JetStreamListener = JetStreamListener;
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// import { JetStreamManager, RetentionPolicy, StorageType, DiscardPolicy } from "nats";
|
|
3
|
-
// import { Streams } from "../events/subjects";
|
|
4
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
5
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
6
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -12,22 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
12
10
|
};
|
|
13
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
12
|
exports.ensureStreams = ensureStreams;
|
|
15
|
-
// export async function ensureEventStream(jsm: JetStreamManager) {
|
|
16
|
-
// const streams = await jsm.streams.list().next();
|
|
17
|
-
// const exists = streams.some(s => s.config.name === Streams.Events);
|
|
18
|
-
// if (exists) {
|
|
19
|
-
// console.log("Stream already exists:", Streams.Events);
|
|
20
|
-
// return};
|
|
21
|
-
// await jsm.streams.add({
|
|
22
|
-
// name: Streams.Events,
|
|
23
|
-
// subjects: ["events.>"],
|
|
24
|
-
// // retention: RetentionPolicy.Workqueue, // consumer needs to be unique
|
|
25
|
-
// retention: RetentionPolicy.Limits, // fan-out to multiple services
|
|
26
|
-
// storage: StorageType.File,
|
|
27
|
-
// discard: DiscardPolicy.Old,
|
|
28
|
-
// max_age: 7 * 24 * 60 * 60 * 1000, // 7 days
|
|
29
|
-
// });
|
|
30
|
-
// }
|
|
31
13
|
const nats_1 = require("nats");
|
|
32
14
|
const subjects_1 = require("../events/subjects");
|
|
33
15
|
function ensureStreams(jsm) {
|
|
@@ -59,35 +41,3 @@ function ensureStreams(jsm) {
|
|
|
59
41
|
}
|
|
60
42
|
});
|
|
61
43
|
}
|
|
62
|
-
// import {
|
|
63
|
-
// JetStreamManager,
|
|
64
|
-
// RetentionPolicy,
|
|
65
|
-
// StorageType,
|
|
66
|
-
// DiscardPolicy,
|
|
67
|
-
// } from "nats";
|
|
68
|
-
// import { Streams } from "../events/subjects";
|
|
69
|
-
// export async function ensureStreams(jsm: JetStreamManager) {
|
|
70
|
-
// const streams = await jsm.streams.list().next();
|
|
71
|
-
// const hasEvents = streams.some(s => s.config.name === Streams.Events);
|
|
72
|
-
// // 1️⃣ Stream already exists → validate config
|
|
73
|
-
// if (hasEvents) {
|
|
74
|
-
// const info = await jsm.streams.info(Streams.Events);
|
|
75
|
-
// if (info.config.retention !== RetentionPolicy.Limits) {
|
|
76
|
-
// throw new Error(
|
|
77
|
-
// `EVENTS stream has invalid retention: ${info.config.retention}`
|
|
78
|
-
// );
|
|
79
|
-
// }
|
|
80
|
-
// console.log("✅ EVENTS stream already exists with correct retention");
|
|
81
|
-
// return;
|
|
82
|
-
// }
|
|
83
|
-
// // 2️⃣ Stream does not exist → create it
|
|
84
|
-
// await jsm.streams.add({
|
|
85
|
-
// name: Streams.Events,
|
|
86
|
-
// subjects: ["events.>"],
|
|
87
|
-
// retention: RetentionPolicy.Limits, // FAN-OUT
|
|
88
|
-
// storage: StorageType.File,
|
|
89
|
-
// discard: DiscardPolicy.Old,
|
|
90
|
-
// max_age: 7 * 24 * 60 * 60 * 1_000_000_000 // ✅ nanoseconds
|
|
91
|
-
// });
|
|
92
|
-
// console.log("✅ Created EVENTS stream");
|
|
93
|
-
// }
|