From ff9a33b28a45e0b0c8ccaff01a08c261e4efe6d9 Mon Sep 17 00:00:00 2001 From: xilesun <2013xile@gmail.com> Date: Sat, 12 Oct 2024 11:01:01 +0800 Subject: [PATCH] fix(locale): fix issues of syncing locale resources in cluster mode --- packages/core/server/src/locale/locale.ts | 8 ++++ .../plugin-localization/src/server/plugin.ts | 45 ++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/packages/core/server/src/locale/locale.ts b/packages/core/server/src/locale/locale.ts index f04a5ecad3..6611b858fe 100644 --- a/packages/core/server/src/locale/locale.ts +++ b/packages/core/server/src/locale/locale.ts @@ -38,6 +38,13 @@ export class Locale { this.app.log.debug('locale resource loaded', { submodule: 'locale', method: 'onAfterLoad' }); this.app.setMaintainingMessage('locale resource loaded'); }); + this.app.syncMessageManager.subscribe('localeManager', async (message) => { + switch (message.type) { + case 'reload': + await this.cache.reset(); + return; + } + }); } async load() { @@ -52,6 +59,7 @@ export class Locale { async reload() { await this.cache.reset(); + this.app.syncMessageManager.publish('localeManager', { type: 'reload' }); } setLocaleFn(name: string, fn: (lang: string) => Promise) { diff --git a/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts index 5bfa09fcdb..bdb06927dd 100644 --- a/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts @@ -9,9 +9,7 @@ import { Model } from '@nocobase/database'; import PluginUISchemaStorageServer from '@nocobase/plugin-ui-schema-storage'; -import { InstallOptions, OFFICIAL_PLUGIN_PREFIX, Plugin } from '@nocobase/server'; -import deepmerge from 'deepmerge'; -import { resolve } from 'path'; +import { InstallOptions, Plugin } from '@nocobase/server'; import localization from './actions/localization'; import localizationTexts from './actions/localizationTexts'; import Resources from './resources'; @@ -46,8 +44,16 @@ export class PluginLocalizationServer extends Plugin { text: title, }, }) - .then((res) => this.resources.updateCacheTexts([res])) - .catch((err) => {}); + .then((res) => { + this.resources.updateCacheTexts([res]); + this.sendSyncMessage({ + type: 'updateCacheTexts', + texts: [res], + }); + }) + .catch((err) => { + this.log.error(err); + }); }); } @@ -56,14 +62,12 @@ export class PluginLocalizationServer extends Plugin { beforeLoad() {} async load() { - await this.importCollections(resolve(__dirname, 'collections')); - - this.app.resource({ + this.app.resourceManager.define({ name: 'localizationTexts', actions: localizationTexts, }); - this.app.resource({ + this.app.resourceManager.define({ name: 'localization', actions: localization, }); @@ -103,8 +107,19 @@ export class PluginLocalizationServer extends Plugin { transaction: options?.transaction, }, ) - .then((newTexts) => this.resources.updateCacheTexts(newTexts, options?.transaction)) - .catch((err) => {}); + .then((newTexts) => { + this.resources.updateCacheTexts(newTexts, options?.transaction); + this.sendSyncMessage( + { + type: 'updateCacheTexts', + texts: newTexts, + }, + { transaction: options?.transaction }, + ); + }) + .catch((err) => { + this.log.error(err); + }); }); const cache = await this.app.cacheManager.createCache({ @@ -121,6 +136,14 @@ export class PluginLocalizationServer extends Plugin { }); } + async handleSyncMessage(message: any): Promise { + switch (message.type) { + case 'updateCacheTexts': + await this.resources.updateCacheTexts(message.texts); + return; + } + } + async install(options?: InstallOptions) {} async afterEnable() {}