diff --git a/.changeset/dirty-trees-smoke.md b/.changeset/dirty-trees-smoke.md new file mode 100644 index 0000000000..ef40a4e9ef --- /dev/null +++ b/.changeset/dirty-trees-smoke.md @@ -0,0 +1,5 @@ +--- +"@medusajs/index": patch +--- + +feat(index): Only run the synchronisation in worker/shared mode diff --git a/packages/modules/index/src/services/index-module-service.ts b/packages/modules/index/src/services/index-module-service.ts index c0ebec02e8..17252c2bd2 100644 --- a/packages/modules/index/src/services/index-module-service.ts +++ b/packages/modules/index/src/services/index-module-service.ts @@ -40,6 +40,8 @@ export default class IndexModuleService extends ModulesSdkUtils.MedusaService({}) implements IndexTypes.IIndexService { + #isWorkerMode: boolean = false + private readonly container_: InjectedDependencies private readonly moduleOptions_: IndexTypes.IndexModuleOptions @@ -83,6 +85,8 @@ export default class IndexModuleService this.moduleOptions_ = (moduleDeclaration.options ?? moduleDeclaration) as unknown as IndexTypes.IndexModuleOptions + this.#isWorkerMode = moduleDeclaration.worker_mode !== "server" + const { [Modules.EVENT_BUS]: eventBusModuleService, storageProviderCtr, @@ -126,22 +130,29 @@ export default class IndexModuleService await gqlSchemaToTypes(executableSchema!) - this.dataSynchronizer_.onApplicationStart({ - schemaObjectRepresentation: this.schemaObjectRepresentation_, - storageProvider: this.storageProvider_, - }) + /** + * Only run the data synchronization in worker mode + */ - const configurationChecker = new Configuration({ - logger: this.logger_, - schemaObjectRepresentation: this.schemaObjectRepresentation_, - indexMetadataService: this.indexMetadataService_, - indexSyncService: this.indexSyncService_, - dataSynchronizer: this.dataSynchronizer_, - }) - const entitiesMetadataChanged = await configurationChecker.checkChanges() + if (this.#isWorkerMode) { + this.dataSynchronizer_.onApplicationStart({ + schemaObjectRepresentation: this.schemaObjectRepresentation_, + storageProvider: this.storageProvider_, + }) - if (entitiesMetadataChanged.length) { - await this.dataSynchronizer_.syncEntities(entitiesMetadataChanged) + const configurationChecker = new Configuration({ + logger: this.logger_, + schemaObjectRepresentation: this.schemaObjectRepresentation_, + indexMetadataService: this.indexMetadataService_, + indexSyncService: this.indexSyncService_, + dataSynchronizer: this.dataSynchronizer_, + }) + const entitiesMetadataChanged = + await configurationChecker.checkChanges() + + if (entitiesMetadataChanged.length) { + await this.dataSynchronizer_.syncEntities(entitiesMetadataChanged) + } } } catch (e) { this.logger_.error(e)