@dudousxd/nestjs-diagnostics-redis 0.2.4 → 0.3.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/CHANGELOG.md +28 -0
- package/dist/relay.d.ts +2 -50
- package/dist/relay.d.ts.map +1 -1
- package/dist/relay.js +1 -162
- package/dist/relay.js.map +1 -1
- package/package.json +5 -5
- package/skills/redis-transport/SKILL.md +30 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @dudousxd/nestjs-diagnostics-redis
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#81](https://github.com/DavideCarvalho/nestjs-diagnostics/pull/81) [`ebdacd0`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/ebdacd09676892eca5bf2d359b6edd8079600015) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - Listeners with delivery transports and `role`.
|
|
8
|
+
|
|
9
|
+
- Every envelope from `emit` and `trace` carries a unique `id` (a UUID). Relays keep it, so the same event has the same `id` in every process.
|
|
10
|
+
- `@OnDiagnostic(lib, event?)` accepts a `RegExp` for `event`, and checks the handler's parameter against the `ChannelRegistry` payload for registered channels. Each listener has a stable id, `<ClassName>#<method>`; two listener classes with the same name fail the bootstrap.
|
|
11
|
+
- `DiagnosticsModule.forRoot({ default, transports, role, nodeId })` and `forRootAsync(...)` select how listeners run across processes, with the `transports` factory:
|
|
12
|
+
- `transports.redis({ pub, sub })`: broadcast over Redis pub/sub, best-effort.
|
|
13
|
+
- `transports.queue({ connection })`: one BullMQ job per `(listener, event)`, retried when the handler throws (optional peer `bullmq`).
|
|
14
|
+
- `transports.durable(options)`: one durable run per `(listener, event)` through `@dudousxd/nestjs-durable-diagnostics/delivery`, imported only when selected; bootstrap fails with a clear message when it is missing or durable is not configured.
|
|
15
|
+
With `queue` and `durable`, delivery happens once, from the emitting process.
|
|
16
|
+
- `role: 'all' | 'api' | 'worker'`: `'api'` emits and hands work to the transport but never consumes or runs listeners. `'api'`/`'worker'` without a `default` transport fail the bootstrap.
|
|
17
|
+
- The Redis relay now lives in `@dudousxd/nestjs-diagnostics` (`createDiagnosticsRedisRelay`); `@dudousxd/nestjs-diagnostics-redis` re-exports it. It takes `receive: false` for a publish-only relay (then `sub` is optional), and marks re-emitted copies (`isRelayed`).
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies [[`ebdacd0`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/ebdacd09676892eca5bf2d359b6edd8079600015)]:
|
|
22
|
+
- @dudousxd/nestjs-diagnostics@0.9.0
|
|
23
|
+
|
|
24
|
+
## 0.2.5
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- Updated dependencies [[`f4c910b`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/f4c910b0651ee9419aa452268f23646533d10306)]:
|
|
29
|
+
- @dudousxd/nestjs-diagnostics@0.8.0
|
|
30
|
+
|
|
3
31
|
## 0.2.4
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/dist/relay.d.ts
CHANGED
|
@@ -1,51 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
publish(channel: string, message: string): unknown;
|
|
4
|
-
/** When present, a tick's forwarded events go out as one pipeline instead of one round trip each. */
|
|
5
|
-
pipeline?(): RedisPipelineLike;
|
|
6
|
-
subscribe(channel: string, callback?: (err: Error | null, count: number) => void): unknown;
|
|
7
|
-
on(event: 'message', listener: (channel: string, message: string) => void): unknown;
|
|
8
|
-
removeListener(event: 'message', listener: (channel: string, message: string) => void): unknown;
|
|
9
|
-
unsubscribe(channel: string): unknown;
|
|
10
|
-
}
|
|
11
|
-
/** The slice of an ioredis pipeline the relay uses to batch publishes. */
|
|
12
|
-
export interface RedisPipelineLike {
|
|
13
|
-
publish(channel: string, message: string): unknown;
|
|
14
|
-
exec(): unknown;
|
|
15
|
-
}
|
|
16
|
-
export interface ChannelRef {
|
|
17
|
-
lib: string;
|
|
18
|
-
event: string;
|
|
19
|
-
}
|
|
20
|
-
export interface DiagnosticsRedisRelayOptions {
|
|
21
|
-
/** Publisher connection. */
|
|
22
|
-
pub: RedisLike;
|
|
23
|
-
/** Subscriber connection (separate from `pub`). For ioredis: `const sub = pub.duplicate()`. */
|
|
24
|
-
sub: RedisLike;
|
|
25
|
-
/** Forward every event of these libs (current + future channels). */
|
|
26
|
-
libs?: string[];
|
|
27
|
-
/** Forward these exact channels, in addition to `libs`. */
|
|
28
|
-
channels?: ChannelRef[];
|
|
29
|
-
/** Forward EVERY aviary channel (current + future). Overrides `libs`/`channels`. Default false. */
|
|
30
|
-
all?: boolean;
|
|
31
|
-
/** Redis channel to relay on. Default 'aviary:diagnostics:relay'. */
|
|
32
|
-
redisChannel?: string;
|
|
33
|
-
/** Unique id for THIS process, for echo suppression. Default a random id. */
|
|
34
|
-
nodeId?: string;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Relay diagnostics events across processes over Redis pub/sub. Forwards selected local
|
|
38
|
-
* `aviary:<lib>:<event>` channels to Redis and re-emits Redis-received events onto the local bus, so
|
|
39
|
-
* `@OnDiagnostic` handlers / `getChannel(...).subscribe(...)` fire cross-process. Loop-safe via nodeId
|
|
40
|
-
* echo suppression and a re-emit guard. Never throws into `emit()` or the Redis handler. Does NOT
|
|
41
|
-
* close the `pub`/`sub` connections — the caller owns them.
|
|
42
|
-
*
|
|
43
|
-
* Forwarding happens off the emitter's call stack: selected events are queued and, once per event
|
|
44
|
-
* loop turn, serialized and published in emit order (one Redis message per event, sent as a single
|
|
45
|
-
* pipeline when `pub` supports it).
|
|
46
|
-
*
|
|
47
|
-
* @returns a teardown that publishes any queued events, then removes all local subscriptions and
|
|
48
|
-
* the Redis message handler.
|
|
49
|
-
*/
|
|
50
|
-
export declare function createDiagnosticsRedisRelay(options: DiagnosticsRedisRelayOptions): () => void;
|
|
1
|
+
export { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics';
|
|
2
|
+
export type { ChannelRef, DiagnosticsRedisRelayOptions, RedisLike, RedisPipelineLike, } from '@dudousxd/nestjs-diagnostics';
|
|
51
3
|
//# sourceMappingURL=relay.d.ts.map
|
package/dist/relay.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,YAAY,EACV,UAAU,EACV,4BAA4B,EAC5B,SAAS,EACT,iBAAiB,GAClB,MAAM,8BAA8B,CAAC"}
|
package/dist/relay.js
CHANGED
|
@@ -1,163 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
import { CHANNEL_PREFIX, channelName, getChannel, onChannelRegistered, registeredChannels, } from '@dudousxd/nestjs-diagnostics';
|
|
3
|
-
const DEFAULT_REDIS_CHANNEL = 'aviary:diagnostics:relay';
|
|
4
|
-
/** Strip the `aviary:` prefix and split on the FIRST colon — the event segment may contain dots
|
|
5
|
-
* (e.g. `durable:run.failed`), but the lib/event boundary is the first colon after the prefix. */
|
|
6
|
-
function parseChannelName(name) {
|
|
7
|
-
const prefix = `${CHANNEL_PREFIX}:`;
|
|
8
|
-
if (!name.startsWith(prefix))
|
|
9
|
-
return null;
|
|
10
|
-
const rest = name.slice(prefix.length);
|
|
11
|
-
const idx = rest.indexOf(':');
|
|
12
|
-
if (idx <= 0 || idx === rest.length - 1)
|
|
13
|
-
return null;
|
|
14
|
-
return { lib: rest.slice(0, idx), event: rest.slice(idx + 1) };
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Relay diagnostics events across processes over Redis pub/sub. Forwards selected local
|
|
18
|
-
* `aviary:<lib>:<event>` channels to Redis and re-emits Redis-received events onto the local bus, so
|
|
19
|
-
* `@OnDiagnostic` handlers / `getChannel(...).subscribe(...)` fire cross-process. Loop-safe via nodeId
|
|
20
|
-
* echo suppression and a re-emit guard. Never throws into `emit()` or the Redis handler. Does NOT
|
|
21
|
-
* close the `pub`/`sub` connections — the caller owns them.
|
|
22
|
-
*
|
|
23
|
-
* Forwarding happens off the emitter's call stack: selected events are queued and, once per event
|
|
24
|
-
* loop turn, serialized and published in emit order (one Redis message per event, sent as a single
|
|
25
|
-
* pipeline when `pub` supports it).
|
|
26
|
-
*
|
|
27
|
-
* @returns a teardown that publishes any queued events, then removes all local subscriptions and
|
|
28
|
-
* the Redis message handler.
|
|
29
|
-
*/
|
|
30
|
-
export function createDiagnosticsRedisRelay(options) {
|
|
31
|
-
const { pub, sub } = options;
|
|
32
|
-
const redisChannel = options.redisChannel ?? DEFAULT_REDIS_CHANNEL;
|
|
33
|
-
const nodeId = options.nodeId ?? randomUUID();
|
|
34
|
-
const forwardAll = options.all === true;
|
|
35
|
-
const libs = options.libs ?? [];
|
|
36
|
-
const exact = options.channels ?? [];
|
|
37
|
-
const subscriptions = [];
|
|
38
|
-
const subscribed = new Set();
|
|
39
|
-
const reEmitting = new WeakSet();
|
|
40
|
-
const pending = [];
|
|
41
|
-
let scheduled = null;
|
|
42
|
-
const publishOne = (message) => {
|
|
43
|
-
try {
|
|
44
|
-
pub.publish(redisChannel, message);
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
47
|
-
// a failing publish drops only its own event
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const flush = () => {
|
|
51
|
-
if (scheduled !== null) {
|
|
52
|
-
clearImmediate(scheduled);
|
|
53
|
-
scheduled = null;
|
|
54
|
-
}
|
|
55
|
-
if (pending.length === 0)
|
|
56
|
-
return;
|
|
57
|
-
const messages = [];
|
|
58
|
-
for (const env of pending.splice(0)) {
|
|
59
|
-
try {
|
|
60
|
-
messages.push(JSON.stringify({ node: nodeId, env }));
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
// an unserializable event is dropped without affecting the rest of the batch
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (messages.length > 1 && typeof pub.pipeline === 'function') {
|
|
67
|
-
try {
|
|
68
|
-
const pipeline = pub.pipeline();
|
|
69
|
-
for (const message of messages)
|
|
70
|
-
pipeline.publish(redisChannel, message);
|
|
71
|
-
pipeline.exec();
|
|
72
|
-
}
|
|
73
|
-
catch {
|
|
74
|
-
// never throw out of the flush
|
|
75
|
-
}
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
for (const message of messages)
|
|
79
|
-
publishOne(message);
|
|
80
|
-
};
|
|
81
|
-
const forward = (msg) => {
|
|
82
|
-
if (typeof msg !== 'object' || msg === null)
|
|
83
|
-
return;
|
|
84
|
-
if (reEmitting.has(msg))
|
|
85
|
-
return; // a re-emitted remote event — do not send it back
|
|
86
|
-
pending.push(msg);
|
|
87
|
-
scheduled ??= setImmediate(flush);
|
|
88
|
-
};
|
|
89
|
-
const subscribeRef = (ref) => {
|
|
90
|
-
const name = channelName(ref.lib, ref.event);
|
|
91
|
-
if (subscribed.has(name))
|
|
92
|
-
return;
|
|
93
|
-
getChannel(ref.lib, ref.event).subscribe(forward);
|
|
94
|
-
subscribed.add(name);
|
|
95
|
-
subscriptions.push({ ref, listener: forward });
|
|
96
|
-
};
|
|
97
|
-
const wildcardMatches = (name) => {
|
|
98
|
-
if (forwardAll)
|
|
99
|
-
return name.startsWith(`${CHANNEL_PREFIX}:`);
|
|
100
|
-
return libs.some((lib) => name.startsWith(`${CHANNEL_PREFIX}:${lib}:`));
|
|
101
|
-
};
|
|
102
|
-
for (const ref of exact)
|
|
103
|
-
subscribeRef(ref);
|
|
104
|
-
const hasWildcard = forwardAll || libs.length > 0;
|
|
105
|
-
if (hasWildcard) {
|
|
106
|
-
for (const name of registeredChannels()) {
|
|
107
|
-
if (wildcardMatches(name)) {
|
|
108
|
-
const ref = parseChannelName(name);
|
|
109
|
-
if (ref)
|
|
110
|
-
subscribeRef(ref);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const offRegistered = hasWildcard
|
|
115
|
-
? onChannelRegistered((name) => {
|
|
116
|
-
if (wildcardMatches(name)) {
|
|
117
|
-
const ref = parseChannelName(name);
|
|
118
|
-
if (ref)
|
|
119
|
-
subscribeRef(ref);
|
|
120
|
-
}
|
|
121
|
-
})
|
|
122
|
-
: null;
|
|
123
|
-
const onMessage = (channel, raw) => {
|
|
124
|
-
if (channel !== redisChannel)
|
|
125
|
-
return;
|
|
126
|
-
let parsed;
|
|
127
|
-
try {
|
|
128
|
-
parsed = JSON.parse(raw);
|
|
129
|
-
}
|
|
130
|
-
catch {
|
|
131
|
-
return; // ignore malformed
|
|
132
|
-
}
|
|
133
|
-
if (parsed.node === nodeId)
|
|
134
|
-
return; // our own echo
|
|
135
|
-
const env = parsed.env;
|
|
136
|
-
if (!env || typeof env.lib !== 'string' || typeof env.event !== 'string')
|
|
137
|
-
return;
|
|
138
|
-
reEmitting.add(env);
|
|
139
|
-
try {
|
|
140
|
-
getChannel(env.lib, env.event).publish(env);
|
|
141
|
-
}
|
|
142
|
-
catch {
|
|
143
|
-
// a local subscriber threw — never propagate into the message handler
|
|
144
|
-
}
|
|
145
|
-
finally {
|
|
146
|
-
reEmitting.delete(env);
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
sub.subscribe(redisChannel);
|
|
150
|
-
sub.on('message', onMessage);
|
|
151
|
-
return () => {
|
|
152
|
-
flush();
|
|
153
|
-
for (const { ref, listener } of subscriptions) {
|
|
154
|
-
getChannel(ref.lib, ref.event).unsubscribe(listener);
|
|
155
|
-
}
|
|
156
|
-
subscriptions.length = 0;
|
|
157
|
-
subscribed.clear();
|
|
158
|
-
offRegistered?.();
|
|
159
|
-
sub.removeListener('message', onMessage);
|
|
160
|
-
sub.unsubscribe(redisChannel);
|
|
161
|
-
};
|
|
162
|
-
}
|
|
1
|
+
export { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics';
|
|
163
2
|
//# sourceMappingURL=relay.js.map
|
package/dist/relay.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relay.js","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"relay.js","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dudousxd/nestjs-diagnostics-redis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Cross-process transport for @dudousxd/nestjs-diagnostics — relay aviary:<lib>:<event> events over Redis pub/sub so @OnDiagnostic handlers fire across processes.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
],
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@dudousxd/nestjs-diagnostics": "^0.
|
|
44
|
+
"@dudousxd/nestjs-diagnostics": "^0.9.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"ioredis": "^5",
|
|
@@ -61,9 +61,9 @@
|
|
|
61
61
|
}
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@nestjs/common": "12.
|
|
65
|
-
"@nestjs/core": "12.
|
|
66
|
-
"@nestjs/testing": "12.
|
|
64
|
+
"@nestjs/common": "12.3.3",
|
|
65
|
+
"@nestjs/core": "12.3.3",
|
|
66
|
+
"@nestjs/testing": "12.3.3",
|
|
67
67
|
"@types/node": "20.19.43",
|
|
68
68
|
"ioredis": "5.11.1",
|
|
69
69
|
"reflect-metadata": "0.2.2",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: redis-transport
|
|
3
|
-
description: Relay @dudousxd/nestjs-diagnostics events across processes/pods over Redis pub/sub
|
|
3
|
+
description: "Relay @dudousxd/nestjs-diagnostics events across processes/pods over Redis pub/sub. The relay lives in `@dudousxd/nestjs-diagnostics` (`createDiagnosticsRedisRelay`, re-exported by `@dudousxd/nestjs-diagnostics-redis`) and is started via `DiagnosticsModule` `transports.redis()` or `DiagnosticsRedisModule.forRoot`/`forRootAsync({ pub, sub, receive, libs, channels, all }) wires it from DI; createDiagnosticsRedisRelay() is the manual form returning a teardown. Forwards selected local aviary:<lib>:<event> channels to Redis and re-emits remote events locally so @OnDiagnostic fires cross-process. Needs separate pub and sub ioredis connections (redis.duplicate()) unless `receive: false` publish-only; loop-safe via nodeId echo suppression with re-emitted copies marked (`isRelayed`); envelopes keep their `id`; payloads cross as JSON."
|
|
4
4
|
license: MIT
|
|
5
5
|
metadata:
|
|
6
6
|
type: core
|
|
@@ -11,11 +11,15 @@ metadata:
|
|
|
11
11
|
|
|
12
12
|
# Cross-process diagnostics over Redis
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
The Redis relay is an opt-in **relay**: it forwards selected
|
|
15
15
|
local `aviary:<lib>:<event>` channels onto Redis pub/sub and re-emits
|
|
16
16
|
Redis-received events back onto the local diagnostics bus — so `@OnDiagnostic`
|
|
17
17
|
handlers and `getChannel(...).subscribe(...)` reactions fire **across
|
|
18
|
-
processes/pods**.
|
|
18
|
+
processes/pods**. It lives in `@dudousxd/nestjs-diagnostics`
|
|
19
|
+
(`createDiagnosticsRedisRelay`); `@dudousxd/nestjs-diagnostics-redis`
|
|
20
|
+
re-exports it and provides `DiagnosticsRedisModule`. The same relay also
|
|
21
|
+
starts as `DiagnosticsModule` `transports.redis()` (libs with listeners are
|
|
22
|
+
forwarded automatically).
|
|
19
23
|
|
|
20
24
|
## Setup
|
|
21
25
|
|
|
@@ -24,7 +28,9 @@ pnpm add @dudousxd/nestjs-diagnostics-redis @dudousxd/nestjs-diagnostics ioredis
|
|
|
24
28
|
```
|
|
25
29
|
|
|
26
30
|
Supply your own ioredis connections — a publisher and a **separate** subscriber
|
|
27
|
-
(`redis.duplicate()`, since a subscribed connection can't publish)
|
|
31
|
+
(`redis.duplicate()`, since a subscribed connection can't publish), unless
|
|
32
|
+
`receive: false` for a publish-only relay (then `sub` is optional, e.g. a
|
|
33
|
+
`role: 'api'` process that publishes and never subscribes). Prefer
|
|
28
34
|
`forRootAsync` so the relay's clients come from the same DI container:
|
|
29
35
|
|
|
30
36
|
```ts
|
|
@@ -52,7 +58,7 @@ export class AppModule {}
|
|
|
52
58
|
Now an `@OnDiagnostic('durable', 'run.failed')` handler in **another** process
|
|
53
59
|
fires when a worker elsewhere emits it.
|
|
54
60
|
|
|
55
|
-
Source: `packages/redis/src/diagnostics-redis.module.ts`, `packages/redis/src/relay.ts
|
|
61
|
+
Source: `packages/redis/src/diagnostics-redis.module.ts`, `packages/core/src/redis-relay.ts` (re-exported by `packages/redis/src/relay.ts`).
|
|
56
62
|
|
|
57
63
|
## Core patterns
|
|
58
64
|
|
|
@@ -75,7 +81,7 @@ DiagnosticsRedisModule.forRoot({
|
|
|
75
81
|
Wildcard selections subscribe to matching channels at startup and via
|
|
76
82
|
`onChannelRegistered` for ones that appear later.
|
|
77
83
|
|
|
78
|
-
Source: `packages/
|
|
84
|
+
Source: `packages/core/src/redis-relay.ts` (`wildcardMatches`, `subscribeRef`, `DiagnosticsRedisRelayOptions`).
|
|
79
85
|
|
|
80
86
|
### 2. Manual relay outside Nest
|
|
81
87
|
|
|
@@ -85,34 +91,40 @@ message handler. It never opens or closes your Redis connections — you own the
|
|
|
85
91
|
lifecycle:
|
|
86
92
|
|
|
87
93
|
```ts
|
|
88
|
-
import { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics
|
|
94
|
+
import { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics';
|
|
89
95
|
|
|
90
96
|
const teardown = createDiagnosticsRedisRelay({
|
|
91
97
|
pub: redis,
|
|
92
98
|
sub: redis.duplicate(),
|
|
93
99
|
libs: ['durable'],
|
|
94
100
|
});
|
|
101
|
+
// publish-only (never subscribes; sub optional):
|
|
102
|
+
// createDiagnosticsRedisRelay({ pub: redis, receive: false, libs: ['durable'] });
|
|
95
103
|
// … later
|
|
96
104
|
teardown();
|
|
97
105
|
```
|
|
98
106
|
|
|
99
|
-
|
|
107
|
+
Re-exported unchanged from `@dudousxd/nestjs-diagnostics-redis`, so the old
|
|
108
|
+
import path keeps working. Forwarding is batched off the emitter's call stack: `emit()` only queues the
|
|
100
109
|
event, and the queue is serialized and published once per event loop turn in emit
|
|
101
|
-
order (one Redis message per event, unchanged wire format) — via a single
|
|
110
|
+
order (one Redis message per event, unchanged wire format, `id` kept) — via a single
|
|
102
111
|
pipeline when the turn queued more than one event and `pub` supports
|
|
103
112
|
`pipeline()`, otherwise one `publish` per message.
|
|
104
113
|
|
|
105
|
-
Source: `packages/
|
|
114
|
+
Source: `packages/core/src/redis-relay.ts` (`createDiagnosticsRedisRelay` `forward`/`flush`).
|
|
106
115
|
|
|
107
116
|
### 3. Loop-safety
|
|
108
117
|
|
|
109
118
|
Each relay tags its publishes with a per-process `nodeId`. The receiver skips its
|
|
110
|
-
own echoes (`node === nodeId`) and a re-emit guard
|
|
111
|
-
received event from being forwarded back — so two processes never ping-pong.
|
|
119
|
+
own echoes (`node === nodeId`) and a re-emit guard stops a
|
|
120
|
+
received event from being forwarded back — so two processes never ping-pong.
|
|
121
|
+
Re-emitted copies are marked (`isRelayed`) so delivering transports
|
|
122
|
+
(`queue`, `durable`) deliver only at the origin and never twice; the envelope
|
|
123
|
+
keeps its `id` across the hop. The
|
|
112
124
|
default Redis channel is `aviary:diagnostics:relay` (override with
|
|
113
125
|
`redisChannel`).
|
|
114
126
|
|
|
115
|
-
Source: `packages/
|
|
127
|
+
Source: `packages/core/src/redis-relay.ts` (`forward`, `onMessage`, `reEmitting`), `packages/core/src/relayed.ts` (`isRelayed`).
|
|
116
128
|
|
|
117
129
|
## Common mistakes
|
|
118
130
|
|
|
@@ -131,8 +143,9 @@ DiagnosticsRedisModule.forRoot({ pub: redis, sub: redis.duplicate(), libs: ['dur
|
|
|
131
143
|
```
|
|
132
144
|
|
|
133
145
|
ioredis puts a connection that has `subscribe`d into subscriber mode where most
|
|
134
|
-
commands (including `publish`) are rejected — the relay needs two connections
|
|
135
|
-
|
|
146
|
+
commands (including `publish`) are rejected — the relay needs two connections,
|
|
147
|
+
unless `receive: false` (publish-only, no `sub`).
|
|
148
|
+
Source: `packages/core/src/redis-relay.ts` (`sub` docs: "separate from `pub`").
|
|
136
149
|
|
|
137
150
|
### Reading methods/Date on a cross-process payload
|
|
138
151
|
|
|
@@ -154,7 +167,7 @@ onFail(e: DiagnosticEvent<{ failedAt: string }>) {
|
|
|
154
167
|
Payloads cross the process boundary as JSON: class instances arrive as plain
|
|
155
168
|
objects (no methods/prototype), `Date` as an ISO string. Read fields, not
|
|
156
169
|
behavior, in cross-process handlers.
|
|
157
|
-
Source: `packages/
|
|
170
|
+
Source: `packages/core/src/redis-relay.ts` (`forward`/`onMessage` JSON), redis README "Notes".
|
|
158
171
|
|
|
159
172
|
### Expecting the module to manage your Redis connection lifecycle
|
|
160
173
|
|
|
@@ -173,4 +186,4 @@ The relay teardown first publishes any queued events, then removes its own
|
|
|
173
186
|
subscriptions and Redis message handler; it never opens or closes `pub`/`sub`.
|
|
174
187
|
Manage their lifecycle in your own provider.
|
|
175
188
|
Source: `packages/redis/src/diagnostics-redis.module.ts` (`DiagnosticsRedisStarter` teardown),
|
|
176
|
-
`packages/
|
|
189
|
+
`packages/core/src/redis-relay.ts` (return teardown).
|