From e86e40b93a537fae0b60ac4328640a728b623f6a Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 14 Mar 2023 13:10:15 +0800 Subject: [PATCH] fix: app manager reload (#1565) * fix: app manager reload * chore: plugin test order --- packages/core/database/src/mock-database.ts | 3 +++ packages/core/server/src/app-manager.ts | 9 +++++++-- packages/core/server/src/application.ts | 6 +++++- .../src/server/__tests__/collection-sync.test.ts | 2 +- .../multi-app-share-collection/src/server/plugin.ts | 7 ++++++- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/core/database/src/mock-database.ts b/packages/core/database/src/mock-database.ts index 8ce980526..6414232bd 100644 --- a/packages/core/database/src/mock-database.ts +++ b/packages/core/database/src/mock-database.ts @@ -33,6 +33,9 @@ export function getConfigByEnv() { timezone: process.env.DB_TIMEZONE, underscored: process.env.DB_UNDERSCORED === 'true', schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined, + dialectOptions: { + application_name: process.env.DB_DIALECT == 'postgres' ? 'nocobase.main' : undefined, + }, }; } diff --git a/packages/core/server/src/app-manager.ts b/packages/core/server/src/app-manager.ts index 4a3f069db..c3cff0568 100644 --- a/packages/core/server/src/app-manager.ts +++ b/packages/core/server/src/app-manager.ts @@ -9,12 +9,17 @@ type AppSelector = (req: IncomingMessage) => AppSelectorReturn | Promise = new Map(); + public app: Application; - constructor(public app: Application) { + constructor(app: Application) { super(); + this.bindMainApplication(app); + } + bindMainApplication(mainApp: Application) { + this.app = mainApp; const passEventToSubApps = (eventName, method) => { - app.on(eventName, async (mainApp, options) => { + mainApp.on(eventName, async (mainApp, options) => { console.log(`receive event ${eventName} from ${mainApp.name}`); for (const application of this.applications.values()) { console.log(`pass ${eventName} to ${application.name} `); diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index 255dd7b9c..0e9fbba73 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -271,7 +271,11 @@ export class Application exten }); } - this._appManager = new AppManager(this); + if (this._appManager) { + this._appManager.bindMainApplication(this); + } else { + this._appManager = new AppManager(this); + } if (this.options.acl !== false) { this._resourcer.use(this._acl.middleware(), { tag: 'acl', after: ['parseToken'] }); diff --git a/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts b/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts index 01fd38f6b..7a2a58b23 100644 --- a/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts +++ b/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts @@ -90,7 +90,7 @@ pgOnly()('collection sync', () => { expect(userInSub2.get('roles').map((item) => item.name)).toContain(defaultRoleInSub2.name); }); - it.skip('should sync plugin status between apps', async () => { + it('should sync plugin status between apps', async () => { await mainApp.db.getRepository('applications').create({ values: { name: 'sub1', diff --git a/packages/plugins/multi-app-share-collection/src/server/plugin.ts b/packages/plugins/multi-app-share-collection/src/server/plugin.ts index 0dbcfc15f..36fb0c027 100644 --- a/packages/plugins/multi-app-share-collection/src/server/plugin.ts +++ b/packages/plugins/multi-app-share-collection/src/server/plugin.ts @@ -1,5 +1,6 @@ import PluginMultiAppManager from '@nocobase/plugin-multi-app-manager'; import { Application, InstallOptions, Plugin } from '@nocobase/server'; +import lodash from 'lodash'; const subAppFilteredPlugins = ['multi-app-share-collection', 'multi-app-manager']; @@ -241,7 +242,11 @@ export class MultiAppShareCollectionPlugin extends Plugin { ); return { - database: databaseOptions, + database: lodash.merge(databaseOptions, { + dialectOptions: { + application_name: `nocobase.${appName}`, + }, + }), plugins: plugins.includes('nocobase') ? ['nocobase'] : plugins, resourcer: { prefix: '/api',