@nocobase/server 1.9.0-alpha.9 → 1.9.0-beta.10
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/lib/application.d.ts +13 -2
- package/lib/application.js +48 -24
- package/lib/audit-manager/index.js +3 -0
- package/lib/event-queue.d.ts +2 -0
- package/lib/event-queue.js +40 -28
- package/lib/gateway/index.d.ts +2 -0
- package/lib/gateway/index.js +33 -0
- package/lib/gateway/ws-server.js +20 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -1
- package/lib/migrations/20250902230900-update-primary-keys.d.ts +14 -0
- package/lib/migrations/20250902230900-update-primary-keys.js +126 -0
- package/lib/pub-sub-manager/pub-sub-manager.d.ts +3 -2
- package/lib/pub-sub-manager/pub-sub-manager.js +22 -9
- package/lib/redis-connection-manager.d.ts +28 -0
- package/lib/redis-connection-manager.js +119 -0
- package/lib/snowflake-id-field.d.ts +10 -0
- package/lib/snowflake-id-field.js +48 -0
- package/lib/sync-message-manager.d.ts +1 -1
- package/lib/worker-id-allocator.d.ts +18 -0
- package/lib/worker-id-allocator.js +56 -0
- package/package.json +17 -15
package/lib/application.d.ts
CHANGED
|
@@ -37,6 +37,8 @@ import { Environment } from './environment';
|
|
|
37
37
|
import { ServiceContainer } from './service-container';
|
|
38
38
|
import { EventQueue, EventQueueOptions } from './event-queue';
|
|
39
39
|
import { BackgroundJobManager, BackgroundJobManagerOptions } from './background-job-manager';
|
|
40
|
+
import { RedisConfig, RedisConnectionManager } from './redis-connection-manager';
|
|
41
|
+
import { WorkerIdAllocator } from './worker-id-allocator';
|
|
40
42
|
export type PluginType = string | typeof Plugin;
|
|
41
43
|
export type PluginConfiguration = PluginType | [PluginType, any];
|
|
42
44
|
export interface ResourceManagerOptions {
|
|
@@ -57,8 +59,9 @@ export interface AppTelemetryOptions extends TelemetryOptions {
|
|
|
57
59
|
enabled?: boolean;
|
|
58
60
|
}
|
|
59
61
|
export interface ApplicationOptions {
|
|
60
|
-
instanceId?:
|
|
62
|
+
instanceId?: number;
|
|
61
63
|
database?: IDatabaseOptions | Database;
|
|
64
|
+
redisConfig?: RedisConfig;
|
|
62
65
|
cacheManager?: CacheManagerOptions;
|
|
63
66
|
/**
|
|
64
67
|
* this property is deprecated and should not be used.
|
|
@@ -131,9 +134,12 @@ export type MaintainingCommandStatus = {
|
|
|
131
134
|
status: MaintainingStatus;
|
|
132
135
|
error?: Error;
|
|
133
136
|
};
|
|
137
|
+
interface SnowflakeIdGenerator {
|
|
138
|
+
generate(): number | BigInt;
|
|
139
|
+
}
|
|
134
140
|
export declare class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
|
135
141
|
options: ApplicationOptions;
|
|
136
|
-
|
|
142
|
+
private _instanceId;
|
|
137
143
|
/**
|
|
138
144
|
* @internal
|
|
139
145
|
*/
|
|
@@ -168,6 +174,9 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
168
174
|
/**
|
|
169
175
|
* @internal
|
|
170
176
|
*/
|
|
177
|
+
redisConnectionManager: RedisConnectionManager;
|
|
178
|
+
workerIdAllocator: WorkerIdAllocator;
|
|
179
|
+
snowflakeIdGenerator: SnowflakeIdGenerator;
|
|
171
180
|
pubSubManager: PubSubManager;
|
|
172
181
|
syncMessageManager: SyncMessageManager;
|
|
173
182
|
requestLogger: Logger;
|
|
@@ -186,6 +195,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
186
195
|
private static staticCommands;
|
|
187
196
|
static addCommand(callback: (app: Application) => void): void;
|
|
188
197
|
private _sqlLogger;
|
|
198
|
+
get instanceId(): number;
|
|
189
199
|
get sqlLogger(): Logger;
|
|
190
200
|
protected _logger: SystemLogger;
|
|
191
201
|
get logger(): SystemLogger;
|
|
@@ -309,6 +319,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
|
|
|
309
319
|
actions(handlers: any, options?: ActionsOptions): void;
|
|
310
320
|
command(name: string, desc?: string, opts?: CommandOptions): AppCommand;
|
|
311
321
|
findCommand(name: string): Command;
|
|
322
|
+
private disposeServices;
|
|
312
323
|
/**
|
|
313
324
|
* @internal
|
|
314
325
|
*/
|
package/lib/application.js
CHANGED
|
@@ -51,12 +51,12 @@ var import_logger = require("@nocobase/logger");
|
|
|
51
51
|
var import_telemetry = require("@nocobase/telemetry");
|
|
52
52
|
var import_lock_manager = require("@nocobase/lock-manager");
|
|
53
53
|
var import_utils = require("@nocobase/utils");
|
|
54
|
+
var import_snowflake_id = require("@nocobase/snowflake-id");
|
|
54
55
|
var import_crypto = require("crypto");
|
|
55
56
|
var import_glob = __toESM(require("glob"));
|
|
56
57
|
var import_koa = __toESM(require("koa"));
|
|
57
58
|
var import_koa_compose = __toESM(require("koa-compose"));
|
|
58
59
|
var import_lodash = __toESM(require("lodash"));
|
|
59
|
-
var import_nanoid = require("nanoid");
|
|
60
60
|
var import_path = __toESM(require("path"));
|
|
61
61
|
var import_semver = __toESM(require("semver"));
|
|
62
62
|
var import_acl = require("./acl");
|
|
@@ -84,11 +84,13 @@ var import_environment = require("./environment");
|
|
|
84
84
|
var import_service_container = require("./service-container");
|
|
85
85
|
var import_event_queue = require("./event-queue");
|
|
86
86
|
var import_background_job_manager = require("./background-job-manager");
|
|
87
|
+
var import_redis_connection_manager = require("./redis-connection-manager");
|
|
88
|
+
var import_worker_id_allocator = require("./worker-id-allocator");
|
|
89
|
+
var import_snowflake_id_field = require("./snowflake-id-field");
|
|
87
90
|
const _Application = class _Application extends import_koa.default {
|
|
88
91
|
constructor(options) {
|
|
89
92
|
super();
|
|
90
93
|
this.options = options;
|
|
91
|
-
this.instanceId = options.instanceId || (0, import_nanoid.nanoid)();
|
|
92
94
|
this.context.reqId = (0, import_crypto.randomUUID)();
|
|
93
95
|
this.rawOptions = this.name == "main" ? import_lodash.default.cloneDeep(options) : {};
|
|
94
96
|
this.init();
|
|
@@ -96,7 +98,7 @@ const _Application = class _Application extends import_koa.default {
|
|
|
96
98
|
this._appSupervisor.addApp(this);
|
|
97
99
|
}
|
|
98
100
|
}
|
|
99
|
-
|
|
101
|
+
_instanceId;
|
|
100
102
|
/**
|
|
101
103
|
* @internal
|
|
102
104
|
*/
|
|
@@ -124,6 +126,9 @@ const _Application = class _Application extends import_koa.default {
|
|
|
124
126
|
/**
|
|
125
127
|
* @internal
|
|
126
128
|
*/
|
|
129
|
+
redisConnectionManager;
|
|
130
|
+
workerIdAllocator;
|
|
131
|
+
snowflakeIdGenerator;
|
|
127
132
|
pubSubManager;
|
|
128
133
|
syncMessageManager;
|
|
129
134
|
requestLogger;
|
|
@@ -142,6 +147,9 @@ const _Application = class _Application extends import_koa.default {
|
|
|
142
147
|
this.staticCommands.push(callback);
|
|
143
148
|
}
|
|
144
149
|
_sqlLogger;
|
|
150
|
+
get instanceId() {
|
|
151
|
+
return this._instanceId;
|
|
152
|
+
}
|
|
145
153
|
get sqlLogger() {
|
|
146
154
|
return this._sqlLogger;
|
|
147
155
|
}
|
|
@@ -279,6 +287,9 @@ const _Application = class _Application extends import_koa.default {
|
|
|
279
287
|
if (!WORKER_MODE) {
|
|
280
288
|
return true;
|
|
281
289
|
}
|
|
290
|
+
if (WORKER_MODE === "-") {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
282
293
|
const topics = WORKER_MODE.trim().split(",");
|
|
283
294
|
if (key) {
|
|
284
295
|
if (WORKER_MODE === "*") {
|
|
@@ -389,16 +400,10 @@ const _Application = class _Application extends import_koa.default {
|
|
|
389
400
|
findCommand(name) {
|
|
390
401
|
return this.cli._findCommand(name);
|
|
391
402
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
async reInit() {
|
|
396
|
-
if (!this._loaded) {
|
|
397
|
-
return;
|
|
403
|
+
async disposeServices() {
|
|
404
|
+
if (this.redisConnectionManager) {
|
|
405
|
+
await this.redisConnectionManager.close();
|
|
398
406
|
}
|
|
399
|
-
this.log.info("app reinitializing");
|
|
400
|
-
await this.emitAsync("beforeStop");
|
|
401
|
-
await this.emitAsync("afterStop");
|
|
402
407
|
if (this.cacheManager) {
|
|
403
408
|
await this.cacheManager.close();
|
|
404
409
|
}
|
|
@@ -408,6 +413,19 @@ const _Application = class _Application extends import_koa.default {
|
|
|
408
413
|
if (this.telemetry.started) {
|
|
409
414
|
await this.telemetry.shutdown();
|
|
410
415
|
}
|
|
416
|
+
await this.workerIdAllocator.release();
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* @internal
|
|
420
|
+
*/
|
|
421
|
+
async reInit() {
|
|
422
|
+
if (!this._loaded) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
this.log.info("app reinitializing");
|
|
426
|
+
await this.emitAsync("beforeStop");
|
|
427
|
+
await this.emitAsync("afterStop");
|
|
428
|
+
await this.disposeServices();
|
|
411
429
|
this.closeLogger();
|
|
412
430
|
const oldDb = this.db;
|
|
413
431
|
this.init();
|
|
@@ -431,12 +449,7 @@ const _Application = class _Application extends import_koa.default {
|
|
|
431
449
|
if (options == null ? void 0 : options.reload) {
|
|
432
450
|
this.setMaintainingMessage("app reload");
|
|
433
451
|
this.log.info(`app.reload()`, { method: "load" });
|
|
434
|
-
|
|
435
|
-
await this.cacheManager.close();
|
|
436
|
-
}
|
|
437
|
-
if (this.telemetry.started) {
|
|
438
|
-
await this.telemetry.shutdown();
|
|
439
|
-
}
|
|
452
|
+
await this.disposeServices();
|
|
440
453
|
const oldDb = this.db;
|
|
441
454
|
this.init();
|
|
442
455
|
if (!oldDb.closed()) {
|
|
@@ -457,6 +470,15 @@ const _Application = class _Application extends import_koa.default {
|
|
|
457
470
|
if ((options == null ? void 0 : options.hooks) !== false) {
|
|
458
471
|
await this.emitAsync("beforeLoad", this, options);
|
|
459
472
|
}
|
|
473
|
+
if (!this._instanceId) {
|
|
474
|
+
this._instanceId = await this.workerIdAllocator.getWorkerId();
|
|
475
|
+
this.log.info(`allocate worker id: ${this._instanceId}`, { method: "load" });
|
|
476
|
+
}
|
|
477
|
+
if (!this.snowflakeIdGenerator) {
|
|
478
|
+
this.snowflakeIdGenerator = new import_snowflake_id.Snowflake({
|
|
479
|
+
workerId: this._instanceId
|
|
480
|
+
});
|
|
481
|
+
}
|
|
460
482
|
if (!this.telemetry.started) {
|
|
461
483
|
this.telemetry.init();
|
|
462
484
|
if ((_a = this.options.telemetry) == null ? void 0 : _a.enabled) {
|
|
@@ -704,12 +726,7 @@ const _Application = class _Application extends import_koa.default {
|
|
|
704
726
|
} catch (e) {
|
|
705
727
|
log.error(e.message, { method: "stop", err: e.stack });
|
|
706
728
|
}
|
|
707
|
-
|
|
708
|
-
await this.cacheManager.close();
|
|
709
|
-
}
|
|
710
|
-
if (this.telemetry.started) {
|
|
711
|
-
await this.telemetry.shutdown();
|
|
712
|
-
}
|
|
729
|
+
await this.disposeServices();
|
|
713
730
|
await this.emitAsync("afterStop", this, options);
|
|
714
731
|
this.emit("__stopped", this, options);
|
|
715
732
|
this.stopped = true;
|
|
@@ -873,6 +890,7 @@ const _Application = class _Application extends import_koa.default {
|
|
|
873
890
|
}
|
|
874
891
|
init() {
|
|
875
892
|
const options = this.options;
|
|
893
|
+
this._instanceId = options.instanceId;
|
|
876
894
|
this.initLogger(options.logger);
|
|
877
895
|
this.reInitEvents();
|
|
878
896
|
this.middleware = new import_utils.Toposort();
|
|
@@ -881,6 +899,11 @@ const _Application = class _Application extends import_koa.default {
|
|
|
881
899
|
this.db.removeAllListeners();
|
|
882
900
|
}
|
|
883
901
|
this.createMainDataSource(options);
|
|
902
|
+
this.redisConnectionManager = new import_redis_connection_manager.RedisConnectionManager({
|
|
903
|
+
redisConfig: options.redisConfig,
|
|
904
|
+
logger: this._logger.child({ module: "redis-connection-manager" })
|
|
905
|
+
});
|
|
906
|
+
this.workerIdAllocator = new import_worker_id_allocator.WorkerIdAllocator();
|
|
884
907
|
this._cronJobManager = new import_cron_job_manager.CronJobManager(this);
|
|
885
908
|
this._env = new import_environment.Environment();
|
|
886
909
|
this._cli = this.createCLI();
|
|
@@ -959,6 +982,7 @@ const _Application = class _Application extends import_koa.default {
|
|
|
959
982
|
app: this
|
|
960
983
|
});
|
|
961
984
|
this.dataSourceManager.dataSources.set("main", mainDataSourceInstance);
|
|
985
|
+
(0, import_snowflake_id_field.setupSnowflakeIdField)(this);
|
|
962
986
|
}
|
|
963
987
|
createDatabase(options) {
|
|
964
988
|
const logging = /* @__PURE__ */ __name((...args) => {
|
|
@@ -252,6 +252,9 @@ const _AuditManager = class _AuditManager {
|
|
|
252
252
|
async output(ctx, reqId, metadata) {
|
|
253
253
|
var _a;
|
|
254
254
|
try {
|
|
255
|
+
if (!ctx.action) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
255
258
|
const { resourceName, actionName } = ctx.action;
|
|
256
259
|
const action = this.getAction(actionName, resourceName);
|
|
257
260
|
if (!action) {
|
package/lib/event-queue.d.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
import Application from './application';
|
|
10
|
+
import { SystemLogger } from '@nocobase/logger';
|
|
10
11
|
export declare const QUEUE_DEFAULT_INTERVAL = 250;
|
|
11
12
|
export declare const QUEUE_DEFAULT_CONCURRENCY = 1;
|
|
12
13
|
export declare const QUEUE_DEFAULT_ACK_TIMEOUT = 15000;
|
|
@@ -58,6 +59,7 @@ export declare class MemoryEventQueueAdapter implements IEventQueueAdapter {
|
|
|
58
59
|
listen: (channel: string) => void;
|
|
59
60
|
constructor(options: {
|
|
60
61
|
appName: string;
|
|
62
|
+
logger: SystemLogger;
|
|
61
63
|
});
|
|
62
64
|
isConnected(): boolean;
|
|
63
65
|
setConnected(connected: boolean): void;
|
package/lib/event-queue.js
CHANGED
|
@@ -77,9 +77,10 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
77
77
|
if (!this.connected) {
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
|
+
const { logger } = this.options;
|
|
80
81
|
const event = this.events.get(channel);
|
|
81
82
|
if (!event) {
|
|
82
|
-
|
|
83
|
+
logger.warn(`memory queue (${channel}) not found, skipping...`);
|
|
83
84
|
return;
|
|
84
85
|
}
|
|
85
86
|
if (!event.idle()) {
|
|
@@ -88,12 +89,9 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
88
89
|
const reading = this.reading.get(channel) || [];
|
|
89
90
|
const count = (event.concurrency || QUEUE_DEFAULT_CONCURRENCY) - reading.length;
|
|
90
91
|
if (count <= 0) {
|
|
91
|
-
console.debug(
|
|
92
|
-
`memory queue (${channel}) is already reading as max concurrency (${reading.length}), waiting last reading to end...`
|
|
93
|
-
);
|
|
94
92
|
return;
|
|
95
93
|
}
|
|
96
|
-
|
|
94
|
+
logger.debug(`reading more from queue (${channel}), count: ${count}`);
|
|
97
95
|
this.read(channel, count).forEach((promise) => {
|
|
98
96
|
reading.push(promise);
|
|
99
97
|
promise.finally(() => {
|
|
@@ -114,20 +112,21 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
114
112
|
async loadFromStorage() {
|
|
115
113
|
let queues = {};
|
|
116
114
|
let exists = false;
|
|
115
|
+
const { logger } = this.options;
|
|
117
116
|
try {
|
|
118
117
|
await import_promises.default.stat(this.storagePath);
|
|
119
118
|
exists = true;
|
|
120
119
|
} catch (ex) {
|
|
121
|
-
|
|
120
|
+
logger.info(`memory queue storage file not found, skip`);
|
|
122
121
|
}
|
|
123
122
|
if (exists) {
|
|
124
123
|
try {
|
|
125
124
|
const queueJson = await import_promises.default.readFile(this.storagePath);
|
|
126
125
|
queues = JSON.parse(queueJson.toString());
|
|
127
|
-
|
|
126
|
+
logger.debug("memory queue loaded from storage", queues);
|
|
128
127
|
await import_promises.default.unlink(this.storagePath);
|
|
129
128
|
} catch (ex) {
|
|
130
|
-
|
|
129
|
+
logger.error("failed to load queue from storage", ex);
|
|
131
130
|
}
|
|
132
131
|
}
|
|
133
132
|
this.queues = new Map(Object.entries(queues));
|
|
@@ -139,12 +138,13 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
139
138
|
}
|
|
140
139
|
return acc;
|
|
141
140
|
}, {});
|
|
141
|
+
const { logger } = this.options;
|
|
142
142
|
if (Object.keys(queues).length) {
|
|
143
143
|
await import_promises.default.mkdir(import_path.default.dirname(this.storagePath), { recursive: true });
|
|
144
144
|
await import_promises.default.writeFile(this.storagePath, JSON.stringify(queues));
|
|
145
|
-
|
|
145
|
+
logger.debug("memory queue saved to storage", queues);
|
|
146
146
|
} else {
|
|
147
|
-
|
|
147
|
+
logger.debug("memory queue empty, no need to save to storage");
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
async connect() {
|
|
@@ -163,13 +163,14 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
163
163
|
if (!this.connected) {
|
|
164
164
|
return;
|
|
165
165
|
}
|
|
166
|
+
const { logger } = this.options;
|
|
166
167
|
this.connected = false;
|
|
167
168
|
if (this.processing) {
|
|
168
|
-
|
|
169
|
+
logger.info("memory queue waiting for processing job...");
|
|
169
170
|
await this.processing;
|
|
170
|
-
|
|
171
|
+
logger.info("memory queue job cleaned");
|
|
171
172
|
}
|
|
172
|
-
|
|
173
|
+
logger.info("memory queue gracefully shutting down...");
|
|
173
174
|
await this.saveToStorage();
|
|
174
175
|
}
|
|
175
176
|
subscribe(channel, options) {
|
|
@@ -195,6 +196,7 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
195
196
|
publish(channel, content, options = { timestamp: Date.now() }) {
|
|
196
197
|
const event = this.events.get(channel);
|
|
197
198
|
if (!event) {
|
|
199
|
+
console.debug(`memory queue (${channel}) not subscribed, skip`);
|
|
198
200
|
return;
|
|
199
201
|
}
|
|
200
202
|
if (!this.queues.get(channel)) {
|
|
@@ -203,7 +205,8 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
203
205
|
const queue = this.queues.get(channel);
|
|
204
206
|
const message = { id: (0, import_crypto.randomUUID)(), content, options };
|
|
205
207
|
queue.push(message);
|
|
206
|
-
|
|
208
|
+
const { logger } = this.options;
|
|
209
|
+
logger.debug(`memory queue (${channel}) published message`, content);
|
|
207
210
|
setImmediate(() => {
|
|
208
211
|
this.emitter.emit(channel, channel);
|
|
209
212
|
});
|
|
@@ -227,8 +230,9 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
227
230
|
if (!(queue == null ? void 0 : queue.length)) {
|
|
228
231
|
return [];
|
|
229
232
|
}
|
|
233
|
+
const { logger } = this.options;
|
|
230
234
|
const messages = queue.slice(0, n);
|
|
231
|
-
|
|
235
|
+
logger.debug(`memory queue (${channel}) read ${messages.length} messages`, messages);
|
|
232
236
|
queue.splice(0, messages.length);
|
|
233
237
|
const batch = messages.map(({ id, ...message }) => this.process(channel, { id, message }));
|
|
234
238
|
return batch;
|
|
@@ -236,17 +240,18 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
236
240
|
async process(channel, { id, message }) {
|
|
237
241
|
const event = this.events.get(channel);
|
|
238
242
|
const { content, options: { timeout = QUEUE_DEFAULT_ACK_TIMEOUT, maxRetries = 0, retried = 0 } = {} } = message;
|
|
239
|
-
|
|
243
|
+
const { logger } = this.options;
|
|
244
|
+
logger.debug(`memory queue (${channel}) processing message (${id})...`, content);
|
|
240
245
|
return (async () => event.process(content, {
|
|
241
246
|
id,
|
|
242
247
|
retried,
|
|
243
248
|
signal: AbortSignal.timeout(timeout)
|
|
244
249
|
}))().then(() => {
|
|
245
|
-
|
|
250
|
+
logger.debug(`memory queue (${channel}) consumed message (${id})`);
|
|
246
251
|
}).catch((ex) => {
|
|
247
252
|
if (maxRetries > 0 && retried < maxRetries) {
|
|
248
253
|
const currentRetry = retried + 1;
|
|
249
|
-
|
|
254
|
+
logger.warn(
|
|
250
255
|
`memory queue (${channel}) consum message (${id}) failed, retrying (${currentRetry} / ${maxRetries})...`,
|
|
251
256
|
ex
|
|
252
257
|
);
|
|
@@ -254,7 +259,7 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
|
|
|
254
259
|
this.publish(channel, content, { timeout, maxRetries, retried: currentRetry, timestamp: Date.now() });
|
|
255
260
|
}, 500);
|
|
256
261
|
} else {
|
|
257
|
-
|
|
262
|
+
logger.error(ex);
|
|
258
263
|
}
|
|
259
264
|
});
|
|
260
265
|
}
|
|
@@ -265,14 +270,16 @@ const _EventQueue = class _EventQueue {
|
|
|
265
270
|
constructor(app, options = {}) {
|
|
266
271
|
this.app = app;
|
|
267
272
|
this.options = options;
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
app.
|
|
274
|
-
|
|
275
|
-
|
|
273
|
+
if (app.serving()) {
|
|
274
|
+
this.setAdapter(new MemoryEventQueueAdapter({ appName: this.app.name, logger: this.app.logger }));
|
|
275
|
+
app.on("afterStart", async () => {
|
|
276
|
+
await this.connect();
|
|
277
|
+
});
|
|
278
|
+
app.on("beforeStop", async () => {
|
|
279
|
+
app.logger.info("[queue] gracefully shutting down...");
|
|
280
|
+
await this.close();
|
|
281
|
+
});
|
|
282
|
+
}
|
|
276
283
|
}
|
|
277
284
|
adapter;
|
|
278
285
|
events = /* @__PURE__ */ new Map();
|
|
@@ -296,7 +303,12 @@ const _EventQueue = class _EventQueue {
|
|
|
296
303
|
if (!this.adapter) {
|
|
297
304
|
throw new Error("no adapter set, cannot connect");
|
|
298
305
|
}
|
|
306
|
+
if (!this.app.serving()) {
|
|
307
|
+
this.app.logger.warn("app is not serving, will not connect to event queue");
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
299
310
|
await this.adapter.connect();
|
|
311
|
+
this.app.logger.debug(`connected to adapter, using memory? ${this.adapter instanceof MemoryEventQueueAdapter}`);
|
|
300
312
|
for (const [channel, event] of this.events.entries()) {
|
|
301
313
|
this.adapter.subscribe(this.getFullChannel(channel), event);
|
|
302
314
|
}
|
|
@@ -337,7 +349,7 @@ const _EventQueue = class _EventQueue {
|
|
|
337
349
|
throw new Error("event queue not connected, cannot publish");
|
|
338
350
|
}
|
|
339
351
|
const c = this.getFullChannel(channel);
|
|
340
|
-
this.app.logger.debug(
|
|
352
|
+
this.app.logger.debug(`event queue publishing to channel(${c})`, { message });
|
|
341
353
|
await this.adapter.publish(c, message, {
|
|
342
354
|
timeout: QUEUE_DEFAULT_ACK_TIMEOUT,
|
|
343
355
|
...options,
|
package/lib/gateway/index.d.ts
CHANGED
|
@@ -47,6 +47,8 @@ export declare class Gateway extends EventEmitter {
|
|
|
47
47
|
private port;
|
|
48
48
|
private host;
|
|
49
49
|
private socketPath;
|
|
50
|
+
private terminating;
|
|
51
|
+
private onTerminate;
|
|
50
52
|
private constructor();
|
|
51
53
|
static getInstance(options?: any): Gateway;
|
|
52
54
|
static getIPCSocketClient(): Promise<false | IPCSocketClient>;
|
package/lib/gateway/index.js
CHANGED
|
@@ -86,10 +86,37 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
|
86
86
|
port = import_node_process.default.env.APP_PORT ? parseInt(import_node_process.default.env.APP_PORT) : null;
|
|
87
87
|
host = "0.0.0.0";
|
|
88
88
|
socketPath = (0, import_path.resolve)(import_node_process.default.cwd(), "storage", "gateway.sock");
|
|
89
|
+
terminating = false;
|
|
90
|
+
onTerminate = /* @__PURE__ */ __name(async (signal) => {
|
|
91
|
+
var _a;
|
|
92
|
+
if (this.terminating) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.terminating = true;
|
|
96
|
+
const supervisor = import_app_supervisor.AppSupervisor.getInstance();
|
|
97
|
+
const apps = Object.values(supervisor.apps || {});
|
|
98
|
+
try {
|
|
99
|
+
for (const app of apps) {
|
|
100
|
+
try {
|
|
101
|
+
await app.destroy({ signal });
|
|
102
|
+
} catch (error) {
|
|
103
|
+
const logger = (app == null ? void 0 : app.log) ?? console;
|
|
104
|
+
(_a = logger.error) == null ? void 0 : _a.call(logger, error);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
await supervisor.destroy();
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error("Failed to shutdown applications gracefully", error);
|
|
110
|
+
} finally {
|
|
111
|
+
this.destroy();
|
|
112
|
+
}
|
|
113
|
+
}, "onTerminate");
|
|
89
114
|
constructor() {
|
|
90
115
|
super();
|
|
91
116
|
this.reset();
|
|
92
117
|
this.socketPath = getSocketPath();
|
|
118
|
+
import_node_process.default.once("SIGTERM", this.onTerminate);
|
|
119
|
+
import_node_process.default.once("SIGINT", this.onTerminate);
|
|
93
120
|
}
|
|
94
121
|
static getInstance(options = {}) {
|
|
95
122
|
if (!_Gateway.instance) {
|
|
@@ -106,6 +133,8 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
|
106
133
|
}
|
|
107
134
|
}
|
|
108
135
|
destroy() {
|
|
136
|
+
import_node_process.default.off("SIGTERM", this.onTerminate);
|
|
137
|
+
import_node_process.default.off("SIGINT", this.onTerminate);
|
|
109
138
|
this.reset();
|
|
110
139
|
_Gateway.instance = null;
|
|
111
140
|
}
|
|
@@ -137,6 +166,10 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
|
137
166
|
this.ipcSocketServer.close();
|
|
138
167
|
this.ipcSocketServer = null;
|
|
139
168
|
}
|
|
169
|
+
if (this.wsServer) {
|
|
170
|
+
this.wsServer.close();
|
|
171
|
+
this.wsServer = null;
|
|
172
|
+
}
|
|
140
173
|
}
|
|
141
174
|
addAppSelectorMiddleware(middleware, options) {
|
|
142
175
|
if (this.selectorMiddlewares.nodes.some((existingFunc) => existingFunc.toString() === middleware.toString())) {
|
package/lib/gateway/ws-server.js
CHANGED
|
@@ -172,6 +172,20 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
172
172
|
message
|
|
173
173
|
);
|
|
174
174
|
});
|
|
175
|
+
app.on("ws:sendToUser", ({ userId, message }) => {
|
|
176
|
+
this.sendToAppUser(app.name, userId, message);
|
|
177
|
+
app.logger.trace(`[broadcasting message] ws:sendToUser for user ${userId}`, { message });
|
|
178
|
+
app.pubSubManager.publish(
|
|
179
|
+
"ws:sendToUser",
|
|
180
|
+
{
|
|
181
|
+
userId,
|
|
182
|
+
message
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
skipSelf: true
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
});
|
|
175
189
|
app.on("ws:sendToClient", ({ clientId, message }) => {
|
|
176
190
|
this.sendToClient(clientId, message);
|
|
177
191
|
});
|
|
@@ -184,6 +198,12 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
184
198
|
app.on("ws:authorized", ({ clientId, userId }) => {
|
|
185
199
|
this.sendToClient(clientId, { type: "authorized" });
|
|
186
200
|
});
|
|
201
|
+
app.on("afterLoad", () => {
|
|
202
|
+
app.pubSubManager.subscribe("ws:sendToUser", ({ userId, message }) => {
|
|
203
|
+
app.logger.debug(`[receive broadcasting message] ws:sendToUser for user ${userId}`, { message });
|
|
204
|
+
this.sendToAppUser(app.name, userId, message);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
187
207
|
}
|
|
188
208
|
addNewConnection(ws, request) {
|
|
189
209
|
const id = (0, import_nanoid.nanoid)();
|
package/lib/index.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export * from './plugin-manager';
|
|
|
19
19
|
export * from './pub-sub-manager';
|
|
20
20
|
export * from './event-queue';
|
|
21
21
|
export * from './background-job-manager';
|
|
22
|
+
export * from './worker-id-allocator';
|
|
23
|
+
export * from './redis-connection-manager';
|
|
22
24
|
export declare const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
|
|
23
25
|
export { appendToBuiltInPlugins, findAllPlugins, findBuiltInPlugins, findLocalPlugins, packageNameTrim, } from './plugin-manager/findPackageNames';
|
|
24
26
|
export { runPluginStaticImports } from './run-plugin-static-imports';
|
package/lib/index.js
CHANGED
|
@@ -61,6 +61,8 @@ __reExport(src_exports, require("./plugin-manager"), module.exports);
|
|
|
61
61
|
__reExport(src_exports, require("./pub-sub-manager"), module.exports);
|
|
62
62
|
__reExport(src_exports, require("./event-queue"), module.exports);
|
|
63
63
|
__reExport(src_exports, require("./background-job-manager"), module.exports);
|
|
64
|
+
__reExport(src_exports, require("./worker-id-allocator"), module.exports);
|
|
65
|
+
__reExport(src_exports, require("./redis-connection-manager"), module.exports);
|
|
64
66
|
var import_findPackageNames = require("./plugin-manager/findPackageNames");
|
|
65
67
|
var import_run_plugin_static_imports = require("./run-plugin-static-imports");
|
|
66
68
|
const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
|
|
@@ -84,5 +86,7 @@ const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
|
|
|
84
86
|
...require("./plugin-manager"),
|
|
85
87
|
...require("./pub-sub-manager"),
|
|
86
88
|
...require("./event-queue"),
|
|
87
|
-
...require("./background-job-manager")
|
|
89
|
+
...require("./background-job-manager"),
|
|
90
|
+
...require("./worker-id-allocator"),
|
|
91
|
+
...require("./redis-connection-manager")
|
|
88
92
|
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Migration } from '../migration';
|
|
10
|
+
export default class extends Migration {
|
|
11
|
+
on: string;
|
|
12
|
+
appVersion: string;
|
|
13
|
+
up(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var update_primary_keys_exports = {};
|
|
29
|
+
__export(update_primary_keys_exports, {
|
|
30
|
+
default: () => update_primary_keys_default
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(update_primary_keys_exports);
|
|
33
|
+
var import_sequelize = require("sequelize");
|
|
34
|
+
var import_migration = require("../migration");
|
|
35
|
+
const collections = [
|
|
36
|
+
"departments",
|
|
37
|
+
"desktopRoutes",
|
|
38
|
+
"mobileRoutes",
|
|
39
|
+
"collectionCategories",
|
|
40
|
+
"dataSourcesRolesResources",
|
|
41
|
+
"dataSourcesRolesResourcesActions",
|
|
42
|
+
"dataSourcesRolesResourcesScopes",
|
|
43
|
+
"storages",
|
|
44
|
+
"workflows",
|
|
45
|
+
"flow_nodes",
|
|
46
|
+
"executions",
|
|
47
|
+
"userWokrflowTasks",
|
|
48
|
+
"workflowCategories",
|
|
49
|
+
"approvalExecutions",
|
|
50
|
+
"approvalRecords",
|
|
51
|
+
"approvals",
|
|
52
|
+
"workflowManualTasks",
|
|
53
|
+
"workflowCcTasks",
|
|
54
|
+
"approvalMsgTpls",
|
|
55
|
+
"mailAccounts",
|
|
56
|
+
"mailSettings"
|
|
57
|
+
];
|
|
58
|
+
const _update_primary_keys_default = class _update_primary_keys_default extends import_migration.Migration {
|
|
59
|
+
on = "afterLoad";
|
|
60
|
+
// 'beforeLoad' or 'afterLoad'
|
|
61
|
+
appVersion = "<1.9.0";
|
|
62
|
+
async up() {
|
|
63
|
+
const repo = this.db.getRepository("fields");
|
|
64
|
+
if (!repo) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const queryInterface = this.db.sequelize.getQueryInterface();
|
|
68
|
+
for (const collectionName of collections) {
|
|
69
|
+
const collection = this.db.getCollection(collectionName);
|
|
70
|
+
if (collection) {
|
|
71
|
+
const tableName = collection.getTableNameWithSchema();
|
|
72
|
+
if (this.db.isPostgresCompatibleDialect()) {
|
|
73
|
+
await this.db.sequelize.transaction(async (transaction) => {
|
|
74
|
+
const schema = collection.collectionSchema();
|
|
75
|
+
const table = collection.model.tableName;
|
|
76
|
+
const seqName = `"${schema}"."${table}_id_seq"`;
|
|
77
|
+
await this.db.sequelize.query(`ALTER TABLE "${schema}"."${table}" ALTER COLUMN id DROP DEFAULT;`, {
|
|
78
|
+
transaction
|
|
79
|
+
});
|
|
80
|
+
await this.db.sequelize.query(`DROP SEQUENCE IF EXISTS ${seqName} CASCADE;`, { transaction });
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
await queryInterface.changeColumn(tableName, "id", {
|
|
84
|
+
type: import_sequelize.DataTypes.BIGINT,
|
|
85
|
+
primaryKey: true,
|
|
86
|
+
allowNull: false,
|
|
87
|
+
autoIncrement: false
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const field = await repo.findOne({
|
|
92
|
+
filter: {
|
|
93
|
+
collectionName,
|
|
94
|
+
name: "id"
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
if (field) {
|
|
98
|
+
const options = { ...field.options };
|
|
99
|
+
delete options["autoIncrement"];
|
|
100
|
+
await repo.update({
|
|
101
|
+
filter: { key: field.key },
|
|
102
|
+
values: {
|
|
103
|
+
type: "snowflakeId",
|
|
104
|
+
options
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const treeCollections = [...this.db.collections.values()].filter((collection) => collection.options.tree);
|
|
110
|
+
for (const collection of treeCollections) {
|
|
111
|
+
const pathCollection = this.db.getCollection(`main_${collection.name}_path`);
|
|
112
|
+
if (pathCollection) {
|
|
113
|
+
const nodePk = pathCollection.getField("nodePk").columnName();
|
|
114
|
+
await queryInterface.changeColumn(pathCollection.getTableNameWithSchema(), nodePk, {
|
|
115
|
+
type: import_sequelize.DataTypes.BIGINT
|
|
116
|
+
});
|
|
117
|
+
const rootPk = pathCollection.getField("rootPk").columnName();
|
|
118
|
+
await queryInterface.changeColumn(pathCollection.getTableNameWithSchema(), rootPk, {
|
|
119
|
+
type: import_sequelize.DataTypes.BIGINT
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
__name(_update_primary_keys_default, "default");
|
|
126
|
+
let update_primary_keys_default = _update_primary_keys_default;
|
|
@@ -11,11 +11,12 @@ import { HandlerManager } from './handler-manager';
|
|
|
11
11
|
import { PubSubCallback, type IPubSubAdapter, type PubSubManagerOptions, type PubSubManagerPublishOptions, type PubSubManagerSubscribeOptions } from './types';
|
|
12
12
|
export declare const createPubSubManager: (app: Application, options: PubSubManagerOptions) => PubSubManager;
|
|
13
13
|
export declare class PubSubManager {
|
|
14
|
+
protected app: Application;
|
|
14
15
|
protected options: PubSubManagerOptions;
|
|
15
16
|
protected publisherId: string;
|
|
16
17
|
protected adapter: IPubSubAdapter;
|
|
17
18
|
protected handlerManager: HandlerManager;
|
|
18
|
-
constructor(options?: PubSubManagerOptions);
|
|
19
|
+
constructor(app: Application, options?: PubSubManagerOptions);
|
|
19
20
|
get channelPrefix(): string;
|
|
20
21
|
setAdapter(adapter: IPubSubAdapter): void;
|
|
21
22
|
isConnected(): Promise<boolean>;
|
|
@@ -23,5 +24,5 @@ export declare class PubSubManager {
|
|
|
23
24
|
close(): Promise<any>;
|
|
24
25
|
subscribe(channel: string, callback: PubSubCallback, options?: PubSubManagerSubscribeOptions): Promise<void>;
|
|
25
26
|
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
26
|
-
publish(channel: string, message: any, options?: PubSubManagerPublishOptions): Promise<
|
|
27
|
+
publish(channel: string, message: any, options?: PubSubManagerPublishOptions): Promise<void>;
|
|
27
28
|
}
|
|
@@ -34,17 +34,20 @@ module.exports = __toCommonJS(pub_sub_manager_exports);
|
|
|
34
34
|
var import_utils = require("@nocobase/utils");
|
|
35
35
|
var import_handler_manager = require("./handler-manager");
|
|
36
36
|
const createPubSubManager = /* @__PURE__ */ __name((app, options) => {
|
|
37
|
-
const pubSubManager = new PubSubManager(options);
|
|
38
|
-
app.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
const pubSubManager = new PubSubManager(app, options);
|
|
38
|
+
if (app.serving()) {
|
|
39
|
+
app.on("afterStart", async () => {
|
|
40
|
+
await pubSubManager.connect();
|
|
41
|
+
});
|
|
42
|
+
app.on("afterStop", async () => {
|
|
43
|
+
await pubSubManager.close();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
44
46
|
return pubSubManager;
|
|
45
47
|
}, "createPubSubManager");
|
|
46
48
|
const _PubSubManager = class _PubSubManager {
|
|
47
|
-
constructor(options = {}) {
|
|
49
|
+
constructor(app, options = {}) {
|
|
50
|
+
this.app = app;
|
|
48
51
|
this.options = options;
|
|
49
52
|
this.publisherId = (0, import_utils.uid)();
|
|
50
53
|
this.handlerManager = new import_handler_manager.HandlerManager(this.publisherId);
|
|
@@ -69,8 +72,13 @@ const _PubSubManager = class _PubSubManager {
|
|
|
69
72
|
if (!this.adapter) {
|
|
70
73
|
return;
|
|
71
74
|
}
|
|
75
|
+
if (!this.app.serving()) {
|
|
76
|
+
this.app.logger.warn("app is not serving, will not connect to event queue");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
72
79
|
await this.adapter.connect();
|
|
73
80
|
await this.handlerManager.each(async (channel, headler) => {
|
|
81
|
+
this.app.logger.debug(`[PubSubManager] subscribe ${channel} added before connected`);
|
|
74
82
|
await this.adapter.subscribe(`${this.channelPrefix}${channel}`, headler);
|
|
75
83
|
});
|
|
76
84
|
}
|
|
@@ -84,6 +92,7 @@ const _PubSubManager = class _PubSubManager {
|
|
|
84
92
|
await this.unsubscribe(channel, callback);
|
|
85
93
|
const handler = this.handlerManager.set(channel, callback, options);
|
|
86
94
|
if (await this.isConnected()) {
|
|
95
|
+
this.app.logger.debug(`[PubSubManager] subscribe ${channel} added after connected`);
|
|
87
96
|
await this.adapter.subscribe(`${this.channelPrefix}${channel}`, handler);
|
|
88
97
|
}
|
|
89
98
|
}
|
|
@@ -97,6 +106,9 @@ const _PubSubManager = class _PubSubManager {
|
|
|
97
106
|
async publish(channel, message, options) {
|
|
98
107
|
var _a;
|
|
99
108
|
if (!((_a = this.adapter) == null ? void 0 : _a.isConnected())) {
|
|
109
|
+
this.app.logger.warn(
|
|
110
|
+
`[PubSubManager] adapter is not exist or not connected, cannot publish message to channel ${channel}`
|
|
111
|
+
);
|
|
100
112
|
return;
|
|
101
113
|
}
|
|
102
114
|
const wrappedMessage = JSON.stringify({
|
|
@@ -104,7 +116,8 @@ const _PubSubManager = class _PubSubManager {
|
|
|
104
116
|
...options,
|
|
105
117
|
message
|
|
106
118
|
});
|
|
107
|
-
|
|
119
|
+
await this.adapter.publish(`${this.channelPrefix}${channel}`, wrappedMessage);
|
|
120
|
+
this.app.logger.trace(`[PubSubManager] published message to channel ${channel}`);
|
|
108
121
|
}
|
|
109
122
|
};
|
|
110
123
|
__name(_PubSubManager, "PubSubManager");
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import Redis from 'ioredis';
|
|
10
|
+
import { Logger } from '@nocobase/logger';
|
|
11
|
+
export { Redis };
|
|
12
|
+
export interface RedisConfig {
|
|
13
|
+
connectionString: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class RedisConnectionManager {
|
|
16
|
+
private logger;
|
|
17
|
+
private config;
|
|
18
|
+
private connections;
|
|
19
|
+
constructor(config: {
|
|
20
|
+
redisConfig: RedisConfig;
|
|
21
|
+
logger: Logger;
|
|
22
|
+
});
|
|
23
|
+
private bindEvents;
|
|
24
|
+
private getClient;
|
|
25
|
+
getConnection(key?: string, config?: RedisConfig): Redis | null;
|
|
26
|
+
getConnectionSync(key?: string, config?: RedisConfig): Promise<Redis>;
|
|
27
|
+
close(): Promise<void>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
+
mod
|
|
36
|
+
));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
var redis_connection_manager_exports = {};
|
|
39
|
+
__export(redis_connection_manager_exports, {
|
|
40
|
+
Redis: () => import_ioredis.default,
|
|
41
|
+
RedisConnectionManager: () => RedisConnectionManager
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(redis_connection_manager_exports);
|
|
44
|
+
var import_ioredis = __toESM(require("ioredis"));
|
|
45
|
+
const _RedisConnectionManager = class _RedisConnectionManager {
|
|
46
|
+
logger;
|
|
47
|
+
config;
|
|
48
|
+
connections = /* @__PURE__ */ new Map();
|
|
49
|
+
constructor(config) {
|
|
50
|
+
this.config = config.redisConfig;
|
|
51
|
+
this.logger = config.logger;
|
|
52
|
+
}
|
|
53
|
+
bindEvents(conn, key, config) {
|
|
54
|
+
conn.on("connect", () => {
|
|
55
|
+
this.logger.info(`Redis connected`, {
|
|
56
|
+
method: "getConnection",
|
|
57
|
+
key,
|
|
58
|
+
config
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
conn.on("error", (err) => {
|
|
62
|
+
this.logger.error(err.message, {
|
|
63
|
+
err,
|
|
64
|
+
method: "getConnection",
|
|
65
|
+
key,
|
|
66
|
+
config
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
conn.on("close", () => {
|
|
70
|
+
this.logger.trace(`Redis closed`, {
|
|
71
|
+
method: "getConnection",
|
|
72
|
+
key,
|
|
73
|
+
config
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
getClient(key = "default", config) {
|
|
78
|
+
let conn = this.connections.get(key);
|
|
79
|
+
if (conn) {
|
|
80
|
+
return conn;
|
|
81
|
+
}
|
|
82
|
+
const cfg = config || this.config;
|
|
83
|
+
if (!cfg.connectionString) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
conn = new import_ioredis.default(cfg.connectionString);
|
|
87
|
+
this.connections.set(key, conn);
|
|
88
|
+
this.bindEvents(conn, key, cfg);
|
|
89
|
+
return conn;
|
|
90
|
+
}
|
|
91
|
+
getConnection(key = "default", config) {
|
|
92
|
+
return this.getClient(key, config);
|
|
93
|
+
}
|
|
94
|
+
async getConnectionSync(key = "default", config) {
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
const conn = this.getClient(key, config);
|
|
97
|
+
if (!conn) {
|
|
98
|
+
return reject(new Error("Redis connect string is missing"));
|
|
99
|
+
}
|
|
100
|
+
conn.once("connect", () => resolve(conn));
|
|
101
|
+
conn.once("error", reject);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async close() {
|
|
105
|
+
for (const conn of this.connections.values()) {
|
|
106
|
+
if (!(conn == null ? void 0 : conn.status) || conn.status === "close" || conn.status === "end") {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
await conn.quit();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
__name(_RedisConnectionManager, "RedisConnectionManager");
|
|
114
|
+
let RedisConnectionManager = _RedisConnectionManager;
|
|
115
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
116
|
+
0 && (module.exports = {
|
|
117
|
+
Redis,
|
|
118
|
+
RedisConnectionManager
|
|
119
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import Application from './application';
|
|
10
|
+
export declare function setupSnowflakeIdField(app: Application): void;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var snowflake_id_field_exports = {};
|
|
29
|
+
__export(snowflake_id_field_exports, {
|
|
30
|
+
setupSnowflakeIdField: () => setupSnowflakeIdField
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(snowflake_id_field_exports);
|
|
33
|
+
var import_database = require("@nocobase/database");
|
|
34
|
+
function setupSnowflakeIdField(app) {
|
|
35
|
+
const _SnowflakeIdField = class _SnowflakeIdField extends import_database.SnowflakeIdField {
|
|
36
|
+
};
|
|
37
|
+
__name(_SnowflakeIdField, "SnowflakeIdField");
|
|
38
|
+
let SnowflakeIdField = _SnowflakeIdField;
|
|
39
|
+
SnowflakeIdField.setApp(app);
|
|
40
|
+
app.db.registerFieldTypes({
|
|
41
|
+
snowflakeId: SnowflakeIdField
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
__name(setupSnowflakeIdField, "setupSnowflakeIdField");
|
|
45
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
46
|
+
0 && (module.exports = {
|
|
47
|
+
setupSnowflakeIdField
|
|
48
|
+
});
|
|
@@ -16,7 +16,7 @@ export declare class SyncMessageManager {
|
|
|
16
16
|
protected pubSubManager: PubSubManager;
|
|
17
17
|
constructor(app: Application, options?: any);
|
|
18
18
|
get debounce(): any;
|
|
19
|
-
publish(channel: string, message: any, options?: PubSubManagerPublishOptions & Transactionable): Promise<
|
|
19
|
+
publish(channel: string, message: any, options?: PubSubManagerPublishOptions & Transactionable): Promise<unknown>;
|
|
20
20
|
subscribe(channel: string, callback: PubSubCallback): Promise<void>;
|
|
21
21
|
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
22
22
|
sync(): Promise<void>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
export interface WorkerIdAllocatorAdapter {
|
|
10
|
+
getWorkerId(): Promise<number>;
|
|
11
|
+
release(): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare class WorkerIdAllocator {
|
|
14
|
+
private adapter;
|
|
15
|
+
setAdapter(adapter: WorkerIdAllocatorAdapter): void;
|
|
16
|
+
getWorkerId(): Promise<number>;
|
|
17
|
+
release(): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var worker_id_allocator_exports = {};
|
|
29
|
+
__export(worker_id_allocator_exports, {
|
|
30
|
+
WorkerIdAllocator: () => WorkerIdAllocator
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(worker_id_allocator_exports);
|
|
33
|
+
const _WorkerIdAllocator = class _WorkerIdAllocator {
|
|
34
|
+
adapter;
|
|
35
|
+
setAdapter(adapter) {
|
|
36
|
+
this.adapter = adapter;
|
|
37
|
+
}
|
|
38
|
+
async getWorkerId() {
|
|
39
|
+
if (this.adapter) {
|
|
40
|
+
return this.adapter.getWorkerId();
|
|
41
|
+
}
|
|
42
|
+
return Math.floor(Math.random() * 32);
|
|
43
|
+
}
|
|
44
|
+
async release() {
|
|
45
|
+
if (this.adapter) {
|
|
46
|
+
return this.adapter.release();
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
__name(_WorkerIdAllocator, "WorkerIdAllocator");
|
|
52
|
+
let WorkerIdAllocator = _WorkerIdAllocator;
|
|
53
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
54
|
+
0 && (module.exports = {
|
|
55
|
+
WorkerIdAllocator
|
|
56
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/server",
|
|
3
|
-
"version": "1.9.0-
|
|
3
|
+
"version": "1.9.0-beta.10",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -10,19 +10,20 @@
|
|
|
10
10
|
"@koa/cors": "^5.0.0",
|
|
11
11
|
"@koa/multer": "^3.1.0",
|
|
12
12
|
"@koa/router": "^13.1.0",
|
|
13
|
-
"@nocobase/acl": "1.9.0-
|
|
14
|
-
"@nocobase/actions": "1.9.0-
|
|
15
|
-
"@nocobase/auth": "1.9.0-
|
|
16
|
-
"@nocobase/cache": "1.9.0-
|
|
17
|
-
"@nocobase/data-source-manager": "1.9.0-
|
|
18
|
-
"@nocobase/database": "1.9.0-
|
|
19
|
-
"@nocobase/evaluators": "1.9.0-
|
|
20
|
-
"@nocobase/lock-manager": "1.9.0-
|
|
21
|
-
"@nocobase/logger": "1.9.0-
|
|
22
|
-
"@nocobase/resourcer": "1.9.0-
|
|
23
|
-
"@nocobase/sdk": "1.9.0-
|
|
24
|
-
"@nocobase/
|
|
25
|
-
"@nocobase/
|
|
13
|
+
"@nocobase/acl": "1.9.0-beta.10",
|
|
14
|
+
"@nocobase/actions": "1.9.0-beta.10",
|
|
15
|
+
"@nocobase/auth": "1.9.0-beta.10",
|
|
16
|
+
"@nocobase/cache": "1.9.0-beta.10",
|
|
17
|
+
"@nocobase/data-source-manager": "1.9.0-beta.10",
|
|
18
|
+
"@nocobase/database": "1.9.0-beta.10",
|
|
19
|
+
"@nocobase/evaluators": "1.9.0-beta.10",
|
|
20
|
+
"@nocobase/lock-manager": "1.9.0-beta.10",
|
|
21
|
+
"@nocobase/logger": "1.9.0-beta.10",
|
|
22
|
+
"@nocobase/resourcer": "1.9.0-beta.10",
|
|
23
|
+
"@nocobase/sdk": "1.9.0-beta.10",
|
|
24
|
+
"@nocobase/snowflake-id": "1.9.0-beta.10",
|
|
25
|
+
"@nocobase/telemetry": "1.9.0-beta.10",
|
|
26
|
+
"@nocobase/utils": "1.9.0-beta.10",
|
|
26
27
|
"@types/decompress": "4.2.7",
|
|
27
28
|
"@types/ini": "^1.3.31",
|
|
28
29
|
"@types/koa-send": "^4.1.3",
|
|
@@ -40,6 +41,7 @@
|
|
|
40
41
|
"fs-extra": "^11.1.1",
|
|
41
42
|
"i18next": "^22.4.9",
|
|
42
43
|
"ini": "^4.1.1",
|
|
44
|
+
"ioredis": "^5.7.0",
|
|
43
45
|
"koa": "^2.15.4",
|
|
44
46
|
"koa-bodyparser": "^4.3.0",
|
|
45
47
|
"koa-send": "^5.0.1",
|
|
@@ -57,5 +59,5 @@
|
|
|
57
59
|
"@types/serve-handler": "^6.1.1",
|
|
58
60
|
"@types/ws": "^8.5.5"
|
|
59
61
|
},
|
|
60
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "a4a5180d8e9b6bebe9df1a931c2c0f8b5c5d26e6"
|
|
61
63
|
}
|