@okf/ootils 1.5.5 → 1.6.0

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/node.js CHANGED
@@ -165,16 +165,11 @@ var init_Annotations = __esm({
165
165
  }
166
166
  });
167
167
 
168
- // src/models/PlatformConfigs.ts
169
- var PlatformConfigs_exports = {};
170
- __export(PlatformConfigs_exports, {
171
- default: () => PlatformConfigs_default
172
- });
173
- var import_mongoose2, platformConfigTypes, PlatformConfigsSchema, PlatformConfigs_default;
174
- var init_PlatformConfigs = __esm({
175
- "src/models/PlatformConfigs.ts"() {
168
+ // src/models/PlatformConfigs/platformConfigTypes.ts
169
+ var platformConfigTypes;
170
+ var init_platformConfigTypes = __esm({
171
+ "src/models/PlatformConfigs/platformConfigTypes.ts"() {
176
172
  "use strict";
177
- import_mongoose2 = __toESM(require("mongoose"));
178
173
  platformConfigTypes = [
179
174
  "roles",
180
175
  "nav",
@@ -184,6 +179,20 @@ var init_PlatformConfigs = __esm({
184
179
  "theme",
185
180
  "ai"
186
181
  ];
182
+ }
183
+ });
184
+
185
+ // src/models/PlatformConfigs/index.ts
186
+ var PlatformConfigs_exports = {};
187
+ __export(PlatformConfigs_exports, {
188
+ default: () => PlatformConfigs_default
189
+ });
190
+ var import_mongoose2, PlatformConfigsSchema, PlatformConfigs_default;
191
+ var init_PlatformConfigs = __esm({
192
+ "src/models/PlatformConfigs/index.ts"() {
193
+ "use strict";
194
+ import_mongoose2 = __toESM(require("mongoose"));
195
+ init_platformConfigTypes();
187
196
  PlatformConfigsSchema = new import_mongoose2.default.Schema(
188
197
  {
189
198
  type: {
@@ -583,46 +592,16 @@ var require_ElasticSearchConnector = __commonJS({
583
592
  }
584
593
  });
585
594
 
586
- // src/db/getDbByTenant.js
587
- var getDbByTenant_exports = {};
588
- __export(getDbByTenant_exports, {
589
- getDbByTenant: () => getDbByTenant
590
- });
591
- var import_mongoose4, import_MongoConnector, getDbByTenant;
592
- var init_getDbByTenant = __esm({
593
- "src/db/getDbByTenant.js"() {
594
- "use strict";
595
- import_mongoose4 = require("mongoose");
596
- import_MongoConnector = __toESM(require_MongoConnector());
597
- getDbByTenant = ({
598
- tenant,
599
- env: _env
600
- }) => {
601
- if (!tenant) throw new Error("tenant id has not been provided");
602
- const env = _env || import_MongoConnector.MongoConnector.getEnv();
603
- const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
604
- const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
605
- const { CLUSTER_NAME } = dbConfigs[env];
606
- const dbName = `${tenant}_${env}`;
607
- const connection = clusterConnections[CLUSTER_NAME];
608
- if (!connection) {
609
- throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
610
- }
611
- return connection.useDb(dbName, { useCache: true });
612
- };
613
- }
614
- });
615
-
616
595
  // src/models/AIChat.ts
617
- var import_mongoose5, AIChatSchema, AIChat_default;
596
+ var import_mongoose4, AIChatSchema, AIChat_default;
618
597
  var init_AIChat = __esm({
619
598
  "src/models/AIChat.ts"() {
620
599
  "use strict";
621
- import_mongoose5 = __toESM(require("mongoose"));
600
+ import_mongoose4 = __toESM(require("mongoose"));
622
601
  init_Annotations();
623
- AIChatSchema = new import_mongoose5.default.Schema(
602
+ AIChatSchema = new import_mongoose4.default.Schema(
624
603
  {
625
- userId: { type: import_mongoose5.default.Schema.Types.ObjectId, index: { unique: true } },
604
+ userId: { type: import_mongoose4.default.Schema.Types.ObjectId, index: { unique: true } },
626
605
  createdAt: Date,
627
606
  lastAcitivity: Date,
628
607
  messages: [
@@ -675,13 +654,43 @@ var init_models = __esm({
675
654
  }
676
655
  });
677
656
 
657
+ // src/db/getDbByTenant.js
658
+ var getDbByTenant_exports = {};
659
+ __export(getDbByTenant_exports, {
660
+ getDbByTenant: () => getDbByTenant
661
+ });
662
+ var import_mongoose5, import_MongoConnector, getDbByTenant;
663
+ var init_getDbByTenant = __esm({
664
+ "src/db/getDbByTenant.js"() {
665
+ "use strict";
666
+ import_mongoose5 = require("mongoose");
667
+ import_MongoConnector = __toESM(require_MongoConnector());
668
+ getDbByTenant = ({
669
+ tenant,
670
+ env: _env
671
+ }) => {
672
+ if (!tenant) throw new Error("tenant id has not been provided");
673
+ const env = _env || import_MongoConnector.MongoConnector.getEnv();
674
+ const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
675
+ const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
676
+ const { CLUSTER_NAME } = dbConfigs[env];
677
+ const dbName = `${tenant}_${env}`;
678
+ const connection = clusterConnections[CLUSTER_NAME];
679
+ if (!connection) {
680
+ throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
681
+ }
682
+ return connection.useDb(dbName, { useCache: true });
683
+ };
684
+ }
685
+ });
686
+
678
687
  // src/db/getModelByTenant.js
679
688
  var require_getModelByTenant = __commonJS({
680
689
  "src/db/getModelByTenant.js"(exports2, module2) {
681
690
  "use strict";
682
691
  var { getDbByTenant: getDbByTenant2 } = (init_getDbByTenant(), __toCommonJS(getDbByTenant_exports));
683
692
  var { AIChatSchema: AIChatSchema2, AnnotationSchema: AnnotationSchema2, PlatformConfigsSchema: PlatformConfigsSchema2, TplSchema: TplSchema2 } = (init_models(), __toCommonJS(models_exports));
684
- var getModelByTenant2 = ({ tenant, modelName, schema, env }) => {
693
+ var getModelByTenant3 = ({ tenant, modelName, schema, env }) => {
685
694
  if (!tenant) {
686
695
  throw new Error("tenant id has not been provided");
687
696
  }
@@ -691,32 +700,32 @@ var require_getModelByTenant = __commonJS({
691
700
  }
692
701
  return db.model(modelName);
693
702
  };
694
- var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant2({
703
+ var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant3({
695
704
  tenant,
696
705
  modelName: modelName || "annotations",
697
706
  schema: AnnotationSchema2,
698
707
  env
699
708
  });
700
- var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant2({
709
+ var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
701
710
  tenant,
702
711
  modelName: "platformConfigs",
703
712
  schema: PlatformConfigsSchema2,
704
713
  env
705
714
  });
706
- var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant2({
715
+ var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
707
716
  tenant,
708
717
  modelName: "tpl",
709
718
  schema: TplSchema2,
710
719
  env
711
720
  });
712
- var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant2({
721
+ var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
713
722
  tenant,
714
723
  modelName: "aiChat",
715
724
  schema: AIChatSchema2,
716
725
  env
717
726
  });
718
727
  module2.exports = {
719
- getModelByTenant: getModelByTenant2,
728
+ getModelByTenant: getModelByTenant3,
720
729
  getAIChatModelByTenant: getAIChatModelByTenant2,
721
730
  getAnnotationsModelByTenant: getAnnotationsModelByTenant2,
722
731
  getPlatformConfigsModelByTenant: getPlatformConfigsModelByTenant2,
@@ -779,6 +788,145 @@ var require_WorkerManager = __commonJS({
779
788
  }
780
789
  });
781
790
 
791
+ // src/bullmq/ProducerManager.js
792
+ var require_ProducerManager = __commonJS({
793
+ "src/bullmq/ProducerManager.js"(exports2, module2) {
794
+ "use strict";
795
+ var _ProducerManager = class _ProducerManager {
796
+ constructor(producerClasses = []) {
797
+ if (_ProducerManager.instance) {
798
+ return _ProducerManager.instance;
799
+ }
800
+ this.producerClasses = producerClasses;
801
+ this.activeProducers = /* @__PURE__ */ new Map();
802
+ _ProducerManager.instance = this;
803
+ }
804
+ // Static methods that delegate to singleton instance
805
+ static initialize(producerClasses = []) {
806
+ if (!this.instance) {
807
+ this.instance = new _ProducerManager(producerClasses);
808
+ }
809
+ return this.instance.initializeAllProducers();
810
+ }
811
+ static async addJob(producerClassName, jobName, jobData, opts = {}) {
812
+ if (!this.instance) {
813
+ throw new Error("ProducerManager not initialized. Call ProducerManager.initialize() first.");
814
+ }
815
+ return await this.instance.addJob(producerClassName, jobName, jobData, opts);
816
+ }
817
+ static async addBulkJobs(producerClassName, jobs) {
818
+ if (!this.instance) {
819
+ throw new Error("ProducerManager not initialized. Call ProducerManager.initialize() first.");
820
+ }
821
+ return await this.instance.addBulkJobs(producerClassName, jobs);
822
+ }
823
+ static async getStatus() {
824
+ if (!this.instance) {
825
+ throw new Error("ProducerManager not initialized. Call ProducerManager.initialize() first.");
826
+ }
827
+ return await this.instance.getStatus();
828
+ }
829
+ static async shutdown() {
830
+ if (this.instance) {
831
+ await this.instance.shutdown();
832
+ this.instance = null;
833
+ }
834
+ }
835
+ /**
836
+ * Initialize all producers as singletons
837
+ */
838
+ initializeAllProducers() {
839
+ if (this.producerClasses.length === 0) {
840
+ console.log("No producers provided to initialize");
841
+ return [];
842
+ }
843
+ console.log("\u{1F680} Initializing all producers...");
844
+ this.producerClasses.forEach((ProducerClass) => {
845
+ try {
846
+ const producerInstance = new ProducerClass();
847
+ this.activeProducers.set(ProducerClass.name, {
848
+ class: ProducerClass,
849
+ instance: producerInstance
850
+ });
851
+ console.log(`\u2705 Initialized ${ProducerClass.name}`);
852
+ } catch (error) {
853
+ console.error(`\u274C Failed to initialize ${ProducerClass.name}:`, error);
854
+ }
855
+ });
856
+ console.log(`\u{1F389} Successfully initialized ${this.activeProducers.size} producers`);
857
+ return Array.from(this.activeProducers.values());
858
+ }
859
+ /**
860
+ * Get a specific producer instance
861
+ */
862
+ getProducer(producerClassName) {
863
+ const producer = this.activeProducers.get(producerClassName);
864
+ if (!producer) {
865
+ throw new Error(`Producer ${producerClassName} not found. Make sure it's initialized.`);
866
+ }
867
+ return producer.instance;
868
+ }
869
+ /**
870
+ * Add a job using a specific producer
871
+ */
872
+ async addJob(producerClassName, jobData, opts = {}) {
873
+ const producer = this.getProducer(producerClassName);
874
+ return await producer.addJobWithName(jobData, opts);
875
+ }
876
+ /**
877
+ * Add bulk jobs using a specific producer
878
+ */
879
+ async addBulkJobs(producerClassName, jobs) {
880
+ const producer = this.getProducer(producerClassName);
881
+ return await producer.addBulkJobs(jobs);
882
+ }
883
+ /**
884
+ * Get status of all producers
885
+ */
886
+ async getStatus() {
887
+ const statusPromises = Array.from(this.activeProducers.entries()).map(
888
+ async ([name, { instance }]) => {
889
+ try {
890
+ const status = await instance.getStatus();
891
+ return {
892
+ name,
893
+ queueId: instance.config?.id || "unknown",
894
+ isActive: !!instance.queue,
895
+ ...status
896
+ };
897
+ } catch (error) {
898
+ return {
899
+ name,
900
+ queueId: instance.config?.id || "unknown",
901
+ isActive: false,
902
+ error: error.message
903
+ };
904
+ }
905
+ }
906
+ );
907
+ return await Promise.all(statusPromises);
908
+ }
909
+ /**
910
+ * Gracefully shutdown all producers
911
+ */
912
+ async shutdown() {
913
+ console.log("\u{1F6D1} Stopping all producers...");
914
+ const stopPromises = Array.from(this.activeProducers.entries()).map(
915
+ ([name, { instance }]) => instance.stop().catch(
916
+ (err) => console.error(`\u274C Error stopping ${name}:`, err)
917
+ )
918
+ );
919
+ await Promise.all(stopPromises);
920
+ this.activeProducers.clear();
921
+ console.log("\u2705 All producers stopped");
922
+ }
923
+ };
924
+ __publicField(_ProducerManager, "instance", null);
925
+ var ProducerManager2 = _ProducerManager;
926
+ module2.exports = { ProducerManager: ProducerManager2 };
927
+ }
928
+ });
929
+
782
930
  // src/bullmq/BaseProducer.js
783
931
  var require_BaseProducer = __commonJS({
784
932
  "src/bullmq/BaseProducer.js"(exports2, module2) {
@@ -861,7 +1009,8 @@ var require_BaseProducer = __commonJS({
861
1009
  async stop() {
862
1010
  if (this.queue) {
863
1011
  await this.queue.close();
864
- console.log(`\u{1F6D1} ${this.constructor.name} queue closed`);
1012
+ await this.queue.disconnect();
1013
+ console.log(`\u{1F6D1} ${this.constructor.name} queue closed & disconnected`);
865
1014
  }
866
1015
  }
867
1016
  };
@@ -921,7 +1070,8 @@ var require_BaseWorker = __commonJS({
921
1070
  async stop() {
922
1071
  if (this.worker) {
923
1072
  await this.worker.close();
924
- console.log(`\u{1F6D1} ${this.constructor.name} stopped`);
1073
+ await this.worker.disconnect();
1074
+ console.log(`\u{1F6D1} ${this.constructor.name} stopped & disconnected`);
925
1075
  }
926
1076
  }
927
1077
  };
@@ -956,7 +1106,7 @@ var init_GLOBAL_BULLMQ_CONFIG = __esm({
956
1106
  concurrency: 1,
957
1107
  // Process jobs one at a time to avoid race conditions
958
1108
  limiter: {
959
- max: 10,
1109
+ max: 5,
960
1110
  // Max 10 jobs per...
961
1111
  duration: 6e4
962
1112
  // ...60 seconds (rate limiting)
@@ -1028,7 +1178,7 @@ var init_GLOBAL_BULLMQ_CONFIG = __esm({
1028
1178
  workerConfig: {
1029
1179
  concurrency: 1,
1030
1180
  limiter: {
1031
- max: 5,
1181
+ max: 15,
1032
1182
  // Max 50 jobs per...
1033
1183
  duration: 6e4
1034
1184
  // ...60 seconds (higher throughput for chunking)
@@ -1074,8 +1224,8 @@ var init_GLOBAL_BULLMQ_CONFIG = __esm({
1074
1224
  workerConfig: {
1075
1225
  concurrency: 1,
1076
1226
  limiter: {
1077
- max: 5,
1078
- // Max 5 jobs per...
1227
+ max: 15,
1228
+ // (lets always keep this same as content enhance & embed since it comes immediately after)
1079
1229
  duration: 6e4
1080
1230
  // ...60 seconds (higher throughput for chunking)
1081
1231
  }
@@ -1097,8 +1247,8 @@ var init_GLOBAL_BULLMQ_CONFIG = __esm({
1097
1247
  workerConfig: {
1098
1248
  concurrency: 5,
1099
1249
  limiter: {
1100
- max: 5,
1101
- // Max 5 jobs per...
1250
+ max: 15,
1251
+ // (lets always keep this same as content enhance & embed since it comes immediately after)
1102
1252
  duration: 6e4
1103
1253
  // ...60 seconds (higher throughput for chunking)
1104
1254
  }
@@ -1112,10 +1262,10 @@ var init_GLOBAL_BULLMQ_CONFIG = __esm({
1112
1262
  var require_GET_GLOBAL_BULLMQ_CONFIG = __commonJS({
1113
1263
  "src/bullmq/GET_GLOBAL_BULLMQ_CONFIG.js"(exports2, module2) {
1114
1264
  "use strict";
1115
- var { default: Redis } = require("ioredis");
1265
+ var { default: Redis2 } = require("ioredis");
1116
1266
  var { BASE_BULLMQ_CONFIG: BASE_BULLMQ_CONFIG2 } = (init_GLOBAL_BULLMQ_CONFIG(), __toCommonJS(GLOBAL_BULLMQ_CONFIG_exports));
1117
1267
  function GET_GLOBAL_BULLMQ_CONFIG2({ env, redisCredentials }) {
1118
- const redisConnectionForBullMQ = new Redis({
1268
+ const redisConnectionForBullMQ = new Redis2({
1119
1269
  host: redisCredentials.REDIS_HOST,
1120
1270
  port: redisCredentials.REDIS_PORT,
1121
1271
  password: redisCredentials.REDIS_PASSWORD,
@@ -1153,21 +1303,19 @@ __export(node_exports, {
1153
1303
  GET_GLOBAL_BULLMQ_CONFIG: () => import_GET_GLOBAL_BULLMQ_CONFIG.GET_GLOBAL_BULLMQ_CONFIG,
1154
1304
  MongoConnector: () => import_MongoConnector2.MongoConnector,
1155
1305
  PlatformConfigsSchema: () => PlatformConfigs_default,
1306
+ ProducerManager: () => import_ProducerManager.ProducerManager,
1307
+ RedisCacheConnector: () => RedisCacheConnector,
1156
1308
  TplSchema: () => Tpl_default,
1157
1309
  WorkerManager: () => import_WorkerManager.WorkerManager,
1158
- connectToRedis: () => connectToRedis,
1159
1310
  deleteVal: () => deleteVal,
1160
1311
  extractAllBlocksFromTpl: () => extractAllBlocksFromTpl,
1161
1312
  genTagId: () => genTagId,
1162
- getAIChatModelByTenant: () => import_getModelByTenant.getAIChatModelByTenant,
1163
- getAIConfigs: () => getAIConfigs,
1164
- getAnnotationsModelByTenant: () => import_getModelByTenant.getAnnotationsModelByTenant,
1313
+ getAIChatModelByTenant: () => import_getModelByTenant2.getAIChatModelByTenant,
1314
+ getAnnotationsModelByTenant: () => import_getModelByTenant2.getAnnotationsModelByTenant,
1165
1315
  getDbByTenant: () => getDbByTenant,
1166
- getModelByTenant: () => import_getModelByTenant.getModelByTenant,
1167
- getPlatformConfigsModelByTenant: () => import_getModelByTenant.getPlatformConfigsModelByTenant,
1168
- getRedisClient: () => getRedisClient,
1169
- getTpl: () => getTpl,
1170
- getTplModelByTenant: () => import_getModelByTenant.getTplModelByTenant,
1316
+ getModelByTenant: () => import_getModelByTenant2.getModelByTenant,
1317
+ getPlatformConfigsModelByTenant: () => import_getModelByTenant2.getPlatformConfigsModelByTenant,
1318
+ getTplModelByTenant: () => import_getModelByTenant2.getTplModelByTenant,
1171
1319
  getVal: () => getVal,
1172
1320
  initializeGlobalConfig: () => initializeGlobalConfig,
1173
1321
  recursivelyExtractBlocks: () => _recursExtractBlocks,
@@ -1455,6 +1603,319 @@ var _extractBlocksFromSomeBuilders = ({
1455
1603
  // src/node.ts
1456
1604
  var import_MongoConnector2 = __toESM(require_MongoConnector());
1457
1605
  var import_ElasticSearchConnector = __toESM(require_ElasticSearchConnector());
1606
+
1607
+ // src/RedisCacheConnector.js
1608
+ var import_ioredis = __toESM(require("ioredis"));
1609
+ init_models();
1610
+ var import_getModelByTenant = __toESM(require_getModelByTenant());
1611
+ init_platformConfigTypes();
1612
+ var BASE_SETTINGS_FOR_CONFIGS_CACHE = [
1613
+ {
1614
+ modelName: "tpl",
1615
+ collectionName: "tpls",
1616
+ schema: Tpl_default,
1617
+ defaultQuery: [
1618
+ {
1619
+ status: {
1620
+ $ne: "unpublished"
1621
+ }
1622
+ },
1623
+ {
1624
+ drafts: 0
1625
+ }
1626
+ ],
1627
+ typeKey: "kp_content_type"
1628
+ },
1629
+ {
1630
+ modelName: "platformConfigs",
1631
+ collectionName: "platformConfigs",
1632
+ schema: PlatformConfigs_default,
1633
+ defaultQuery: [],
1634
+ typeKey: "type"
1635
+ }
1636
+ ];
1637
+ var RedisCacheConnector = class _RedisCacheConnector {
1638
+ constructor(options = {}) {
1639
+ /**
1640
+ * Initialize Redis client
1641
+ */
1642
+ __publicField(this, "connect", async () => {
1643
+ try {
1644
+ this.client = new import_ioredis.default({
1645
+ host: this.host,
1646
+ port: this.port,
1647
+ password: this.password
1648
+ });
1649
+ this.client.on("connect", () => {
1650
+ console.log("\u2705 Connected to our redis cache instance!");
1651
+ });
1652
+ await new Promise((resolve, reject) => {
1653
+ this.client.on("ready", resolve);
1654
+ this.client.on("error", reject);
1655
+ });
1656
+ return this.client;
1657
+ } catch (err) {
1658
+ console.error(err.message);
1659
+ process.exit(1);
1660
+ }
1661
+ });
1662
+ if (_RedisCacheConnector.instance) {
1663
+ throw new Error(
1664
+ "RedisCacheConnector instance already exists. Use RedisCacheConnector.getInstance() instead."
1665
+ );
1666
+ }
1667
+ this.client = null;
1668
+ this.host = options.host;
1669
+ this.env = options.env;
1670
+ this.port = options.port;
1671
+ this.password = options.password;
1672
+ _RedisCacheConnector.instance = this;
1673
+ if (!this.host) {
1674
+ throw new Error("Host must be provided in constructor options");
1675
+ }
1676
+ if (!this.port) {
1677
+ throw new Error("Port must be provided in constructor options");
1678
+ }
1679
+ if (!this.password) {
1680
+ throw new Error("Password must be provided in constructor options");
1681
+ }
1682
+ if (!this.env) {
1683
+ throw new Error("Env must be provided in constructor options");
1684
+ }
1685
+ }
1686
+ static getEnv() {
1687
+ if (!_RedisCacheConnector.instance) {
1688
+ throw new Error("RedisCacheConnector not initialized");
1689
+ }
1690
+ return _RedisCacheConnector.instance.env;
1691
+ }
1692
+ // Static method to get the instance
1693
+ static getInstance() {
1694
+ if (!_RedisCacheConnector.instance) {
1695
+ throw new Error("RedisCacheConnector not initialized");
1696
+ }
1697
+ return _RedisCacheConnector.instance;
1698
+ }
1699
+ // Static method to get the client
1700
+ static getClient() {
1701
+ if (!_RedisCacheConnector.instance || !_RedisCacheConnector.instance.client) {
1702
+ throw new Error(
1703
+ "RedisCacheConnector not initialized or client not connected"
1704
+ );
1705
+ }
1706
+ return _RedisCacheConnector.instance.client;
1707
+ }
1708
+ /**
1709
+ * Find documents in Redis cache
1710
+ * @param {Object} params - Search parameters
1711
+ * @param {string} params.tenant - The tenant identifier
1712
+ * @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
1713
+ * @param {string} [params.type] - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string. If omitted, returns all documents for the collection.
1714
+ * @returns {Promise<Array>} Array of matching documents
1715
+ * @see {platformConfigTypes} for valid platformConfigs type values
1716
+ */
1717
+ static async findConfigsInCache({ tenant, modelName, type }) {
1718
+ try {
1719
+ const client = this.getClient();
1720
+ const environment = this.getEnv();
1721
+ const keyPrefix = `${environment}:${tenant}:${modelName}:`;
1722
+ const keys = await client.keys(`${keyPrefix}${type || "*"}`);
1723
+ if (keys.length === 0) {
1724
+ const { schema, typeKey } = BASE_SETTINGS_FOR_CONFIGS_CACHE.find(
1725
+ (obj) => obj.modelName === modelName
1726
+ );
1727
+ const model = (0, import_getModelByTenant.getModelByTenant)({
1728
+ tenant,
1729
+ env: environment,
1730
+ modelName,
1731
+ schema
1732
+ });
1733
+ let findQuery = {};
1734
+ if (type) {
1735
+ findQuery = { [typeKey]: type };
1736
+ }
1737
+ const data = await model.find(findQuery);
1738
+ await Promise.all(
1739
+ data.map(
1740
+ (doc) => this.setOneConfigInCache({
1741
+ tenant,
1742
+ modelName,
1743
+ type: doc[typeKey],
1744
+ val: doc
1745
+ })
1746
+ )
1747
+ );
1748
+ return data;
1749
+ }
1750
+ const cachedDocs = await Promise.all(
1751
+ keys.map(async (key) => {
1752
+ const value = await client.get(key);
1753
+ return JSON.parse(value);
1754
+ })
1755
+ );
1756
+ return cachedDocs;
1757
+ } catch (error) {
1758
+ console.error("Error in findConfigsInCache:", error);
1759
+ throw error;
1760
+ }
1761
+ }
1762
+ /**
1763
+ * Find a single document in Redis cache (returns first match)
1764
+ * @param {Object} params - Search parameters
1765
+ * @param {string} params.tenant - The tenant identifier
1766
+ * @param {'platformConfigs'|'tpl'} params.modelName - The collection to search in
1767
+ * @param {string} [params.type] - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string. If omitted, returns first document of any type.
1768
+ * @returns {Promise<Object|undefined>} First matching document or undefined if none found
1769
+ * @see {platformConfigTypes} for valid platformConfigs type values
1770
+ */
1771
+ static async findOneConfigInCache({ tenant, modelName, type }) {
1772
+ const cacheData = await this.findConfigsInCache({
1773
+ tenant,
1774
+ modelName,
1775
+ type
1776
+ });
1777
+ return cacheData?.[0];
1778
+ }
1779
+ /**
1780
+ * Generate Redis key for config caching
1781
+ * @private
1782
+ * @param {Object} params - Parameters for key generation
1783
+ * @param {string} params.tenant - The tenant identifier
1784
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
1785
+ * @param {string} params.type - The document type
1786
+ * @param {string} [params.env] - Environment (defaults to current env)
1787
+ * @returns {string} The Redis key
1788
+ */
1789
+ static _getRedisKeyForConfig({ tenant, modelName, type, env }) {
1790
+ const environment = env || this.getEnv();
1791
+ return `${environment}:${tenant}:${modelName}:${type}`;
1792
+ }
1793
+ /**
1794
+ * Set a single config document in Redis cache
1795
+ * @param {Object} params - Cache parameters
1796
+ * @param {string} params.tenant - The tenant identifier
1797
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
1798
+ * @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
1799
+ * @param {Object} params.val - The value to cache
1800
+ * @param {string} [params.env] - Environment (defaults to current env)
1801
+ * @returns {Promise<void>}
1802
+ * @see {platformConfigTypes} for valid platformConfigs type values
1803
+ */
1804
+ static async setOneConfigInCache({ tenant, modelName, type, val, env }) {
1805
+ const client = this.getClient();
1806
+ const cacheKey = this._getRedisKeyForConfig({
1807
+ tenant,
1808
+ modelName,
1809
+ type,
1810
+ env
1811
+ });
1812
+ await client.set(cacheKey, JSON.stringify(val));
1813
+ }
1814
+ /**
1815
+ * Invalidate (delete) a config from Redis cache and optionally set a new value
1816
+ * @param {Object} params - Cache parameters
1817
+ * @param {string} params.tenant - The tenant identifier
1818
+ * @param {'platformConfigs'|'tpl'} params.modelName - The model/collection name
1819
+ * @param {string} params.type - When modelName='platformConfigs': one of platformConfigTypes array values; when modelName='tpl': content type string
1820
+ * @param {Object} [params.newVal] - New value to set after deletion
1821
+ * @param {string} [params.env] - Environment (defaults to current env)
1822
+ * @returns {Promise<void>}
1823
+ * @see {platformConfigTypes} for valid platformConfigs type values
1824
+ * @note According to the current usecases this function is called only after a database operation.
1825
+ * We should include this function call within the MongoDB transaction.
1826
+ * Otherwise, if the database operation succeeds but cache deletion fails, the cache will hold the outdated data.
1827
+ * We can ignore setAsync failure as we are checking db also if cache is not present (inside findInCache function)
1828
+ */
1829
+ static async invalidateOneConfigInCache({
1830
+ tenant,
1831
+ modelName,
1832
+ type,
1833
+ newVal,
1834
+ env
1835
+ }) {
1836
+ const client = this.getClient();
1837
+ const cacheKey = this._getRedisKeyForConfig({
1838
+ tenant,
1839
+ modelName,
1840
+ type,
1841
+ env
1842
+ });
1843
+ await client.del(cacheKey);
1844
+ if (newVal) {
1845
+ try {
1846
+ await client.set(cacheKey, JSON.stringify(newVal));
1847
+ } catch (err) {
1848
+ console.log("invalidateCache - Cache set failed: ", err);
1849
+ }
1850
+ }
1851
+ }
1852
+ /**
1853
+ * Load all collections and templates into Redis cache for all tenants
1854
+ * Clears existing cache and rebuilds it from database
1855
+ * @returns {Promise<void>}
1856
+ * @param {string} [params.tenant] - Tenant (either a single tenant or an array of tenants for whom the config cache should be refreshed)
1857
+ * @static
1858
+ */
1859
+ static async loadTplsAndPlatformConfigsToCache({ tenant }) {
1860
+ try {
1861
+ const client = this.getClient();
1862
+ const environment = this.getEnv();
1863
+ if (!tenant) throw new Error("No tenant/s defined to recache");
1864
+ console.log(`Loading configs and templates into cache...`);
1865
+ const tenantCollectionPromises = toArray(tenant).map(async (tenant2) => {
1866
+ return Promise.all(
1867
+ BASE_SETTINGS_FOR_CONFIGS_CACHE.map(
1868
+ async ({ modelName, schema, defaultQuery, typeKey }) => {
1869
+ const model = (0, import_getModelByTenant.getModelByTenant)({
1870
+ tenant: tenant2,
1871
+ env: environment,
1872
+ modelName,
1873
+ schema
1874
+ });
1875
+ const keys = await client.keys(
1876
+ `${environment}:${tenant2}:${modelName}:*`
1877
+ );
1878
+ if (keys.length) await client.del(keys);
1879
+ const documents = await model.find(...defaultQuery);
1880
+ await Promise.all(
1881
+ documents.map(async (doc) => {
1882
+ const type = doc[typeKey];
1883
+ await this.setOneConfigInCache({
1884
+ tenant: tenant2,
1885
+ modelName,
1886
+ type,
1887
+ val: doc
1888
+ });
1889
+ })
1890
+ );
1891
+ console.log(
1892
+ `Successfully cached ${documents.length} documents from ${modelName} for tenant: ${tenant2} | env: ${environment}`
1893
+ );
1894
+ }
1895
+ )
1896
+ );
1897
+ });
1898
+ await Promise.all(tenantCollectionPromises);
1899
+ console.log(`Finished caching for all tenants`);
1900
+ } catch (error) {
1901
+ console.error("Error in loadTplsAndPlatformConfigsToCache: ", error);
1902
+ throw error;
1903
+ }
1904
+ }
1905
+ /**
1906
+ * Close the Redis client
1907
+ */
1908
+ async close() {
1909
+ if (this.client) {
1910
+ await this.client.quit();
1911
+ console.log("\u2705 Redis client closed");
1912
+ this.client = null;
1913
+ }
1914
+ }
1915
+ };
1916
+ RedisCacheConnector.instance = null;
1917
+
1918
+ // src/node.ts
1458
1919
  init_getDbByTenant();
1459
1920
 
1460
1921
  // src/db/getGlobalConfig.ts
@@ -1479,121 +1940,10 @@ var updateGlobalConfig = (updates) => {
1479
1940
  };
1480
1941
 
1481
1942
  // src/node.ts
1482
- var import_getModelByTenant = __toESM(require_getModelByTenant());
1483
-
1484
- // src/redis/functions.ts
1485
- init_getDbByTenant();
1486
-
1487
- // src/redis/index.ts
1488
- var import_ioredis = __toESM(require("ioredis"));
1489
- var redisClient = null;
1490
- var REDIS_CONFIG = {
1491
- port: Number(process.env.REDIS_PORT),
1492
- host: process.env.REDIS_HOST || "",
1493
- username: process.env.REDIS_USERNAME || "",
1494
- password: process.env.REDIS_PASSWORD || ""
1495
- };
1496
- var connectToRedis = async () => {
1497
- try {
1498
- redisClient = new import_ioredis.default(REDIS_CONFIG);
1499
- await redisClient.ping();
1500
- console.log("Redis connected successfully");
1501
- } catch (error) {
1502
- console.error(
1503
- "Redis connection failed:",
1504
- error instanceof Error && error.message
1505
- );
1506
- if (redisClient) {
1507
- redisClient.disconnect();
1508
- }
1509
- throw error;
1510
- }
1511
- };
1512
- var getRedisClient = () => {
1513
- if (!redisClient) {
1514
- throw new Error("Redis client not initialized. Call connectToRedis first.");
1515
- }
1516
- return redisClient;
1517
- };
1518
-
1519
- // src/redis/functions.ts
1520
- var findInCache = async ({
1521
- tenant,
1522
- modelName,
1523
- type,
1524
- query = {}
1525
- }) => {
1526
- const key = `${process.env.ENV}:${tenant}:${modelName}:${type}`;
1527
- try {
1528
- const redisClient2 = getRedisClient();
1529
- const value = await redisClient2.get(key);
1530
- if (value) {
1531
- return JSON.parse(value);
1532
- }
1533
- } catch (error) {
1534
- console.warn(
1535
- `redis read failed for key ${key}`,
1536
- error instanceof Error && error.message
1537
- );
1538
- }
1539
- try {
1540
- const collection = getDbByTenant({
1541
- tenant,
1542
- env: process.env.ENV
1543
- }).collection(modelName);
1544
- const value = await collection.findOne(query);
1545
- if (!value) {
1546
- throw new Error(`${type} value not found in ${modelName} for ${tenant}`);
1547
- }
1548
- try {
1549
- const redisClient2 = getRedisClient();
1550
- await redisClient2.set(key, JSON.stringify(value));
1551
- } catch (error) {
1552
- console.warn(
1553
- `redis write failed for key ${key}:`,
1554
- error instanceof Error && error.message
1555
- );
1556
- }
1557
- return value;
1558
- } catch (error) {
1559
- throw new Error(
1560
- `db read failed: ${error instanceof Error && error.message}`
1561
- );
1562
- }
1563
- };
1564
- var getTpl = async ({ name, tenant }) => {
1565
- if (!name || !tenant) {
1566
- throw new Error("Missing required parameters: name or tenant");
1567
- }
1568
- return await findInCache({
1569
- modelName: "tpl",
1570
- type: name,
1571
- tenant,
1572
- query: {
1573
- kp_content_type: name,
1574
- status: { $in: ["published", "editPublished"] }
1575
- }
1576
- });
1577
- };
1578
- var getAIConfigs = async ({
1579
- tenant
1580
- }) => {
1581
- if (!tenant) {
1582
- throw new Error("Missing required parameter: tenant");
1583
- }
1584
- return await findInCache({
1585
- modelName: "platformConfigs",
1586
- type: "ai",
1587
- tenant,
1588
- query: {
1589
- type: "ai"
1590
- }
1591
- });
1592
- };
1593
-
1594
- // src/node.ts
1943
+ var import_getModelByTenant2 = __toESM(require_getModelByTenant());
1595
1944
  init_models();
1596
1945
  var import_WorkerManager = __toESM(require_WorkerManager());
1946
+ var import_ProducerManager = __toESM(require_ProducerManager());
1597
1947
  var import_BaseProducer = __toESM(require_BaseProducer());
1598
1948
  var import_BaseWorker = __toESM(require_BaseWorker());
1599
1949
  var import_GET_GLOBAL_BULLMQ_CONFIG = __toESM(require_GET_GLOBAL_BULLMQ_CONFIG());
@@ -1607,20 +1957,18 @@ var import_GET_GLOBAL_BULLMQ_CONFIG = __toESM(require_GET_GLOBAL_BULLMQ_CONFIG()
1607
1957
  GET_GLOBAL_BULLMQ_CONFIG,
1608
1958
  MongoConnector,
1609
1959
  PlatformConfigsSchema,
1960
+ ProducerManager,
1961
+ RedisCacheConnector,
1610
1962
  TplSchema,
1611
1963
  WorkerManager,
1612
- connectToRedis,
1613
1964
  deleteVal,
1614
1965
  extractAllBlocksFromTpl,
1615
1966
  genTagId,
1616
1967
  getAIChatModelByTenant,
1617
- getAIConfigs,
1618
1968
  getAnnotationsModelByTenant,
1619
1969
  getDbByTenant,
1620
1970
  getModelByTenant,
1621
1971
  getPlatformConfigsModelByTenant,
1622
- getRedisClient,
1623
- getTpl,
1624
1972
  getTplModelByTenant,
1625
1973
  getVal,
1626
1974
  initializeGlobalConfig,