nmtjs 0.15.0-beta.2 → 0.15.0-beta.21
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/cli.d.ts +2 -0
- package/dist/cli.js +3 -2
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +51 -0
- package/dist/config.js +1 -0
- package/dist/config.js.map +1 -0
- package/dist/entrypoints/cli.d.ts +1 -0
- package/dist/entrypoints/cli.js +1 -0
- package/dist/entrypoints/cli.js.map +1 -0
- package/dist/entrypoints/main.d.ts +5 -0
- package/dist/entrypoints/main.js +83 -15
- package/dist/entrypoints/main.js.map +1 -0
- package/dist/entrypoints/thread.d.ts +14 -0
- package/dist/entrypoints/thread.js +130 -24
- package/dist/entrypoints/thread.js.map +1 -0
- package/dist/entrypoints/worker.d.ts +3 -0
- package/dist/entrypoints/worker.js +4 -3
- package/dist/entrypoints/worker.js.map +1 -0
- package/dist/index.d.ts +69 -0
- package/dist/{_exports/index.js → index.js} +9 -5
- package/dist/index.js.map +1 -0
- package/dist/resolver.d.ts +2 -0
- package/dist/resolver.js +1 -0
- package/dist/resolver.js.map +1 -0
- package/dist/runtime/application/api/api.d.ts +49 -0
- package/dist/runtime/application/api/api.js +193 -0
- package/dist/runtime/application/api/api.js.map +1 -0
- package/dist/runtime/application/api/constants.d.ts +14 -0
- package/dist/runtime/application/api/constants.js +8 -0
- package/dist/runtime/application/api/constants.js.map +1 -0
- package/dist/runtime/application/api/filters.d.ts +14 -0
- package/dist/runtime/application/api/filters.js +11 -0
- package/dist/runtime/application/api/filters.js.map +1 -0
- package/dist/runtime/application/api/guards.d.ts +13 -0
- package/dist/runtime/application/api/guards.js +8 -0
- package/dist/runtime/application/api/guards.js.map +1 -0
- package/dist/runtime/application/api/index.d.ts +8 -0
- package/dist/runtime/application/api/index.js +9 -0
- package/dist/runtime/application/api/index.js.map +1 -0
- package/dist/runtime/application/api/middlewares.d.ts +14 -0
- package/dist/runtime/application/api/middlewares.js +12 -0
- package/dist/runtime/application/api/middlewares.js.map +1 -0
- package/dist/runtime/application/api/procedure.d.ts +67 -0
- package/dist/runtime/application/api/procedure.js +50 -0
- package/dist/runtime/application/api/procedure.js.map +1 -0
- package/dist/runtime/application/api/router.d.ts +71 -0
- package/dist/runtime/application/api/router.js +51 -0
- package/dist/runtime/application/api/router.js.map +1 -0
- package/dist/runtime/application/api/types.d.ts +32 -0
- package/dist/runtime/application/api/types.js +2 -0
- package/dist/runtime/application/api/types.js.map +1 -0
- package/dist/runtime/application/config.d.ts +26 -0
- package/dist/runtime/application/config.js +21 -0
- package/dist/runtime/application/config.js.map +1 -0
- package/dist/runtime/application/constants.d.ts +2 -0
- package/dist/runtime/application/constants.js +2 -0
- package/dist/runtime/application/constants.js.map +1 -0
- package/dist/runtime/application/hook.d.ts +19 -0
- package/dist/runtime/application/hook.js +11 -0
- package/dist/runtime/application/hook.js.map +1 -0
- package/dist/runtime/application/hooks.d.ts +3 -0
- package/dist/runtime/application/hooks.js +4 -0
- package/dist/runtime/application/hooks.js.map +1 -0
- package/dist/runtime/application/index.d.ts +5 -0
- package/dist/runtime/application/index.js +6 -0
- package/dist/runtime/application/index.js.map +1 -0
- package/dist/runtime/constants.d.ts +8 -0
- package/dist/runtime/constants.js +5 -0
- package/dist/runtime/constants.js.map +1 -0
- package/dist/runtime/core/hooks.d.ts +4 -0
- package/dist/runtime/core/hooks.js +4 -0
- package/dist/runtime/core/hooks.js.map +1 -0
- package/dist/runtime/core/plugin.d.ts +8 -0
- package/dist/runtime/core/plugin.js +4 -0
- package/dist/runtime/core/plugin.js.map +1 -0
- package/dist/runtime/core/runtime.d.ts +27 -0
- package/dist/runtime/core/runtime.js +81 -0
- package/dist/runtime/core/runtime.js.map +1 -0
- package/dist/runtime/enums.d.ts +21 -0
- package/dist/runtime/enums.js +26 -0
- package/dist/runtime/enums.js.map +1 -0
- package/dist/runtime/index.d.ts +21 -0
- package/dist/runtime/index.js +22 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/injectables.d.ts +23 -0
- package/dist/runtime/injectables.js +20 -0
- package/dist/runtime/injectables.js.map +1 -0
- package/dist/runtime/jobs/job.d.ts +132 -0
- package/dist/runtime/jobs/job.js +68 -0
- package/dist/runtime/jobs/job.js.map +1 -0
- package/dist/runtime/jobs/manager.d.ts +113 -0
- package/dist/runtime/jobs/manager.js +210 -0
- package/dist/runtime/jobs/manager.js.map +1 -0
- package/dist/runtime/jobs/router.d.ts +266 -0
- package/dist/runtime/jobs/router.js +432 -0
- package/dist/runtime/jobs/router.js.map +1 -0
- package/dist/runtime/jobs/runner.d.ts +64 -0
- package/dist/runtime/jobs/runner.js +256 -0
- package/dist/runtime/jobs/runner.js.map +1 -0
- package/dist/runtime/jobs/step.d.ts +23 -0
- package/dist/runtime/jobs/step.js +18 -0
- package/dist/runtime/jobs/step.js.map +1 -0
- package/dist/runtime/jobs/ui.d.ts +3 -0
- package/dist/runtime/jobs/ui.js +17 -0
- package/dist/runtime/jobs/ui.js.map +1 -0
- package/dist/runtime/pubsub/manager.d.ts +48 -0
- package/dist/runtime/pubsub/manager.js +119 -0
- package/dist/runtime/pubsub/manager.js.map +1 -0
- package/dist/runtime/pubsub/redis.d.ts +16 -0
- package/dist/runtime/pubsub/redis.js +98 -0
- package/dist/runtime/pubsub/redis.js.map +1 -0
- package/dist/runtime/scheduler/index.d.ts +22 -0
- package/dist/runtime/scheduler/index.js +20 -0
- package/dist/runtime/scheduler/index.js.map +1 -0
- package/dist/runtime/server/applications.d.ts +52 -0
- package/dist/runtime/server/applications.js +133 -0
- package/dist/runtime/server/applications.js.map +1 -0
- package/dist/runtime/server/config.d.ts +121 -0
- package/dist/runtime/server/config.js +33 -0
- package/dist/runtime/server/config.js.map +1 -0
- package/dist/runtime/server/jobs.d.ts +41 -0
- package/dist/runtime/server/jobs.js +181 -0
- package/dist/runtime/server/jobs.js.map +1 -0
- package/dist/runtime/server/pool.d.ts +54 -0
- package/dist/runtime/server/pool.js +194 -0
- package/dist/runtime/server/pool.js.map +1 -0
- package/dist/runtime/server/proxy.d.ts +21 -0
- package/dist/runtime/server/proxy.js +79 -0
- package/dist/runtime/server/proxy.js.map +1 -0
- package/dist/runtime/server/server.d.ts +53 -0
- package/dist/runtime/server/server.js +90 -0
- package/dist/runtime/server/server.js.map +1 -0
- package/dist/runtime/store/index.d.ts +3 -0
- package/dist/runtime/store/index.js +23 -0
- package/dist/runtime/store/index.js.map +1 -0
- package/dist/runtime/types.d.ts +103 -0
- package/dist/runtime/types.js +2 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/runtime/workers/application.d.ts +47 -0
- package/dist/runtime/workers/application.js +162 -0
- package/dist/runtime/workers/application.js.map +1 -0
- package/dist/runtime/workers/base.d.ts +16 -0
- package/dist/runtime/workers/base.js +46 -0
- package/dist/runtime/workers/base.js.map +1 -0
- package/dist/runtime/workers/cli.d.ts +1 -0
- package/dist/runtime/workers/cli.js +2 -0
- package/dist/runtime/workers/cli.js.map +1 -0
- package/dist/runtime/workers/job.d.ts +20 -0
- package/dist/runtime/workers/job.js +172 -0
- package/dist/runtime/workers/job.js.map +1 -0
- package/dist/typings.d.ts +5 -0
- package/dist/typings.js +4 -3
- package/dist/typings.js.map +1 -0
- package/dist/vite/builder.d.ts +5 -0
- package/dist/vite/builder.js +5 -1
- package/dist/vite/builder.js.map +1 -0
- package/dist/vite/config.d.ts +28 -0
- package/dist/vite/config.js +1 -0
- package/dist/vite/config.js.map +1 -0
- package/dist/vite/plugins.d.ts +2 -0
- package/dist/vite/plugins.js +1 -0
- package/dist/vite/plugins.js.map +1 -0
- package/dist/vite/runners/worker.d.ts +4 -0
- package/dist/vite/runners/worker.js +1 -0
- package/dist/vite/runners/worker.js.map +1 -0
- package/dist/vite/server.d.ts +3 -0
- package/dist/vite/server.js +6 -1
- package/dist/vite/server.js.map +1 -0
- package/dist/vite/servers/main.d.ts +8 -0
- package/dist/vite/servers/main.js +1 -0
- package/dist/vite/servers/main.js.map +1 -0
- package/dist/vite/servers/worker.d.ts +11 -0
- package/dist/vite/servers/worker.js +28 -0
- package/dist/vite/servers/worker.js.map +1 -0
- package/package.json +31 -18
- package/src/cli.ts +144 -0
- package/src/config.ts +64 -0
- package/src/entrypoints/cli.ts +13 -0
- package/src/entrypoints/main.ts +200 -0
- package/src/entrypoints/thread.ts +184 -0
- package/src/entrypoints/worker.ts +48 -0
- package/src/index.ts +82 -0
- package/src/resolver.ts +16 -0
- package/src/runtime/application/api/api.ts +265 -0
- package/src/runtime/application/api/constants.ts +22 -0
- package/src/runtime/application/api/filters.ts +39 -0
- package/src/runtime/application/api/guards.ts +29 -0
- package/src/runtime/application/api/index.ts +8 -0
- package/src/runtime/application/api/middlewares.ts +37 -0
- package/src/runtime/application/api/procedure.ts +229 -0
- package/src/runtime/application/api/router.ts +193 -0
- package/src/runtime/application/api/types.ts +124 -0
- package/src/runtime/application/config.ts +69 -0
- package/src/runtime/application/constants.ts +4 -0
- package/src/runtime/application/hook.ts +51 -0
- package/src/runtime/application/hooks.ts +3 -0
- package/src/runtime/application/index.ts +5 -0
- package/src/runtime/constants.ts +13 -0
- package/src/runtime/core/hooks.ts +5 -0
- package/src/runtime/core/plugin.ts +13 -0
- package/src/runtime/core/runtime.ts +109 -0
- package/src/runtime/enums.ts +24 -0
- package/src/runtime/index.ts +21 -0
- package/src/runtime/injectables.ts +61 -0
- package/src/runtime/jobs/job.ts +370 -0
- package/src/runtime/jobs/manager.ts +348 -0
- package/src/runtime/jobs/router.ts +896 -0
- package/src/runtime/jobs/runner.ts +320 -0
- package/src/runtime/jobs/step.ts +66 -0
- package/src/runtime/jobs/ui.ts +21 -0
- package/src/runtime/pubsub/manager.ts +211 -0
- package/src/runtime/pubsub/redis.ts +108 -0
- package/src/runtime/scheduler/index.ts +39 -0
- package/src/runtime/server/applications.ts +210 -0
- package/src/runtime/server/config.ts +158 -0
- package/src/runtime/server/jobs.ts +250 -0
- package/src/runtime/server/pool.ts +260 -0
- package/src/runtime/server/proxy.ts +118 -0
- package/src/runtime/server/server.ts +155 -0
- package/src/runtime/store/index.ts +30 -0
- package/src/runtime/types.ts +93 -0
- package/src/runtime/workers/application.ts +209 -0
- package/src/runtime/workers/base.ts +68 -0
- package/src/runtime/workers/cli.ts +0 -0
- package/src/runtime/workers/job.ts +153 -0
- package/src/typings.ts +30 -0
- package/src/vite/builder.ts +122 -0
- package/src/vite/config.ts +45 -0
- package/src/vite/plugins.ts +26 -0
- package/src/vite/runners/worker.ts +57 -0
- package/src/vite/server.ts +39 -0
- package/src/vite/servers/main.ts +34 -0
- package/src/vite/servers/worker.ts +143 -0
- package/dist/_exports/application.js +0 -1
- package/dist/_exports/common.js +0 -1
- package/dist/_exports/contract.js +0 -2
- package/dist/_exports/core.js +0 -1
- package/dist/_exports/gateway.js +0 -1
- package/dist/_exports/http-transport/bun.js +0 -1
- package/dist/_exports/http-transport/deno.js +0 -1
- package/dist/_exports/http-transport/node.js +0 -1
- package/dist/_exports/http-transport.js +0 -1
- package/dist/_exports/json-format.js +0 -1
- package/dist/_exports/protocol/client.js +0 -1
- package/dist/_exports/protocol/server.js +0 -1
- package/dist/_exports/protocol.js +0 -1
- package/dist/_exports/runtime/types.js +0 -1
- package/dist/_exports/runtime.js +0 -1
- package/dist/_exports/type.js +0 -2
- package/dist/_exports/ws-transport/bun.js +0 -1
- package/dist/_exports/ws-transport/deno.js +0 -1
- package/dist/_exports/ws-transport/node.js +0 -1
- package/dist/_exports/ws-transport.js +0 -1
- package/dist/command.js +0 -30
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Injection } from '@nmtjs/core'
|
|
2
|
+
|
|
3
|
+
import type { LifecycleHooks } from './hooks.ts'
|
|
4
|
+
|
|
5
|
+
export interface RuntimePlugin {
|
|
6
|
+
name: string
|
|
7
|
+
hooks?: LifecycleHooks['_']['config']
|
|
8
|
+
injections?: Injection[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createPlugin<T extends RuntimePlugin>(plugin: T): T {
|
|
12
|
+
return plugin
|
|
13
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnyInjectable,
|
|
3
|
+
Dependant,
|
|
4
|
+
Logger,
|
|
5
|
+
LoggingOptions,
|
|
6
|
+
} from '@nmtjs/core'
|
|
7
|
+
import {
|
|
8
|
+
Container,
|
|
9
|
+
createLogger,
|
|
10
|
+
getDepedencencyInjectable,
|
|
11
|
+
Scope,
|
|
12
|
+
} from '@nmtjs/core'
|
|
13
|
+
|
|
14
|
+
import type { RuntimePlugin } from './plugin.ts'
|
|
15
|
+
import { LifecycleHook } from '../enums.ts'
|
|
16
|
+
import { LifecycleHooks } from './hooks.ts'
|
|
17
|
+
|
|
18
|
+
export type BaseRuntimeOptions = {
|
|
19
|
+
logger?: LoggingOptions
|
|
20
|
+
container?: Container
|
|
21
|
+
plugins?: RuntimePlugin[]
|
|
22
|
+
name?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export abstract class BaseRuntime {
|
|
26
|
+
logger: Logger
|
|
27
|
+
container: Container
|
|
28
|
+
lifecycleHooks: LifecycleHooks
|
|
29
|
+
plugins: RuntimePlugin[]
|
|
30
|
+
|
|
31
|
+
constructor(options: BaseRuntimeOptions = {}) {
|
|
32
|
+
this.logger = createLogger(options.logger, options.name || 'Runtime')
|
|
33
|
+
this.container = options.container
|
|
34
|
+
? options.container.fork(Scope.Global)
|
|
35
|
+
: new Container({ logger: this.logger })
|
|
36
|
+
this.lifecycleHooks = new LifecycleHooks()
|
|
37
|
+
this.plugins = options.plugins || []
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected abstract _initialize(): Promise<void>
|
|
41
|
+
protected abstract _dispose(): Promise<void>
|
|
42
|
+
protected abstract _dependents(): Generator<Dependant>
|
|
43
|
+
|
|
44
|
+
async reload(...args: any[]): Promise<void> {
|
|
45
|
+
await this._dispose()
|
|
46
|
+
await this._initialize()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async initialize() {
|
|
50
|
+
this.logger.debug('Initializing a runtime...')
|
|
51
|
+
await this._initializePlugins()
|
|
52
|
+
await this._initializeContainer()
|
|
53
|
+
await this.lifecycleHooks.callHook(LifecycleHook.BeforeInitialize, this)
|
|
54
|
+
await this._initialize()
|
|
55
|
+
await this.lifecycleHooks.callHook(LifecycleHook.AfterInitialize, this)
|
|
56
|
+
this.logger.debug('Runtime initialized')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async dispose() {
|
|
60
|
+
this.logger.debug('Disposing a runtime...')
|
|
61
|
+
await this.lifecycleHooks.callHook(LifecycleHook.BeforeDispose, this)
|
|
62
|
+
await this._dispose()
|
|
63
|
+
await this.lifecycleHooks.callHook(LifecycleHook.AfterDispose, this)
|
|
64
|
+
await this._disposeContainer()
|
|
65
|
+
await this._disposePlugins()
|
|
66
|
+
this.logger.debug('Runtime disposed')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
protected async _initializePlugins() {
|
|
70
|
+
if (!this.plugins?.length) return
|
|
71
|
+
for (const { name, hooks, injections } of this.plugins) {
|
|
72
|
+
this.logger.debug(`Initializing plugin [${name}]...`)
|
|
73
|
+
if (injections) {
|
|
74
|
+
for (const injection of injections) {
|
|
75
|
+
await this.container.provide(injection.token, injection.value)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (hooks) this.lifecycleHooks.addHooks(hooks)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected async _disposePlugins() {
|
|
83
|
+
if (!this.plugins?.length) return
|
|
84
|
+
for (const { name, hooks, injections } of this.plugins) {
|
|
85
|
+
this.logger.debug(`Disposing plugin [${name}]...`)
|
|
86
|
+
if (hooks) this.lifecycleHooks.removeHooks(hooks)
|
|
87
|
+
if (injections) {
|
|
88
|
+
for (const injection of injections) {
|
|
89
|
+
await this.container.disposeInjectableInstances(injection.token)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
protected async _initializeContainer() {
|
|
96
|
+
const dependents = this._dependents()
|
|
97
|
+
const dependencies = new Set<AnyInjectable>()
|
|
98
|
+
for (const dependant of dependents) {
|
|
99
|
+
for (const dependency of Object.values(dependant.dependencies)) {
|
|
100
|
+
dependencies.add(getDepedencencyInjectable(dependency))
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
await this.container.initialize(dependencies)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
protected async _disposeContainer() {
|
|
107
|
+
await this.container.dispose()
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export enum StoreType {
|
|
2
|
+
Redis = 'Redis',
|
|
3
|
+
Valkey = 'Valkey',
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export enum JobWorkerPool {
|
|
7
|
+
Io = 'Io',
|
|
8
|
+
Compute = 'Compute',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum LifecycleHook {
|
|
12
|
+
BeforeInitialize = 'lifecycle:beforeInitialize',
|
|
13
|
+
AfterInitialize = 'lifecycle:afterInitialize',
|
|
14
|
+
BeforeDispose = 'lifecycle:beforeDispose',
|
|
15
|
+
AfterDispose = 'lifecycle:afterDispose',
|
|
16
|
+
Stop = 'lifecycle:stop',
|
|
17
|
+
Start = 'lifecycle:start',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export enum WorkerType {
|
|
21
|
+
Application = 'Application',
|
|
22
|
+
Job = 'Job',
|
|
23
|
+
Command = 'Command',
|
|
24
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export * from './application/index.ts'
|
|
2
|
+
export * from './constants.ts'
|
|
3
|
+
export * from './core/hooks.ts'
|
|
4
|
+
export * from './core/plugin.ts'
|
|
5
|
+
export * from './core/runtime.ts'
|
|
6
|
+
export * from './enums.ts'
|
|
7
|
+
export * from './injectables.ts'
|
|
8
|
+
export * from './jobs/job.ts'
|
|
9
|
+
export * from './jobs/manager.ts'
|
|
10
|
+
export * from './jobs/router.ts'
|
|
11
|
+
export * from './jobs/step.ts'
|
|
12
|
+
export * from './pubsub/manager.ts'
|
|
13
|
+
export * from './pubsub/redis.ts'
|
|
14
|
+
export * from './scheduler/index.ts'
|
|
15
|
+
export * from './server/config.ts'
|
|
16
|
+
export * from './server/server.ts'
|
|
17
|
+
export * from './store/index.ts'
|
|
18
|
+
export * from './types.ts'
|
|
19
|
+
export * from './workers/application.ts'
|
|
20
|
+
export * from './workers/base.ts'
|
|
21
|
+
export * from './workers/job.ts'
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createLazyInjectable, Scope } from '@nmtjs/core'
|
|
2
|
+
|
|
3
|
+
import type { JobWorkerPool, WorkerType } from './enums.ts'
|
|
4
|
+
import type { JobManagerInstance } from './jobs/manager.ts'
|
|
5
|
+
import type {
|
|
6
|
+
PubSubAdapterType,
|
|
7
|
+
PubSubPublish,
|
|
8
|
+
PubSubSubscribe,
|
|
9
|
+
} from './pubsub/manager.ts'
|
|
10
|
+
import type { ServerStoreConfig } from './server/config.ts'
|
|
11
|
+
|
|
12
|
+
export const pubSubAdapter = createLazyInjectable<PubSubAdapterType>(
|
|
13
|
+
Scope.Global,
|
|
14
|
+
'PubSubAdapter',
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
export const pubSubPublish = createLazyInjectable<PubSubPublish>(
|
|
18
|
+
Scope.Global,
|
|
19
|
+
'PubSubPublish',
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
export const pubSubSubscribe = createLazyInjectable<PubSubSubscribe>(
|
|
23
|
+
Scope.Global,
|
|
24
|
+
'PubSubSubscribe',
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
export const jobManager = createLazyInjectable<JobManagerInstance>(
|
|
28
|
+
Scope.Global,
|
|
29
|
+
'JobManager',
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
export const storeConfig = createLazyInjectable<ServerStoreConfig>(
|
|
33
|
+
Scope.Global,
|
|
34
|
+
'StoreConfig',
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
export const workerType = createLazyInjectable<WorkerType>(
|
|
38
|
+
Scope.Global,
|
|
39
|
+
'WorkerType',
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
export const jobWorkerPool = createLazyInjectable<JobWorkerPool>(
|
|
43
|
+
Scope.Global,
|
|
44
|
+
'JobWorkerPool',
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
export const jobAbortSignal = createLazyInjectable<AbortSignal>(
|
|
48
|
+
Scope.Global,
|
|
49
|
+
'JobAbortSignal',
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
export const RuntimeInjectables = {
|
|
53
|
+
pubSubAdapter,
|
|
54
|
+
pubSubPublish,
|
|
55
|
+
pubSubSubscribe,
|
|
56
|
+
jobManager,
|
|
57
|
+
storeConfig,
|
|
58
|
+
workerType,
|
|
59
|
+
jobWorkerPool,
|
|
60
|
+
jobAbortSignal,
|
|
61
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import type { MaybePromise, TSError } from '@nmtjs/common'
|
|
2
|
+
import type { Dependencies, DependencyContext } from '@nmtjs/core'
|
|
3
|
+
import type { t } from '@nmtjs/type'
|
|
4
|
+
import type { AnyObjectLikeType, ObjectType } from '@nmtjs/type/object'
|
|
5
|
+
import { tryCaptureStackTrace } from '@nmtjs/common'
|
|
6
|
+
|
|
7
|
+
import type { JobWorkerPool } from '../enums.ts'
|
|
8
|
+
import type { AnyJobStep, JobStep } from './step.ts'
|
|
9
|
+
import { kJobKey } from '../constants.ts'
|
|
10
|
+
import { isJobStep } from './step.ts'
|
|
11
|
+
|
|
12
|
+
type DefaultObjectType = ObjectType<{}>
|
|
13
|
+
|
|
14
|
+
type DefaultResultType = Record<string, any>
|
|
15
|
+
|
|
16
|
+
export type AnyJobOptions = JobOptions<string, any, any, any, any, any>
|
|
17
|
+
|
|
18
|
+
export interface AnyJob {
|
|
19
|
+
_: { data: any; result: any; input: any; output: any; progress: any }
|
|
20
|
+
[kJobKey]: true
|
|
21
|
+
options: AnyJobOptions
|
|
22
|
+
steps: readonly AnyJobStep[]
|
|
23
|
+
conditions: Map<number, JobCondition<any, any, any, any>>
|
|
24
|
+
name: string
|
|
25
|
+
dependencies: Dependencies
|
|
26
|
+
input: AnyObjectLikeType
|
|
27
|
+
output: AnyObjectLikeType
|
|
28
|
+
progress: AnyObjectLikeType
|
|
29
|
+
afterEachHandler?: JobAfterEachHandler<any, any, any, any, any>
|
|
30
|
+
beforeEachHandler?: JobBeforeEachHandler<any, any, any, any, any>
|
|
31
|
+
onErrorHandler?: JobOnErrorHandler<any, any, any, any, any>
|
|
32
|
+
returnHandler?: JobReturnHandler<any, any, any, any, any, any>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type JobBackoffOptions = {
|
|
36
|
+
type: 'fixed' | 'exponential'
|
|
37
|
+
delay: number
|
|
38
|
+
jitter?: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type JobCondition<
|
|
42
|
+
Deps extends Dependencies = {},
|
|
43
|
+
Result extends DefaultResultType = {},
|
|
44
|
+
Data = any,
|
|
45
|
+
Input extends AnyObjectLikeType = AnyObjectLikeType,
|
|
46
|
+
Progress extends AnyObjectLikeType = AnyObjectLikeType,
|
|
47
|
+
> = (params: {
|
|
48
|
+
context: DependencyContext<Deps>
|
|
49
|
+
data: Data
|
|
50
|
+
input: t.infer.decode.output<Input>
|
|
51
|
+
result: Result
|
|
52
|
+
progress: t.infer.decode.output<Progress>
|
|
53
|
+
}) => MaybePromise<boolean>
|
|
54
|
+
|
|
55
|
+
export type JobReturnHandler<
|
|
56
|
+
Deps extends Dependencies,
|
|
57
|
+
Result extends DefaultResultType,
|
|
58
|
+
Input extends AnyObjectLikeType,
|
|
59
|
+
Output extends AnyObjectLikeType,
|
|
60
|
+
Data,
|
|
61
|
+
Progress extends AnyObjectLikeType = AnyObjectLikeType,
|
|
62
|
+
> = (params: {
|
|
63
|
+
context: DependencyContext<Deps>
|
|
64
|
+
data: Data
|
|
65
|
+
input: t.infer.decode.output<Input>
|
|
66
|
+
result: Result
|
|
67
|
+
progress: t.infer.decode.output<Progress>
|
|
68
|
+
}) => MaybePromise<t.infer.encode.input<Output>>
|
|
69
|
+
|
|
70
|
+
export type JobDataHandler<
|
|
71
|
+
Deps extends Dependencies,
|
|
72
|
+
Input extends AnyObjectLikeType,
|
|
73
|
+
Data,
|
|
74
|
+
Progress extends AnyObjectLikeType = AnyObjectLikeType,
|
|
75
|
+
> = (
|
|
76
|
+
ctx: DependencyContext<Deps>,
|
|
77
|
+
input: t.infer.decode.output<Input>,
|
|
78
|
+
progress: t.infer.decode.output<Progress>,
|
|
79
|
+
) => MaybePromise<Data>
|
|
80
|
+
|
|
81
|
+
export type JobAfterEachHandler<
|
|
82
|
+
Deps extends Dependencies,
|
|
83
|
+
Result extends DefaultResultType,
|
|
84
|
+
Input extends AnyObjectLikeType,
|
|
85
|
+
Data,
|
|
86
|
+
Progress extends AnyObjectLikeType = AnyObjectLikeType,
|
|
87
|
+
> = (params: {
|
|
88
|
+
context: DependencyContext<Deps>
|
|
89
|
+
data: Data
|
|
90
|
+
input: t.infer.decode.output<Input>
|
|
91
|
+
result: Result
|
|
92
|
+
progress: t.infer.decode.output<Progress>
|
|
93
|
+
step: AnyJobStep
|
|
94
|
+
stepIndex: number
|
|
95
|
+
}) => MaybePromise<void>
|
|
96
|
+
|
|
97
|
+
export type JobBeforeEachHandler<
|
|
98
|
+
Deps extends Dependencies,
|
|
99
|
+
Result extends DefaultResultType,
|
|
100
|
+
Input extends AnyObjectLikeType,
|
|
101
|
+
Data,
|
|
102
|
+
Progress extends AnyObjectLikeType = AnyObjectLikeType,
|
|
103
|
+
> = (params: {
|
|
104
|
+
context: DependencyContext<Deps>
|
|
105
|
+
data: Data
|
|
106
|
+
input: t.infer.decode.output<Input>
|
|
107
|
+
result: Result
|
|
108
|
+
progress: t.infer.decode.output<Progress>
|
|
109
|
+
step: AnyJobStep
|
|
110
|
+
stepIndex: number
|
|
111
|
+
}) => MaybePromise<void>
|
|
112
|
+
|
|
113
|
+
export type JobOnErrorHandler<
|
|
114
|
+
Deps extends Dependencies,
|
|
115
|
+
Result extends DefaultResultType,
|
|
116
|
+
Input extends AnyObjectLikeType,
|
|
117
|
+
Data,
|
|
118
|
+
Progress extends AnyObjectLikeType = AnyObjectLikeType,
|
|
119
|
+
> = (params: {
|
|
120
|
+
context: DependencyContext<Deps>
|
|
121
|
+
data: Data
|
|
122
|
+
input: t.infer.decode.output<Input>
|
|
123
|
+
result: Result
|
|
124
|
+
progress: t.infer.decode.output<Progress>
|
|
125
|
+
step: AnyJobStep
|
|
126
|
+
stepIndex: number
|
|
127
|
+
error: unknown
|
|
128
|
+
}) => MaybePromise<
|
|
129
|
+
| boolean
|
|
130
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: its ok
|
|
131
|
+
| void
|
|
132
|
+
>
|
|
133
|
+
|
|
134
|
+
export interface JobOptions<
|
|
135
|
+
Name extends string = string,
|
|
136
|
+
Input extends AnyObjectLikeType = AnyObjectLikeType,
|
|
137
|
+
Output extends AnyObjectLikeType = AnyObjectLikeType,
|
|
138
|
+
Progress extends AnyObjectLikeType = DefaultObjectType,
|
|
139
|
+
Deps extends Dependencies = {},
|
|
140
|
+
Data = any,
|
|
141
|
+
> {
|
|
142
|
+
name: Name
|
|
143
|
+
pool: JobWorkerPool
|
|
144
|
+
input: Input
|
|
145
|
+
output: Output
|
|
146
|
+
progress?: Progress
|
|
147
|
+
concurrency?: number
|
|
148
|
+
timeout?: number
|
|
149
|
+
dependencies?: Deps
|
|
150
|
+
data?: JobDataHandler<Deps, Input, Data, Progress>
|
|
151
|
+
attempts?: number
|
|
152
|
+
backoff?: JobBackoffOptions
|
|
153
|
+
oneoff?: boolean
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export class Job<
|
|
157
|
+
in out Name extends string = string,
|
|
158
|
+
in out Deps extends Dependencies = {},
|
|
159
|
+
in out Data = any,
|
|
160
|
+
in out Input extends AnyObjectLikeType = DefaultObjectType,
|
|
161
|
+
in out Output extends AnyObjectLikeType = DefaultObjectType,
|
|
162
|
+
in out Progress extends AnyObjectLikeType = DefaultObjectType,
|
|
163
|
+
out Steps extends [...AnyJobStep[]] = [],
|
|
164
|
+
in out Result extends DefaultResultType = {},
|
|
165
|
+
out Return extends boolean = false,
|
|
166
|
+
> implements AnyJob
|
|
167
|
+
{
|
|
168
|
+
_!: {
|
|
169
|
+
data: Data
|
|
170
|
+
result: Result & t.infer.decode.output<Input>
|
|
171
|
+
input: t.infer.decode.output<Input>
|
|
172
|
+
output: t.infer.decode.output<Output>
|
|
173
|
+
progress: t.infer.decode.output<Progress>
|
|
174
|
+
};
|
|
175
|
+
[kJobKey] = true as const
|
|
176
|
+
steps: Steps = [] as unknown as Steps
|
|
177
|
+
conditions: Map<number, JobCondition<any, any, any, any, any>> = new Map()
|
|
178
|
+
returnHandler?: JobReturnHandler<
|
|
179
|
+
Deps,
|
|
180
|
+
this['_']['result'],
|
|
181
|
+
Input,
|
|
182
|
+
Output,
|
|
183
|
+
Data,
|
|
184
|
+
Progress
|
|
185
|
+
>
|
|
186
|
+
afterEachHandler?: JobAfterEachHandler<
|
|
187
|
+
Deps,
|
|
188
|
+
this['_']['result'],
|
|
189
|
+
Input,
|
|
190
|
+
Data,
|
|
191
|
+
Progress
|
|
192
|
+
>
|
|
193
|
+
beforeEachHandler?: JobBeforeEachHandler<
|
|
194
|
+
Deps,
|
|
195
|
+
this['_']['result'],
|
|
196
|
+
Input,
|
|
197
|
+
Data,
|
|
198
|
+
Progress
|
|
199
|
+
>
|
|
200
|
+
onErrorHandler?: JobOnErrorHandler<
|
|
201
|
+
Deps,
|
|
202
|
+
this['_']['result'],
|
|
203
|
+
Input,
|
|
204
|
+
Data,
|
|
205
|
+
Progress
|
|
206
|
+
>
|
|
207
|
+
|
|
208
|
+
constructor(
|
|
209
|
+
public options: JobOptions<Name, Input, Output, Progress, Deps, Data>,
|
|
210
|
+
public stack?: string,
|
|
211
|
+
) {}
|
|
212
|
+
|
|
213
|
+
get name(): Name {
|
|
214
|
+
return this.options.name
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
get dependencies(): Deps {
|
|
218
|
+
return this.options.dependencies ?? ({} as Deps)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
get input(): Input {
|
|
222
|
+
return this.options.input as Input
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
get output(): Output {
|
|
226
|
+
return this.options.output as Output
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
get progress(): Progress {
|
|
230
|
+
return this.options.progress as Progress
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
step<
|
|
234
|
+
StepInput extends AnyObjectLikeType,
|
|
235
|
+
StepOutput extends AnyObjectLikeType,
|
|
236
|
+
StepDeps extends Dependencies,
|
|
237
|
+
StepResult,
|
|
238
|
+
Condition extends
|
|
239
|
+
| JobCondition<Deps, Result, this['_']['data'], Input>
|
|
240
|
+
| undefined,
|
|
241
|
+
>(
|
|
242
|
+
...[step, condition]: Return extends false
|
|
243
|
+
? [
|
|
244
|
+
step: JobStep<
|
|
245
|
+
StepInput,
|
|
246
|
+
StepOutput,
|
|
247
|
+
StepDeps,
|
|
248
|
+
this['_']['result'] extends t.infer.decode.output<StepInput>
|
|
249
|
+
? t.infer.decode.output<StepOutput>
|
|
250
|
+
: TSError<
|
|
251
|
+
'Accumulated job result does not satisfy current step input:',
|
|
252
|
+
this['_']['result']
|
|
253
|
+
>,
|
|
254
|
+
this['_']['data']
|
|
255
|
+
>,
|
|
256
|
+
condition?: Condition,
|
|
257
|
+
]
|
|
258
|
+
: [TSError<'Job has already has return statement'>]
|
|
259
|
+
) {
|
|
260
|
+
if (!isJobStep(step)) throw new Error('Invalid job step object')
|
|
261
|
+
|
|
262
|
+
const length = this.steps.push(step)
|
|
263
|
+
if (condition) this.conditions.set(length - 1, condition)
|
|
264
|
+
|
|
265
|
+
return this as unknown as Job<
|
|
266
|
+
Name,
|
|
267
|
+
Deps,
|
|
268
|
+
Data,
|
|
269
|
+
Input,
|
|
270
|
+
Output,
|
|
271
|
+
Progress,
|
|
272
|
+
[...Steps, JobStep<StepInput, StepOutput, StepDeps, StepResult, any>],
|
|
273
|
+
Result &
|
|
274
|
+
(undefined extends Condition
|
|
275
|
+
? t.infer.decode.output<
|
|
276
|
+
StepOutput extends undefined ? DefaultObjectType : StepOutput
|
|
277
|
+
>
|
|
278
|
+
: Partial<
|
|
279
|
+
t.infer.decode.output<
|
|
280
|
+
StepOutput extends undefined ? DefaultObjectType : StepOutput
|
|
281
|
+
>
|
|
282
|
+
>)
|
|
283
|
+
>
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return(
|
|
287
|
+
...[handler]: Return extends false
|
|
288
|
+
? Result extends t.infer.encode.input<Output>
|
|
289
|
+
? [
|
|
290
|
+
JobReturnHandler<
|
|
291
|
+
Deps,
|
|
292
|
+
this['_']['result'],
|
|
293
|
+
Input,
|
|
294
|
+
Output,
|
|
295
|
+
Data,
|
|
296
|
+
Progress
|
|
297
|
+
>?,
|
|
298
|
+
]
|
|
299
|
+
: [
|
|
300
|
+
JobReturnHandler<
|
|
301
|
+
Deps,
|
|
302
|
+
this['_']['result'],
|
|
303
|
+
Input,
|
|
304
|
+
Output,
|
|
305
|
+
Data,
|
|
306
|
+
Progress
|
|
307
|
+
>,
|
|
308
|
+
]
|
|
309
|
+
: [TSError<'Job already has a return statement'>]
|
|
310
|
+
) {
|
|
311
|
+
if (typeof handler === 'function') {
|
|
312
|
+
this.returnHandler = handler
|
|
313
|
+
} else {
|
|
314
|
+
this.returnHandler = (result) => result as any
|
|
315
|
+
}
|
|
316
|
+
return this as unknown as Job<
|
|
317
|
+
Name,
|
|
318
|
+
Deps,
|
|
319
|
+
Data,
|
|
320
|
+
Input,
|
|
321
|
+
Output,
|
|
322
|
+
Progress,
|
|
323
|
+
Steps,
|
|
324
|
+
Result,
|
|
325
|
+
true
|
|
326
|
+
>
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
afterEach(
|
|
330
|
+
handler: Return extends true
|
|
331
|
+
? JobAfterEachHandler<Deps, this['_']['result'], Input, Data, Progress>
|
|
332
|
+
: TSError<'Job must have a return statement to use afterEach'>,
|
|
333
|
+
) {
|
|
334
|
+
this.afterEachHandler = handler as any
|
|
335
|
+
return this
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
beforeEach(
|
|
339
|
+
handler: Return extends true
|
|
340
|
+
? JobBeforeEachHandler<Deps, this['_']['result'], Input, Data, Progress>
|
|
341
|
+
: TSError<'Job must have a return statement to use beforeEach'>,
|
|
342
|
+
) {
|
|
343
|
+
this.beforeEachHandler = handler as any
|
|
344
|
+
return this
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
onError(
|
|
348
|
+
handler: Return extends true
|
|
349
|
+
? JobOnErrorHandler<Deps, this['_']['result'], Input, Data, Progress>
|
|
350
|
+
: TSError<'Job must have a return statement to use onError'>,
|
|
351
|
+
) {
|
|
352
|
+
this.onErrorHandler = handler as any
|
|
353
|
+
return this
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function createJob<
|
|
358
|
+
Name extends string,
|
|
359
|
+
Input extends AnyObjectLikeType,
|
|
360
|
+
Output extends AnyObjectLikeType,
|
|
361
|
+
Deps extends Dependencies = {},
|
|
362
|
+
Data = any,
|
|
363
|
+
Progress extends AnyObjectLikeType = DefaultObjectType,
|
|
364
|
+
>(options: JobOptions<Name, Input, Output, Progress, Deps, Data>) {
|
|
365
|
+
const stack = tryCaptureStackTrace()
|
|
366
|
+
return new Job<Name, Deps, Data, Input, Output, Progress>(
|
|
367
|
+
{ dependencies: {} as Deps, ...options },
|
|
368
|
+
stack,
|
|
369
|
+
)
|
|
370
|
+
}
|