@dudousxd/nestjs-diagnostics-redis 0.1.2 → 0.2.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/CHANGELOG.md +15 -0
- package/README.md +15 -8
- package/dist/diagnostics-redis.module.d.ts +18 -1
- package/dist/diagnostics-redis.module.d.ts.map +1 -1
- package/dist/diagnostics-redis.module.js +27 -5
- package/dist/diagnostics-redis.module.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +6 -4
- package/skills/redis-transport/SKILL.md +168 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @dudousxd/nestjs-diagnostics-redis
|
|
2
2
|
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#14](https://github.com/DavideCarvalho/nestjs-diagnostics/pull/14) [`cced7fc`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/cced7fc843c84eaf7b51686321294355bf2dd346) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - Ship TanStack Intent agent skills (SKILL.md) inside the package.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`cced7fc`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/cced7fc843c84eaf7b51686321294355bf2dd346)]:
|
|
10
|
+
- @dudousxd/nestjs-diagnostics@0.6.1
|
|
11
|
+
|
|
12
|
+
## 0.2.0
|
|
13
|
+
|
|
14
|
+
### Minor Changes
|
|
15
|
+
|
|
16
|
+
- [`af1dcf1`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/af1dcf149534b0cc211cd60ac2a470182e71ce44) - Add `DiagnosticsRedisModule.forRootAsync({ imports?, inject?, useFactory })` so the relay's `pub`/`sub` ioredis clients can come from DI instead of a top-level `new Redis(...)`. `forRoot` still works for pre-built connections.
|
|
17
|
+
|
|
3
18
|
## 0.1.2
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -15,20 +15,24 @@ pnpm add @dudousxd/nestjs-diagnostics-redis @dudousxd/nestjs-diagnostics ioredis
|
|
|
15
15
|
## Use (Nest)
|
|
16
16
|
|
|
17
17
|
Supply your own ioredis connections — a publisher and a **separate** subscriber (`redis.duplicate()`,
|
|
18
|
-
since a subscribed connection can't publish):
|
|
18
|
+
since a subscribed connection can't publish). Use `forRootAsync` to pull your Redis client from DI:
|
|
19
19
|
|
|
20
20
|
```ts
|
|
21
|
-
import Redis from 'ioredis';
|
|
21
|
+
import type Redis from 'ioredis';
|
|
22
|
+
import { Module } from '@nestjs/common';
|
|
22
23
|
import { DiagnosticsRedisModule } from '@dudousxd/nestjs-diagnostics-redis';
|
|
23
|
-
|
|
24
|
-
const redis = new Redis(process.env.REDIS_URL);
|
|
24
|
+
import { REDIS, RedisModule } from './redis.module'; // your app's Redis provider
|
|
25
25
|
|
|
26
26
|
@Module({
|
|
27
27
|
imports: [
|
|
28
|
-
DiagnosticsRedisModule.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
DiagnosticsRedisModule.forRootAsync({
|
|
29
|
+
imports: [RedisModule],
|
|
30
|
+
inject: [REDIS],
|
|
31
|
+
useFactory: (redis: Redis) => ({
|
|
32
|
+
pub: redis,
|
|
33
|
+
sub: redis.duplicate(),
|
|
34
|
+
libs: ['durable', 'notifications'], // forward all events of these libs
|
|
35
|
+
}),
|
|
32
36
|
}),
|
|
33
37
|
],
|
|
34
38
|
})
|
|
@@ -38,6 +42,9 @@ export class AppModule {}
|
|
|
38
42
|
Now an `@OnDiagnostic('durable', 'run.failed')` handler in **another** process fires when a worker
|
|
39
43
|
elsewhere emits it.
|
|
40
44
|
|
|
45
|
+
Already holding the connections? The static `forRoot({ pub, sub, libs })` takes them directly — but
|
|
46
|
+
prefer `forRootAsync` so the relay's clients come from the same DI container as the rest of your app.
|
|
47
|
+
|
|
41
48
|
## Use (manual)
|
|
42
49
|
|
|
43
50
|
```ts
|
|
@@ -2,17 +2,34 @@ import { type DynamicModule } from '@nestjs/common';
|
|
|
2
2
|
import { type DiagnosticsRedisRelayOptions } from './relay.js';
|
|
3
3
|
export interface DiagnosticsRedisModuleOptions extends DiagnosticsRedisRelayOptions {
|
|
4
4
|
}
|
|
5
|
+
/** Async configuration — build the relay options (incl. your ioredis clients) from DI. */
|
|
6
|
+
export interface DiagnosticsRedisModuleAsyncOptions {
|
|
7
|
+
/** Modules to import so the factory can inject their exported providers (e.g. your Redis module). */
|
|
8
|
+
imports?: DynamicModule['imports'];
|
|
9
|
+
inject?: unknown[];
|
|
10
|
+
useFactory: (...args: never[]) => Promise<DiagnosticsRedisModuleOptions> | DiagnosticsRedisModuleOptions;
|
|
11
|
+
}
|
|
5
12
|
/**
|
|
6
13
|
* Import once at the app root to relay diagnostics events across processes over Redis. Supply your
|
|
7
14
|
* `pub` / `sub` ioredis connections (e.g. `redis` and `redis.duplicate()`) and the channel selection.
|
|
8
15
|
* The module manages only the relay's subscriptions — it does NOT open or close your Redis clients.
|
|
9
16
|
*
|
|
10
17
|
* ```ts
|
|
11
|
-
* @Module({
|
|
18
|
+
* @Module({
|
|
19
|
+
* imports: [
|
|
20
|
+
* DiagnosticsRedisModule.forRootAsync({
|
|
21
|
+
* inject: [REDIS],
|
|
22
|
+
* useFactory: (redis: Redis) => ({ pub: redis, sub: redis.duplicate(), libs: ['durable'] }),
|
|
23
|
+
* }),
|
|
24
|
+
* ],
|
|
25
|
+
* })
|
|
12
26
|
* export class AppModule {}
|
|
13
27
|
* ```
|
|
14
28
|
*/
|
|
15
29
|
export declare class DiagnosticsRedisModule {
|
|
30
|
+
/** Static configuration — pass already-built `pub` / `sub` clients and the channel selection. */
|
|
16
31
|
static forRoot(options: DiagnosticsRedisModuleOptions): DynamicModule;
|
|
32
|
+
/** Async configuration — build the options (incl. your ioredis clients) from injected dependencies. */
|
|
33
|
+
static forRootAsync(options: DiagnosticsRedisModuleAsyncOptions): DynamicModule;
|
|
17
34
|
}
|
|
18
35
|
//# sourceMappingURL=diagnostics-redis.module.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diagnostics-redis.module.d.ts","sourceRoot":"","sources":["../src/diagnostics-redis.module.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,
|
|
1
|
+
{"version":3,"file":"diagnostics-redis.module.d.ts","sourceRoot":"","sources":["../src/diagnostics-redis.module.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAOnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,KAAK,4BAA4B,EAA+B,MAAM,YAAY,CAAC;AAE5F,MAAM,WAAW,6BAA8B,SAAQ,4BAA4B;CAAG;AAEtF,0FAA0F;AAC1F,MAAM,WAAW,kCAAkC;IACjD,qGAAqG;IACrG,OAAO,CAAC,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC;IACnC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;IACnB,UAAU,EAAE,CACV,GAAG,IAAI,EAAE,KAAK,EAAE,KACb,OAAO,CAAC,6BAA6B,CAAC,GAAG,6BAA6B,CAAC;CAC7E;AA2BD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAEa,sBAAsB;IACjC,iGAAiG;IACjG,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,6BAA6B,GAAG,aAAa;IAOrE,uGAAuG;IACvG,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,kCAAkC,GAAG,aAAa;CAchF"}
|
|
@@ -29,27 +29,49 @@ DiagnosticsRedisStarter = __decorate([
|
|
|
29
29
|
Injectable(),
|
|
30
30
|
__metadata("design:paramtypes", [Object])
|
|
31
31
|
], DiagnosticsRedisStarter);
|
|
32
|
+
/** Starts the relay from whatever is bound to RELAY_OPTIONS — shared by forRoot and forRootAsync. */
|
|
33
|
+
const starterProvider = {
|
|
34
|
+
provide: DiagnosticsRedisStarter,
|
|
35
|
+
useFactory: (opts) => new DiagnosticsRedisStarter(opts),
|
|
36
|
+
inject: [RELAY_OPTIONS],
|
|
37
|
+
};
|
|
32
38
|
/**
|
|
33
39
|
* Import once at the app root to relay diagnostics events across processes over Redis. Supply your
|
|
34
40
|
* `pub` / `sub` ioredis connections (e.g. `redis` and `redis.duplicate()`) and the channel selection.
|
|
35
41
|
* The module manages only the relay's subscriptions — it does NOT open or close your Redis clients.
|
|
36
42
|
*
|
|
37
43
|
* ```ts
|
|
38
|
-
* @Module({
|
|
44
|
+
* @Module({
|
|
45
|
+
* imports: [
|
|
46
|
+
* DiagnosticsRedisModule.forRootAsync({
|
|
47
|
+
* inject: [REDIS],
|
|
48
|
+
* useFactory: (redis: Redis) => ({ pub: redis, sub: redis.duplicate(), libs: ['durable'] }),
|
|
49
|
+
* }),
|
|
50
|
+
* ],
|
|
51
|
+
* })
|
|
39
52
|
* export class AppModule {}
|
|
40
53
|
* ```
|
|
41
54
|
*/
|
|
42
55
|
let DiagnosticsRedisModule = DiagnosticsRedisModule_1 = class DiagnosticsRedisModule {
|
|
56
|
+
/** Static configuration — pass already-built `pub` / `sub` clients and the channel selection. */
|
|
43
57
|
static forRoot(options) {
|
|
44
58
|
return {
|
|
45
59
|
module: DiagnosticsRedisModule_1,
|
|
60
|
+
providers: [{ provide: RELAY_OPTIONS, useValue: options }, starterProvider],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Async configuration — build the options (incl. your ioredis clients) from injected dependencies. */
|
|
64
|
+
static forRootAsync(options) {
|
|
65
|
+
return {
|
|
66
|
+
module: DiagnosticsRedisModule_1,
|
|
67
|
+
imports: options.imports ?? [],
|
|
46
68
|
providers: [
|
|
47
|
-
{ provide: RELAY_OPTIONS, useValue: options },
|
|
48
69
|
{
|
|
49
|
-
provide:
|
|
50
|
-
useFactory:
|
|
51
|
-
inject: [
|
|
70
|
+
provide: RELAY_OPTIONS,
|
|
71
|
+
useFactory: options.useFactory,
|
|
72
|
+
inject: (options.inject ?? []),
|
|
52
73
|
},
|
|
74
|
+
starterProvider,
|
|
53
75
|
],
|
|
54
76
|
};
|
|
55
77
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diagnostics-redis.module.js","sourceRoot":"","sources":["../src/diagnostics-redis.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAEL,MAAM,EACN,UAAU,EACV,MAAM,
|
|
1
|
+
{"version":3,"file":"diagnostics-redis.module.js","sourceRoot":"","sources":["../src/diagnostics-redis.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAEL,MAAM,EACN,UAAU,EACV,MAAM,GAIP,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAqC,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAc5F,MAAM,aAAa,GAAG,MAAM,CAAC,2BAA2B,CAAC,CAAC;AAE1D,IACM,uBAAuB,GAD7B,MACM,uBAAuB;IAGE;IAFrB,QAAQ,GAAwB,IAAI,CAAC;IAE7C,YAA6B,OAAsC;QAAtC,YAAO,GAAP,OAAO,CAA+B;IAAG,CAAC;IAEvE,sBAAsB;QACpB,IAAI,CAAC,QAAQ,GAAG,2BAA2B,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC5D,CAAC;IAED,qBAAqB;QACnB,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC;QAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;CACF,CAAA;AAbK,uBAAuB;IAD5B,UAAU,EAAE;;GACP,uBAAuB,CAa5B;AAED,qGAAqG;AACrG,MAAM,eAAe,GAAa;IAChC,OAAO,EAAE,uBAAuB;IAChC,UAAU,EAAE,CAAC,IAAmC,EAAE,EAAE,CAAC,IAAI,uBAAuB,CAAC,IAAI,CAAC;IACtF,MAAM,EAAE,CAAC,aAAa,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AAGI,IAAM,sBAAsB,8BAA5B,MAAM,sBAAsB;IACjC,iGAAiG;IACjG,MAAM,CAAC,OAAO,CAAC,OAAsC;QACnD,OAAO;YACL,MAAM,EAAE,wBAAsB;YAC9B,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,eAAe,CAAC;SAC5E,CAAC;IACJ,CAAC;IAED,uGAAuG;IACvG,MAAM,CAAC,YAAY,CAAC,OAA2C;QAC7D,OAAO;YACL,MAAM,EAAE,wBAAsB;YAC9B,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,EAAE;YAC9B,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,aAAa;oBACtB,UAAU,EAAE,OAAO,CAAC,UAAU;oBAC9B,MAAM,EAAE,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAU;iBACxC;gBACD,eAAe;aAChB;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AAxBY,sBAAsB;IAFlC,MAAM,EAAE;IACR,MAAM,CAAC,EAAE,CAAC;GACE,sBAAsB,CAwBlC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createDiagnosticsRedisRelay } from './relay.js';
|
|
2
2
|
export type { ChannelRef, DiagnosticsRedisRelayOptions, RedisLike } from './relay.js';
|
|
3
3
|
export { DiagnosticsRedisModule } from './diagnostics-redis.module.js';
|
|
4
|
-
export type { DiagnosticsRedisModuleOptions } from './diagnostics-redis.module.js';
|
|
4
|
+
export type { DiagnosticsRedisModuleAsyncOptions, DiagnosticsRedisModuleOptions, } from './diagnostics-redis.module.js';
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AACzD,YAAY,EAAE,UAAU,EAAE,4BAA4B,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEtF,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AACzD,YAAY,EAAE,UAAU,EAAE,4BAA4B,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEtF,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,YAAY,EACV,kCAAkC,EAClC,6BAA6B,GAC9B,MAAM,+BAA+B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dudousxd/nestjs-diagnostics-redis",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
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": {
|
|
@@ -22,11 +22,12 @@
|
|
|
22
22
|
"files": [
|
|
23
23
|
"dist/",
|
|
24
24
|
"README.md",
|
|
25
|
-
"CHANGELOG.md"
|
|
25
|
+
"CHANGELOG.md",
|
|
26
|
+
"skills"
|
|
26
27
|
],
|
|
27
28
|
"sideEffects": false,
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"@dudousxd/nestjs-diagnostics": "^0.6.
|
|
30
|
+
"@dudousxd/nestjs-diagnostics": "^0.6.1"
|
|
30
31
|
},
|
|
31
32
|
"peerDependencies": {
|
|
32
33
|
"ioredis": "^5",
|
|
@@ -65,7 +66,8 @@
|
|
|
65
66
|
"redis",
|
|
66
67
|
"transport",
|
|
67
68
|
"cross-process",
|
|
68
|
-
"aviary"
|
|
69
|
+
"aviary",
|
|
70
|
+
"tanstack-intent"
|
|
69
71
|
],
|
|
70
72
|
"scripts": {
|
|
71
73
|
"build": "tsc -p tsconfig.json",
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: redis-transport
|
|
3
|
+
description: Relay @dudousxd/nestjs-diagnostics events across processes/pods over Redis pub/sub with @dudousxd/nestjs-diagnostics-redis. DiagnosticsRedisModule.forRootAsync/forRoot({ pub, sub, 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()); loop-safe via nodeId echo suppression; payloads cross as JSON.
|
|
4
|
+
license: MIT
|
|
5
|
+
metadata:
|
|
6
|
+
type: core
|
|
7
|
+
library: "@dudousxd/nestjs-diagnostics-redis"
|
|
8
|
+
library_version: 0.1.2
|
|
9
|
+
framework: nestjs
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# Cross-process diagnostics over Redis
|
|
13
|
+
|
|
14
|
+
`@dudousxd/nestjs-diagnostics-redis` is an opt-in **relay**: it forwards selected
|
|
15
|
+
local `aviary:<lib>:<event>` channels onto Redis pub/sub and re-emits
|
|
16
|
+
Redis-received events back onto the local diagnostics bus — so `@OnDiagnostic`
|
|
17
|
+
handlers and `getChannel(...).subscribe(...)` reactions fire **across
|
|
18
|
+
processes/pods**. The diagnostics core stays in-process and untouched.
|
|
19
|
+
|
|
20
|
+
## Setup
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add @dudousxd/nestjs-diagnostics-redis @dudousxd/nestjs-diagnostics ioredis
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Supply your own ioredis connections — a publisher and a **separate** subscriber
|
|
27
|
+
(`redis.duplicate()`, since a subscribed connection can't publish). Prefer
|
|
28
|
+
`forRootAsync` so the relay's clients come from the same DI container:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import type Redis from 'ioredis';
|
|
32
|
+
import { Module } from '@nestjs/common';
|
|
33
|
+
import { DiagnosticsRedisModule } from '@dudousxd/nestjs-diagnostics-redis';
|
|
34
|
+
import { REDIS, RedisModule } from './redis.module';
|
|
35
|
+
|
|
36
|
+
@Module({
|
|
37
|
+
imports: [
|
|
38
|
+
DiagnosticsRedisModule.forRootAsync({
|
|
39
|
+
imports: [RedisModule],
|
|
40
|
+
inject: [REDIS],
|
|
41
|
+
useFactory: (redis: Redis) => ({
|
|
42
|
+
pub: redis,
|
|
43
|
+
sub: redis.duplicate(),
|
|
44
|
+
libs: ['durable', 'notifications'], // forward all events of these libs
|
|
45
|
+
}),
|
|
46
|
+
}),
|
|
47
|
+
],
|
|
48
|
+
})
|
|
49
|
+
export class AppModule {}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Now an `@OnDiagnostic('durable', 'run.failed')` handler in **another** process
|
|
53
|
+
fires when a worker elsewhere emits it.
|
|
54
|
+
|
|
55
|
+
Source: `packages/redis/src/diagnostics-redis.module.ts`, `packages/redis/src/relay.ts`.
|
|
56
|
+
|
|
57
|
+
## Core patterns
|
|
58
|
+
|
|
59
|
+
### 1. Selecting what to forward: libs, channels, all
|
|
60
|
+
|
|
61
|
+
The relay options pick which local channels are forwarded:
|
|
62
|
+
|
|
63
|
+
- `libs: ['durable']` — every `aviary:durable:*` channel (current + future).
|
|
64
|
+
- `channels: [{ lib: 'resilience', event: 'circuit-opened' }]` — exact channels.
|
|
65
|
+
- `all: true` — every `aviary:*` channel; overrides `libs`/`channels`.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
DiagnosticsRedisModule.forRoot({
|
|
69
|
+
pub: redis,
|
|
70
|
+
sub: redis.duplicate(),
|
|
71
|
+
channels: [{ lib: 'resilience', event: 'circuit-opened' }],
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Wildcard selections subscribe to matching channels at startup and via
|
|
76
|
+
`onChannelRegistered` for ones that appear later.
|
|
77
|
+
|
|
78
|
+
Source: `packages/redis/src/relay.ts` (`wildcardMatches`, `subscribeRef`, `DiagnosticsRedisRelayOptions`).
|
|
79
|
+
|
|
80
|
+
### 2. Manual relay outside Nest
|
|
81
|
+
|
|
82
|
+
`createDiagnosticsRedisRelay(options)` returns a `teardown()` that removes all
|
|
83
|
+
local subscriptions and the Redis message handler. It never opens or closes your
|
|
84
|
+
Redis connections — you own their lifecycle:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics-redis';
|
|
88
|
+
|
|
89
|
+
const teardown = createDiagnosticsRedisRelay({
|
|
90
|
+
pub: redis,
|
|
91
|
+
sub: redis.duplicate(),
|
|
92
|
+
libs: ['durable'],
|
|
93
|
+
});
|
|
94
|
+
// … later
|
|
95
|
+
teardown();
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Source: `packages/redis/src/relay.ts` (`createDiagnosticsRedisRelay`).
|
|
99
|
+
|
|
100
|
+
### 3. Loop-safety
|
|
101
|
+
|
|
102
|
+
Each relay tags its publishes with a per-process `nodeId`. The receiver skips its
|
|
103
|
+
own echoes (`node === nodeId`) and a re-emit guard (a `WeakSet`) stops a
|
|
104
|
+
received event from being forwarded back — so two processes never ping-pong. The
|
|
105
|
+
default Redis channel is `aviary:diagnostics:relay` (override with
|
|
106
|
+
`redisChannel`).
|
|
107
|
+
|
|
108
|
+
Source: `packages/redis/src/relay.ts` (`forward`, `onMessage`, `reEmitting`).
|
|
109
|
+
|
|
110
|
+
## Common mistakes
|
|
111
|
+
|
|
112
|
+
### Reusing one ioredis connection for both pub and sub
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
// Wrong — once `sub` enters subscriber mode it can't publish; commands error.
|
|
116
|
+
const conn = new Redis();
|
|
117
|
+
DiagnosticsRedisModule.forRoot({ pub: conn, sub: conn, libs: ['durable'] });
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
// Correct — a separate subscriber via duplicate().
|
|
122
|
+
const redis = new Redis();
|
|
123
|
+
DiagnosticsRedisModule.forRoot({ pub: redis, sub: redis.duplicate(), libs: ['durable'] });
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
ioredis puts a connection that has `subscribe`d into subscriber mode where most
|
|
127
|
+
commands (including `publish`) are rejected — the relay needs two connections.
|
|
128
|
+
Source: `packages/redis/src/relay.ts` (`sub` docs: "separate from `pub`").
|
|
129
|
+
|
|
130
|
+
### Reading methods/Date on a cross-process payload
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
// Wrong — across the boundary the payload is plain JSON; methods/prototype are gone.
|
|
134
|
+
@OnDiagnostic('durable', 'run.failed')
|
|
135
|
+
onFail(e: DiagnosticEvent<RunFailed>) {
|
|
136
|
+
e.payload.failedAt.getTime(); // failedAt is an ISO string, not a Date — throws
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
// Correct — read fields, and parse serialized values yourself.
|
|
142
|
+
onFail(e: DiagnosticEvent<{ failedAt: string }>) {
|
|
143
|
+
const at = new Date(e.payload.failedAt).getTime();
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Payloads cross the process boundary as JSON: class instances arrive as plain
|
|
148
|
+
objects (no methods/prototype), `Date` as an ISO string. Read fields, not
|
|
149
|
+
behavior, in cross-process handlers.
|
|
150
|
+
Source: `packages/redis/src/relay.ts` (`forward`/`onMessage` JSON), redis README "Notes".
|
|
151
|
+
|
|
152
|
+
### Expecting the module to manage your Redis connection lifecycle
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
// Wrong — assuming the module connects/quits Redis for you.
|
|
156
|
+
DiagnosticsRedisModule.forRoot({ pub: redis, sub: redis.duplicate(), libs: ['durable'] });
|
|
157
|
+
// (no redis.quit() anywhere) — connections leak on shutdown
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
// Correct — you own connect/quit; the module only manages relay subscriptions.
|
|
162
|
+
// e.g. in your Redis provider's onApplicationShutdown: await redis.quit();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The relay teardown removes only its own subscriptions and Redis message handler;
|
|
166
|
+
it never opens or closes `pub`/`sub`. Manage their lifecycle in your own provider.
|
|
167
|
+
Source: `packages/redis/src/diagnostics-redis.module.ts` (`DiagnosticsRedisStarter` teardown),
|
|
168
|
+
`packages/redis/src/relay.ts` (return teardown).
|