nmtjs 0.16.0 → 0.16.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/dist/index.d.ts +44 -4
- package/dist/runtime/injectables.d.ts +45 -4
- package/dist/runtime/injectables.js +19 -3
- package/dist/runtime/injectables.js.map +1 -1
- package/dist/runtime/subscription/manager.d.ts +3 -3
- package/dist/runtime/subscription/manager.js +8 -8
- package/dist/runtime/subscription/manager.js.map +1 -1
- package/dist/runtime/subscription/redis.js +16 -17
- package/dist/runtime/subscription/redis.js.map +1 -1
- package/dist/runtime/workers/base.d.ts +0 -2
- package/dist/runtime/workers/base.js +3 -8
- package/dist/runtime/workers/base.js.map +1 -1
- package/package.json +12 -12
- package/src/runtime/injectables.ts +33 -4
- package/src/runtime/subscription/manager.ts +11 -14
- package/src/runtime/subscription/redis.ts +24 -22
- package/src/runtime/workers/base.ts +9 -15
package/dist/index.d.ts
CHANGED
|
@@ -31,8 +31,28 @@ export declare namespace neemata {
|
|
|
31
31
|
createBlob: import("@nmtjs/core").LazyInjectable<(source: import("stream").Readable | globalThis.ReadableStream | File | Blob | string | ArrayBuffer | Uint8Array, metadata?: import("@nmtjs/protocol").ProtocolBlobMetadata) => import("@nmtjs/protocol").ProtocolBlobInterface, import("@nmtjs/core").Scope.Call>;
|
|
32
32
|
consumeBlob: import("@nmtjs/core").LazyInjectable<(blob: import("@nmtjs/protocol").ProtocolBlobInterface) => import("@nmtjs/protocol/server").ProtocolClientStream, import("@nmtjs/core").Scope.Call>;
|
|
33
33
|
subscriptionAdapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
subscriptionManager: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscriptionManager, {
|
|
35
|
+
adapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
36
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
37
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
38
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
39
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscriptionManager>;
|
|
40
|
+
publish: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").PublishFn, {
|
|
41
|
+
manager: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscriptionManager, {
|
|
42
|
+
adapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
43
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
44
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
45
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
46
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscriptionManager>;
|
|
47
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").PublishFn>;
|
|
48
|
+
subscribe: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscribeFn, {
|
|
49
|
+
manager: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscriptionManager, {
|
|
50
|
+
adapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
51
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
52
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
53
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
54
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscriptionManager>;
|
|
55
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscribeFn>;
|
|
36
56
|
jobManager: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").JobManagerInstance, import("@nmtjs/core").Scope.Global>;
|
|
37
57
|
storeConfig: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").ServerStoreConfig, import("@nmtjs/core").Scope.Global>;
|
|
38
58
|
workerType: import("@nmtjs/core").LazyInjectable<import("./runtime/enums.ts").WorkerType, import("@nmtjs/core").Scope.Global>;
|
|
@@ -66,8 +86,28 @@ export declare namespace neemata {
|
|
|
66
86
|
createBlob: import("@nmtjs/core").LazyInjectable<(source: import("stream").Readable | globalThis.ReadableStream | File | Blob | string | ArrayBuffer | Uint8Array, metadata?: import("@nmtjs/protocol").ProtocolBlobMetadata) => import("@nmtjs/protocol").ProtocolBlobInterface, import("@nmtjs/core").Scope.Call>;
|
|
67
87
|
consumeBlob: import("@nmtjs/core").LazyInjectable<(blob: import("@nmtjs/protocol").ProtocolBlobInterface) => import("@nmtjs/protocol/server").ProtocolClientStream, import("@nmtjs/core").Scope.Call>;
|
|
68
88
|
subscriptionAdapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
69
|
-
|
|
70
|
-
|
|
89
|
+
subscriptionManager: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscriptionManager, {
|
|
90
|
+
adapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
91
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
92
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
93
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
94
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscriptionManager>;
|
|
95
|
+
publish: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").PublishFn, {
|
|
96
|
+
manager: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscriptionManager, {
|
|
97
|
+
adapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
98
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
99
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
100
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
101
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscriptionManager>;
|
|
102
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").PublishFn>;
|
|
103
|
+
subscribe: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscribeFn, {
|
|
104
|
+
manager: import("@nmtjs/core").FactoryInjectable<import("./runtime/index.ts").SubscriptionManager, {
|
|
105
|
+
adapter: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").SubscriptionAdapterType, import("@nmtjs/core").Scope.Global>;
|
|
106
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
107
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
108
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, import("@nmtjs/core").Scope.Global>;
|
|
109
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscriptionManager>;
|
|
110
|
+
}, import("@nmtjs/core").Scope.Global, import("./runtime/index.ts").SubscribeFn>;
|
|
71
111
|
jobManager: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").JobManagerInstance, import("@nmtjs/core").Scope.Global>;
|
|
72
112
|
storeConfig: import("@nmtjs/core").LazyInjectable<import("./runtime/index.ts").ServerStoreConfig, import("@nmtjs/core").Scope.Global>;
|
|
73
113
|
workerType: import("@nmtjs/core").LazyInjectable<import("./runtime/enums.ts").WorkerType, import("@nmtjs/core").Scope.Global>;
|
|
@@ -4,9 +4,30 @@ import type { JobManagerInstance } from './jobs/manager.ts';
|
|
|
4
4
|
import type { JobExecutionContext, SaveJobProgress } from './jobs/types.ts';
|
|
5
5
|
import type { ServerStoreConfig } from './server/config.ts';
|
|
6
6
|
import type { PublishFn, SubscribeFn, SubscriptionAdapterType } from './subscription/manager.ts';
|
|
7
|
+
import { SubscriptionManager } from './subscription/manager.ts';
|
|
7
8
|
export declare const subscriptionAdapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
8
|
-
export declare const
|
|
9
|
-
|
|
9
|
+
export declare const subscriptionManager: import("@nmtjs/core").FactoryInjectable<SubscriptionManager, {
|
|
10
|
+
adapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
11
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
12
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
13
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, Scope.Global>;
|
|
14
|
+
}, Scope.Global, SubscriptionManager>;
|
|
15
|
+
export declare const publish: import("@nmtjs/core").FactoryInjectable<PublishFn, {
|
|
16
|
+
manager: import("@nmtjs/core").FactoryInjectable<SubscriptionManager, {
|
|
17
|
+
adapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
18
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
19
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
20
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, Scope.Global>;
|
|
21
|
+
}, Scope.Global, SubscriptionManager>;
|
|
22
|
+
}, Scope.Global, PublishFn>;
|
|
23
|
+
export declare const subscribe: import("@nmtjs/core").FactoryInjectable<SubscribeFn, {
|
|
24
|
+
manager: import("@nmtjs/core").FactoryInjectable<SubscriptionManager, {
|
|
25
|
+
adapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
26
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
27
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
28
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, Scope.Global>;
|
|
29
|
+
}, Scope.Global, SubscriptionManager>;
|
|
30
|
+
}, Scope.Global, SubscribeFn>;
|
|
10
31
|
export declare const jobManager: import("@nmtjs/core").LazyInjectable<JobManagerInstance, Scope.Global>;
|
|
11
32
|
export declare const storeConfig: import("@nmtjs/core").LazyInjectable<ServerStoreConfig, Scope.Global>;
|
|
12
33
|
export declare const workerType: import("@nmtjs/core").LazyInjectable<WorkerType, Scope.Global>;
|
|
@@ -16,8 +37,28 @@ export declare const saveJobProgress: import("@nmtjs/core").LazyInjectable<SaveJ
|
|
|
16
37
|
export declare const currentJobInfo: import("@nmtjs/core").LazyInjectable<JobExecutionContext, Scope.Global>;
|
|
17
38
|
export declare const RuntimeInjectables: {
|
|
18
39
|
subscriptionAdapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
19
|
-
|
|
20
|
-
|
|
40
|
+
subscriptionManager: import("@nmtjs/core").FactoryInjectable<SubscriptionManager, {
|
|
41
|
+
adapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
42
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
43
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
44
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, Scope.Global>;
|
|
45
|
+
}, Scope.Global, SubscriptionManager>;
|
|
46
|
+
publish: import("@nmtjs/core").FactoryInjectable<PublishFn, {
|
|
47
|
+
manager: import("@nmtjs/core").FactoryInjectable<SubscriptionManager, {
|
|
48
|
+
adapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
49
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
50
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
51
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, Scope.Global>;
|
|
52
|
+
}, Scope.Global, SubscriptionManager>;
|
|
53
|
+
}, Scope.Global, PublishFn>;
|
|
54
|
+
subscribe: import("@nmtjs/core").FactoryInjectable<SubscribeFn, {
|
|
55
|
+
manager: import("@nmtjs/core").FactoryInjectable<SubscriptionManager, {
|
|
56
|
+
adapter: import("@nmtjs/core").LazyInjectable<SubscriptionAdapterType, Scope.Global>;
|
|
57
|
+
logger: ((label: string) => import("@nmtjs/core").FactoryInjectable<import("pino").Logger, {
|
|
58
|
+
logger: import("@nmtjs/core").LazyInjectable<import("pino").Logger>;
|
|
59
|
+
}>) & import("@nmtjs/core").LazyInjectable<import("pino").Logger, Scope.Global>;
|
|
60
|
+
}, Scope.Global, SubscriptionManager>;
|
|
61
|
+
}, Scope.Global, SubscribeFn>;
|
|
21
62
|
jobManager: import("@nmtjs/core").LazyInjectable<JobManagerInstance, Scope.Global>;
|
|
22
63
|
storeConfig: import("@nmtjs/core").LazyInjectable<ServerStoreConfig, Scope.Global>;
|
|
23
64
|
workerType: import("@nmtjs/core").LazyInjectable<WorkerType, Scope.Global>;
|
|
@@ -1,7 +1,22 @@
|
|
|
1
|
-
import { createLazyInjectable, Scope } from '@nmtjs/core';
|
|
1
|
+
import { CoreInjectables, createFactoryInjectable, createLazyInjectable, Scope, } from '@nmtjs/core';
|
|
2
|
+
import { SubscriptionManager } from './subscription/manager.js';
|
|
2
3
|
export const subscriptionAdapter = createLazyInjectable(Scope.Global, 'SubscriptionAdapter');
|
|
3
|
-
export const
|
|
4
|
-
|
|
4
|
+
export const subscriptionManager = createFactoryInjectable({
|
|
5
|
+
dependencies: {
|
|
6
|
+
adapter: subscriptionAdapter,
|
|
7
|
+
logger: CoreInjectables.logger,
|
|
8
|
+
},
|
|
9
|
+
factory: ({ adapter, logger }) => new SubscriptionManager({ logger, adapter }),
|
|
10
|
+
dispose: (manager) => manager.dispose(),
|
|
11
|
+
}, 'SubscriptionManager');
|
|
12
|
+
export const publish = createFactoryInjectable({
|
|
13
|
+
dependencies: { manager: subscriptionManager },
|
|
14
|
+
factory: ({ manager }) => manager.publish.bind(manager),
|
|
15
|
+
}, 'Publish');
|
|
16
|
+
export const subscribe = createFactoryInjectable({
|
|
17
|
+
dependencies: { manager: subscriptionManager },
|
|
18
|
+
factory: ({ manager }) => manager.subscribe.bind(manager),
|
|
19
|
+
}, 'Subscribe');
|
|
5
20
|
export const jobManager = createLazyInjectable(Scope.Global, 'JobManager');
|
|
6
21
|
export const storeConfig = createLazyInjectable(Scope.Global, 'StoreConfig');
|
|
7
22
|
export const workerType = createLazyInjectable(Scope.Global, 'WorkerType');
|
|
@@ -11,6 +26,7 @@ export const saveJobProgress = createLazyInjectable(Scope.Global, 'SaveJobProgre
|
|
|
11
26
|
export const currentJobInfo = createLazyInjectable(Scope.Global, 'CurrentJobInfo');
|
|
12
27
|
export const RuntimeInjectables = {
|
|
13
28
|
subscriptionAdapter,
|
|
29
|
+
subscriptionManager,
|
|
14
30
|
publish,
|
|
15
31
|
subscribe,
|
|
16
32
|
jobManager,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectables.js","sourceRoot":"","sources":["../../src/runtime/injectables.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"injectables.js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ED,MAAM,CAAC,MAAM,WAAW,GAAG,oBAAoB,CAC7C,KAAK,CAAC,MAAM,EACZ,aAAa,CACd,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,oBAAoB,CAC5C,KAAK,CAAC,MAAM,EACZ,YAAY,CACb,CAAA;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,oBAAoB,CAC/C,KAAK,CAAC,MAAM,EACZ,eAAe,CAChB,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,oBAAoB,CAChD,KAAK,CAAC,MAAM,EACZ,gBAAgB,CACjB,CAAA;AAED,MAAM,CAAC,MAAM,eAAe,GAAG,oBAAoB,CACjD,KAAK,CAAC,MAAM,EACZ,iBAAiB,CAClB,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,oBAAoB,CAChD,KAAK,CAAC,MAAM,EACZ,gBAAgB,CACjB,CAAA;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,mBAAmB;IACnB,mBAAmB;IACnB,OAAO;IACP,SAAS;IACT,UAAU;IACV,WAAW;IACX,UAAU;IACV,aAAa;IACb,cAAc;IACd,eAAe;IACf,cAAc;CACf,CAAA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
2
|
import type { TAnyEventContract, TAnySubscriptionContract } from '@nmtjs/contract';
|
|
3
|
-
import type {
|
|
3
|
+
import type { Logger } from '@nmtjs/core';
|
|
4
4
|
import type { t } from '@nmtjs/type';
|
|
5
5
|
export type SubscriptionAdapterEvent = {
|
|
6
6
|
channel: string;
|
|
@@ -35,15 +35,15 @@ export type SubscribeFn = <Contract extends TAnySubscriptionContract, Events ext
|
|
|
35
35
|
export type PublishFn = <S extends TAnySubscriptionContract, E extends S['events'][keyof S['events']]>(event: E, options: S['options'], data: t.infer.decode.input<E['payload']>) => Promise<boolean>;
|
|
36
36
|
export type SubscriptionManagerOptions = {
|
|
37
37
|
logger: Logger;
|
|
38
|
-
|
|
38
|
+
adapter: SubscriptionAdapterType;
|
|
39
39
|
};
|
|
40
40
|
export declare class SubscriptionManager {
|
|
41
41
|
protected readonly options: SubscriptionManagerOptions;
|
|
42
42
|
readonly subscriptions: Map<string, SubscriptionChannel>;
|
|
43
43
|
protected readonly logger: Logger;
|
|
44
44
|
constructor(options: SubscriptionManagerOptions);
|
|
45
|
-
protected get adapter(): Promise<SubscriptionAdapterType>;
|
|
46
45
|
subscribe: SubscribeFn;
|
|
47
46
|
publish: PublishFn;
|
|
47
|
+
dispose(): Promise<void>;
|
|
48
48
|
private createEventStream;
|
|
49
49
|
}
|
|
@@ -2,7 +2,6 @@ import assert from 'node:assert';
|
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
3
|
import { PassThrough, Readable } from 'node:stream';
|
|
4
4
|
import { isAbortError } from '@nmtjs/common';
|
|
5
|
-
import { subscriptionAdapter } from '../injectables.js';
|
|
6
5
|
export class SubscriptionManager {
|
|
7
6
|
options;
|
|
8
7
|
subscriptions = new Map();
|
|
@@ -11,11 +10,7 @@ export class SubscriptionManager {
|
|
|
11
10
|
this.options = options;
|
|
12
11
|
this.logger = options.logger.child({ component: SubscriptionManager.name });
|
|
13
12
|
}
|
|
14
|
-
get adapter() {
|
|
15
|
-
return this.options.container.resolve(subscriptionAdapter);
|
|
16
|
-
}
|
|
17
13
|
subscribe = async (subscription, events, options, signal) => {
|
|
18
|
-
const adapter = await this.adapter;
|
|
19
14
|
const eventKeys = Object.keys(events).length === 0
|
|
20
15
|
? Object.keys(subscription.events)
|
|
21
16
|
: Object.keys(events);
|
|
@@ -33,7 +28,7 @@ export class SubscriptionManager {
|
|
|
33
28
|
}, 'Reusing pubsub channel stream');
|
|
34
29
|
}
|
|
35
30
|
else {
|
|
36
|
-
const iterable = adapter.subscribe(channel, signal);
|
|
31
|
+
const iterable = this.options.adapter.subscribe(channel, signal);
|
|
37
32
|
const stream = this.createEventStream(iterable);
|
|
38
33
|
stream.on('close', () => {
|
|
39
34
|
this.subscriptions.delete(channel);
|
|
@@ -69,12 +64,11 @@ export class SubscriptionManager {
|
|
|
69
64
|
return mergedStream;
|
|
70
65
|
};
|
|
71
66
|
publish = async (event, options, data) => {
|
|
72
|
-
const adapter = await this.adapter;
|
|
73
67
|
const channel = getChannelName(event, options);
|
|
74
68
|
this.logger.trace({ channel, event: event.name }, 'Publishing pubsub event');
|
|
75
69
|
try {
|
|
76
70
|
const payload = event.payload.encode(data);
|
|
77
|
-
const published = await adapter.publish(channel, payload);
|
|
71
|
+
const published = await this.options.adapter.publish(channel, payload);
|
|
78
72
|
if (published) {
|
|
79
73
|
this.logger.trace({ channel, event: event.name }, 'Published pubsub event');
|
|
80
74
|
}
|
|
@@ -88,6 +82,12 @@ export class SubscriptionManager {
|
|
|
88
82
|
throw error;
|
|
89
83
|
}
|
|
90
84
|
};
|
|
85
|
+
async dispose() {
|
|
86
|
+
for (const { stream } of this.subscriptions.values()) {
|
|
87
|
+
stream.destroy();
|
|
88
|
+
}
|
|
89
|
+
this.subscriptions.clear();
|
|
90
|
+
}
|
|
91
91
|
createEventStream(iterable) {
|
|
92
92
|
const { subscriptions } = this;
|
|
93
93
|
const logger = this.logger;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/runtime/subscription/manager.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AASnD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/runtime/subscription/manager.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,QAAQ,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AE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAA;QACxC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE;YACtB,KAAK,EAAE,CAAA;YACP,IAAI,KAAK,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC;gBAC7B,WAAW,CAAC,GAAG,EAAE,CAAA;YACnB,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,OAAO,WAAW,CAAA;AACpB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import EventEmitter, { on } from 'node:events';
|
|
2
2
|
import { isAbortError } from '@nmtjs/common';
|
|
3
|
-
import { createFactoryInjectable } from '@nmtjs/core';
|
|
3
|
+
import { CoreInjectables, createFactoryInjectable, provision, } from '@nmtjs/core';
|
|
4
4
|
import { storeConfig, subscriptionAdapter } from '../injectables.js';
|
|
5
5
|
import { createStoreClient } from '../store/index.js';
|
|
6
6
|
export class RedisSubscriptionAdapter {
|
|
@@ -97,24 +97,23 @@ export class RedisSubscriptionAdapter {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
export const RedisSubscriptionAdapterPlugin = () => {
|
|
100
|
+
const adapterFactory = createFactoryInjectable({
|
|
101
|
+
dependencies: { config: storeConfig, logger: CoreInjectables.logger },
|
|
102
|
+
factory: async ({ config, logger }) => {
|
|
103
|
+
const connection = await createStoreClient(config);
|
|
104
|
+
const adapter = new RedisSubscriptionAdapter(connection, logger);
|
|
105
|
+
await adapter.initialize();
|
|
106
|
+
return { adapter, connection };
|
|
107
|
+
},
|
|
108
|
+
pick: ({ adapter }) => adapter,
|
|
109
|
+
dispose: async ({ adapter, connection }) => {
|
|
110
|
+
await adapter.dispose();
|
|
111
|
+
await connection.quit();
|
|
112
|
+
},
|
|
113
|
+
}, 'RedisSubscriptionAdapter');
|
|
100
114
|
return {
|
|
101
115
|
name: 'redis-subscription-adapter',
|
|
102
|
-
|
|
103
|
-
'lifecycle:beforeInitialize': async (ctx) => {
|
|
104
|
-
const adapter = await ctx.container.resolve(createFactoryInjectable({
|
|
105
|
-
dependencies: { config: storeConfig },
|
|
106
|
-
factory: async ({ config }) => {
|
|
107
|
-
const connection = await createStoreClient(config);
|
|
108
|
-
const adapter = new RedisSubscriptionAdapter(connection, ctx.logger);
|
|
109
|
-
await adapter.initialize();
|
|
110
|
-
return { adapter, connection };
|
|
111
|
-
},
|
|
112
|
-
pick: ({ adapter }) => adapter,
|
|
113
|
-
dispose: ({ connection }) => connection.quit(),
|
|
114
|
-
}));
|
|
115
|
-
ctx.container.provide(subscriptionAdapter, adapter);
|
|
116
|
-
},
|
|
117
|
-
},
|
|
116
|
+
injections: [provision(subscriptionAdapter, adapterFactory)],
|
|
118
117
|
};
|
|
119
118
|
};
|
|
120
119
|
//# sourceMappingURL=redis.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis.js","sourceRoot":"","sources":["../../../src/runtime/subscription/redis.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,EAAE,EAAE,EAAE,EAAE,MAAM,aAAa,CAAA;AAI9C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,
|
|
1
|
+
{"version":3,"file":"redis.js","sourceRoot":"","sources":["../../../src/runtime/subscription/redis.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,EAAE,EAAE,EAAE,EAAE,MAAM,aAAa,CAAA;AAI9C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,SAAS,GACV,MAAM,aAAa,CAAA;AAOpB,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAErD,MAAM,OAAO,wBAAwB;IAOd,MAAM;IANR,MAAM,GAAG,IAAI,YAAY,EAAE,CAAA;IAC3B,SAAS,GAAG,IAAI,GAAG,E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ClC,UAAU,EAAE,CAAC,SAAS,CAAC,mBAAmB,EAAE,cAAc,CAAC,CAAC;KAC7D,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -3,11 +3,9 @@ import type { BaseRuntimeOptions } from '../runtime.ts';
|
|
|
3
3
|
import type { ServerConfig } from '../server/config.ts';
|
|
4
4
|
import { JobManager } from '../jobs/manager.ts';
|
|
5
5
|
import { BaseRuntime } from '../runtime.ts';
|
|
6
|
-
import { SubscriptionManager } from '../subscription/manager.ts';
|
|
7
6
|
export declare abstract class BaseWorkerRuntime extends BaseRuntime {
|
|
8
7
|
readonly config: ServerConfig;
|
|
9
8
|
readonly workerType: WorkerType;
|
|
10
|
-
subscriptionManager: SubscriptionManager;
|
|
11
9
|
jobManager?: JobManager;
|
|
12
10
|
constructor(config: ServerConfig, options: BaseRuntimeOptions, workerType: WorkerType);
|
|
13
11
|
initialize(): Promise<void>;
|
|
@@ -2,20 +2,14 @@ import { CoreInjectables, provision } from '@nmtjs/core';
|
|
|
2
2
|
import * as injectables from '../injectables.js';
|
|
3
3
|
import { JobManager } from '../jobs/manager.js';
|
|
4
4
|
import { BaseRuntime } from '../runtime.js';
|
|
5
|
-
import { SubscriptionManager } from '../subscription/manager.js';
|
|
6
5
|
export class BaseWorkerRuntime extends BaseRuntime {
|
|
7
6
|
config;
|
|
8
7
|
workerType;
|
|
9
|
-
subscriptionManager;
|
|
10
8
|
jobManager;
|
|
11
9
|
constructor(config, options, workerType) {
|
|
12
10
|
super(options);
|
|
13
11
|
this.config = config;
|
|
14
12
|
this.workerType = workerType;
|
|
15
|
-
this.subscriptionManager = new SubscriptionManager({
|
|
16
|
-
logger: this.logger,
|
|
17
|
-
container: this.container,
|
|
18
|
-
});
|
|
19
13
|
if (this.config.store) {
|
|
20
14
|
this.jobManager = new JobManager(this.config.store, this.config.jobs ? Array.from(this.config.jobs.jobs.values()) : []);
|
|
21
15
|
}
|
|
@@ -24,12 +18,13 @@ export class BaseWorkerRuntime extends BaseRuntime {
|
|
|
24
18
|
const injections = [
|
|
25
19
|
provision(CoreInjectables.logger, this.logger),
|
|
26
20
|
provision(injectables.workerType, this.workerType),
|
|
27
|
-
provision(injectables.publish, this.subscriptionManager.publish.bind(this.subscriptionManager)),
|
|
28
|
-
provision(injectables.subscribe, this.subscriptionManager.subscribe.bind(this.subscriptionManager)),
|
|
29
21
|
];
|
|
30
22
|
if (this.config.store) {
|
|
31
23
|
injections.push(provision(injectables.storeConfig, this.config.store));
|
|
32
24
|
}
|
|
25
|
+
if (this.config.subscription) {
|
|
26
|
+
injections.push(provision(injectables.subscriptionAdapter, this.config.subscription.adapter));
|
|
27
|
+
}
|
|
33
28
|
if (this.jobManager) {
|
|
34
29
|
injections.push(provision(injectables.jobManager, this.jobManager.publicInstance));
|
|
35
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../src/runtime/workers/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAKxD,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../src/runtime/workers/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAKxD,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,MAAM,OAAgB,iBAAkB,SAAQ,WAAW;IAI9C,MAAM;IAEN,UAAU;IALrB,UAAU,CAAa;IAEvB,YACW,MAAoB,EAC7B,OAA2B,EAClB,UAAsB;QAE/B,KAAK,CAAC,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K,CAAC,WAAW;QACzB,MAAM,IAAI,CAAC,UAAU,EAAE,UAAU,EAAE,CAAA;IACrC,CAAC;IAES,KAAK,CAAC,QAAQ;QACtB,MAAM,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,CAAA;IACpC,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -42,17 +42,17 @@
|
|
|
42
42
|
"iovalkey": "^0.3.3",
|
|
43
43
|
"oxc-resolver": "11.19.1",
|
|
44
44
|
"vite": "8.0.14",
|
|
45
|
-
"@nmtjs/
|
|
46
|
-
"@nmtjs/contract": "0.16.
|
|
47
|
-
"@nmtjs/
|
|
48
|
-
"@nmtjs/core": "0.16.
|
|
49
|
-
"@nmtjs/gateway": "0.16.
|
|
50
|
-
"@nmtjs/
|
|
51
|
-
"@nmtjs/http-transport": "0.16.
|
|
52
|
-
"@nmtjs/
|
|
53
|
-
"@nmtjs/type": "0.16.
|
|
54
|
-
"@nmtjs/msgpack-format": "0.16.
|
|
55
|
-
"@nmtjs/ws-transport": "0.16.
|
|
45
|
+
"@nmtjs/application": "0.16.1",
|
|
46
|
+
"@nmtjs/contract": "0.16.1",
|
|
47
|
+
"@nmtjs/common": "0.16.1",
|
|
48
|
+
"@nmtjs/core": "0.16.1",
|
|
49
|
+
"@nmtjs/gateway": "0.16.1",
|
|
50
|
+
"@nmtjs/protocol": "0.16.1",
|
|
51
|
+
"@nmtjs/http-transport": "0.16.1",
|
|
52
|
+
"@nmtjs/json-format": "0.16.1",
|
|
53
|
+
"@nmtjs/type": "0.16.1",
|
|
54
|
+
"@nmtjs/msgpack-format": "0.16.1",
|
|
55
|
+
"@nmtjs/ws-transport": "0.16.1"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@nmtjs/proxy": "1.0.0-beta.6",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"LICENSE.md",
|
|
76
76
|
"README.md"
|
|
77
77
|
],
|
|
78
|
-
"version": "0.16.
|
|
78
|
+
"version": "0.16.1",
|
|
79
79
|
"repository": {
|
|
80
80
|
"type": "git",
|
|
81
81
|
"url": "https://github.com/neematajs/neemata"
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CoreInjectables,
|
|
3
|
+
createFactoryInjectable,
|
|
4
|
+
createLazyInjectable,
|
|
5
|
+
Scope,
|
|
6
|
+
} from '@nmtjs/core'
|
|
2
7
|
|
|
3
8
|
import type { WorkerType } from './enums.ts'
|
|
4
9
|
import type { JobManagerInstance } from './jobs/manager.ts'
|
|
@@ -9,6 +14,7 @@ import type {
|
|
|
9
14
|
SubscribeFn,
|
|
10
15
|
SubscriptionAdapterType,
|
|
11
16
|
} from './subscription/manager.ts'
|
|
17
|
+
import { SubscriptionManager } from './subscription/manager.ts'
|
|
12
18
|
|
|
13
19
|
export const subscriptionAdapter =
|
|
14
20
|
createLazyInjectable<SubscriptionAdapterType>(
|
|
@@ -16,10 +22,32 @@ export const subscriptionAdapter =
|
|
|
16
22
|
'SubscriptionAdapter',
|
|
17
23
|
)
|
|
18
24
|
|
|
19
|
-
export const
|
|
25
|
+
export const subscriptionManager = createFactoryInjectable(
|
|
26
|
+
{
|
|
27
|
+
dependencies: {
|
|
28
|
+
adapter: subscriptionAdapter,
|
|
29
|
+
logger: CoreInjectables.logger,
|
|
30
|
+
},
|
|
31
|
+
factory: ({ adapter, logger }) =>
|
|
32
|
+
new SubscriptionManager({ logger, adapter }),
|
|
33
|
+
dispose: (manager) => manager.dispose(),
|
|
34
|
+
},
|
|
35
|
+
'SubscriptionManager',
|
|
36
|
+
)
|
|
20
37
|
|
|
21
|
-
export const
|
|
22
|
-
|
|
38
|
+
export const publish = createFactoryInjectable(
|
|
39
|
+
{
|
|
40
|
+
dependencies: { manager: subscriptionManager },
|
|
41
|
+
factory: ({ manager }): PublishFn => manager.publish.bind(manager),
|
|
42
|
+
},
|
|
43
|
+
'Publish',
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
export const subscribe = createFactoryInjectable(
|
|
47
|
+
{
|
|
48
|
+
dependencies: { manager: subscriptionManager },
|
|
49
|
+
factory: ({ manager }): SubscribeFn => manager.subscribe.bind(manager),
|
|
50
|
+
},
|
|
23
51
|
'Subscribe',
|
|
24
52
|
)
|
|
25
53
|
|
|
@@ -60,6 +88,7 @@ export const currentJobInfo = createLazyInjectable<JobExecutionContext>(
|
|
|
60
88
|
|
|
61
89
|
export const RuntimeInjectables = {
|
|
62
90
|
subscriptionAdapter,
|
|
91
|
+
subscriptionManager,
|
|
63
92
|
publish,
|
|
64
93
|
subscribe,
|
|
65
94
|
jobManager,
|
|
@@ -7,12 +7,10 @@ import type {
|
|
|
7
7
|
TAnyEventContract,
|
|
8
8
|
TAnySubscriptionContract,
|
|
9
9
|
} from '@nmtjs/contract'
|
|
10
|
-
import type {
|
|
10
|
+
import type { Logger } from '@nmtjs/core'
|
|
11
11
|
import type { t } from '@nmtjs/type'
|
|
12
12
|
import { isAbortError } from '@nmtjs/common'
|
|
13
13
|
|
|
14
|
-
import { subscriptionAdapter } from '../injectables.ts'
|
|
15
|
-
|
|
16
14
|
export type SubscriptionAdapterEvent = { channel: string; payload: any }
|
|
17
15
|
|
|
18
16
|
export interface SubscriptionAdapterType {
|
|
@@ -74,7 +72,7 @@ export type PublishFn = <
|
|
|
74
72
|
|
|
75
73
|
export type SubscriptionManagerOptions = {
|
|
76
74
|
logger: Logger
|
|
77
|
-
|
|
75
|
+
adapter: SubscriptionAdapterType
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
export class SubscriptionManager {
|
|
@@ -85,13 +83,7 @@ export class SubscriptionManager {
|
|
|
85
83
|
this.logger = options.logger.child({ component: SubscriptionManager.name })
|
|
86
84
|
}
|
|
87
85
|
|
|
88
|
-
protected get adapter() {
|
|
89
|
-
return this.options.container.resolve(subscriptionAdapter)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
86
|
subscribe: SubscribeFn = async (subscription, events, options, signal) => {
|
|
93
|
-
const adapter = await this.adapter
|
|
94
|
-
|
|
95
87
|
const eventKeys =
|
|
96
88
|
Object.keys(events).length === 0
|
|
97
89
|
? Object.keys(subscription.events)
|
|
@@ -118,7 +110,7 @@ export class SubscriptionManager {
|
|
|
118
110
|
'Reusing pubsub channel stream',
|
|
119
111
|
)
|
|
120
112
|
} else {
|
|
121
|
-
const iterable = adapter.subscribe(channel, signal)
|
|
113
|
+
const iterable = this.options.adapter.subscribe(channel, signal)
|
|
122
114
|
const stream = this.createEventStream(iterable)
|
|
123
115
|
stream.on('close', () => {
|
|
124
116
|
this.subscriptions.delete(channel)
|
|
@@ -173,15 +165,13 @@ export class SubscriptionManager {
|
|
|
173
165
|
}
|
|
174
166
|
|
|
175
167
|
publish: PublishFn = async (event, options, data) => {
|
|
176
|
-
const adapter = await this.adapter
|
|
177
|
-
|
|
178
168
|
const channel = getChannelName(event, options)
|
|
179
169
|
|
|
180
170
|
this.logger.trace({ channel, event: event.name }, 'Publishing pubsub event')
|
|
181
171
|
|
|
182
172
|
try {
|
|
183
173
|
const payload = event.payload.encode(data)
|
|
184
|
-
const published = await adapter.publish(channel, payload)
|
|
174
|
+
const published = await this.options.adapter.publish(channel, payload)
|
|
185
175
|
|
|
186
176
|
if (published) {
|
|
187
177
|
this.logger.trace(
|
|
@@ -205,6 +195,13 @@ export class SubscriptionManager {
|
|
|
205
195
|
}
|
|
206
196
|
}
|
|
207
197
|
|
|
198
|
+
async dispose(): Promise<void> {
|
|
199
|
+
for (const { stream } of this.subscriptions.values()) {
|
|
200
|
+
stream.destroy()
|
|
201
|
+
}
|
|
202
|
+
this.subscriptions.clear()
|
|
203
|
+
}
|
|
204
|
+
|
|
208
205
|
private createEventStream(
|
|
209
206
|
iterable: AsyncGenerator<SubscriptionAdapterEvent>,
|
|
210
207
|
): Readable {
|
|
@@ -3,7 +3,11 @@ import EventEmitter, { on } from 'node:events'
|
|
|
3
3
|
import type { RuntimePlugin } from '@nmtjs/application'
|
|
4
4
|
import type { Logger } from '@nmtjs/core'
|
|
5
5
|
import { isAbortError } from '@nmtjs/common'
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
CoreInjectables,
|
|
8
|
+
createFactoryInjectable,
|
|
9
|
+
provision,
|
|
10
|
+
} from '@nmtjs/core'
|
|
7
11
|
|
|
8
12
|
import type { Store } from '../types.ts'
|
|
9
13
|
import type {
|
|
@@ -130,28 +134,26 @@ export class RedisSubscriptionAdapter implements SubscriptionAdapterType {
|
|
|
130
134
|
}
|
|
131
135
|
|
|
132
136
|
export const RedisSubscriptionAdapterPlugin = (): RuntimePlugin => {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
await adapter.initialize()
|
|
147
|
-
return { adapter, connection }
|
|
148
|
-
},
|
|
149
|
-
pick: ({ adapter }) => adapter,
|
|
150
|
-
dispose: ({ connection }) => connection.quit(),
|
|
151
|
-
}),
|
|
152
|
-
)
|
|
153
|
-
ctx.container.provide(subscriptionAdapter, adapter)
|
|
137
|
+
const adapterFactory = createFactoryInjectable(
|
|
138
|
+
{
|
|
139
|
+
dependencies: { config: storeConfig, logger: CoreInjectables.logger },
|
|
140
|
+
factory: async ({ config, logger }) => {
|
|
141
|
+
const connection = await createStoreClient(config)
|
|
142
|
+
const adapter = new RedisSubscriptionAdapter(connection, logger)
|
|
143
|
+
await adapter.initialize()
|
|
144
|
+
return { adapter, connection }
|
|
145
|
+
},
|
|
146
|
+
pick: ({ adapter }) => adapter,
|
|
147
|
+
dispose: async ({ adapter, connection }) => {
|
|
148
|
+
await adapter.dispose()
|
|
149
|
+
await connection.quit()
|
|
154
150
|
},
|
|
155
151
|
},
|
|
152
|
+
'RedisSubscriptionAdapter',
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
name: 'redis-subscription-adapter',
|
|
157
|
+
injections: [provision(subscriptionAdapter, adapterFactory)],
|
|
156
158
|
}
|
|
157
159
|
}
|
|
@@ -7,10 +7,8 @@ import type { ServerConfig } from '../server/config.ts'
|
|
|
7
7
|
import * as injectables from '../injectables.ts'
|
|
8
8
|
import { JobManager } from '../jobs/manager.ts'
|
|
9
9
|
import { BaseRuntime } from '../runtime.ts'
|
|
10
|
-
import { SubscriptionManager } from '../subscription/manager.ts'
|
|
11
10
|
|
|
12
11
|
export abstract class BaseWorkerRuntime extends BaseRuntime {
|
|
13
|
-
subscriptionManager: SubscriptionManager
|
|
14
12
|
jobManager?: JobManager
|
|
15
13
|
|
|
16
14
|
constructor(
|
|
@@ -20,11 +18,6 @@ export abstract class BaseWorkerRuntime extends BaseRuntime {
|
|
|
20
18
|
) {
|
|
21
19
|
super(options)
|
|
22
20
|
|
|
23
|
-
this.subscriptionManager = new SubscriptionManager({
|
|
24
|
-
logger: this.logger,
|
|
25
|
-
container: this.container,
|
|
26
|
-
})
|
|
27
|
-
|
|
28
21
|
if (this.config.store) {
|
|
29
22
|
this.jobManager = new JobManager(
|
|
30
23
|
this.config.store,
|
|
@@ -37,20 +30,21 @@ export abstract class BaseWorkerRuntime extends BaseRuntime {
|
|
|
37
30
|
const injections: Provision[] = [
|
|
38
31
|
provision(CoreInjectables.logger, this.logger),
|
|
39
32
|
provision(injectables.workerType, this.workerType),
|
|
40
|
-
provision(
|
|
41
|
-
injectables.publish,
|
|
42
|
-
this.subscriptionManager.publish.bind(this.subscriptionManager),
|
|
43
|
-
),
|
|
44
|
-
provision(
|
|
45
|
-
injectables.subscribe,
|
|
46
|
-
this.subscriptionManager.subscribe.bind(this.subscriptionManager),
|
|
47
|
-
),
|
|
48
33
|
]
|
|
49
34
|
|
|
50
35
|
if (this.config.store) {
|
|
51
36
|
injections.push(provision(injectables.storeConfig, this.config.store))
|
|
52
37
|
}
|
|
53
38
|
|
|
39
|
+
if (this.config.subscription) {
|
|
40
|
+
injections.push(
|
|
41
|
+
provision(
|
|
42
|
+
injectables.subscriptionAdapter,
|
|
43
|
+
this.config.subscription.adapter,
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
54
48
|
if (this.jobManager) {
|
|
55
49
|
injections.push(
|
|
56
50
|
provision(injectables.jobManager, this.jobManager.publicInstance),
|