@cloudnux/local-cloud-provider 0.7.0 → 0.11.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.
@@ -20,7 +20,6 @@ interface QueueService {
20
20
  dlq: QueueMessage[];
21
21
  timeoutId: NodeJS.Timeout | null;
22
22
  processingBatch: boolean;
23
- activeProcessing: number;
24
23
  module?: string;
25
24
  }
26
25
  interface QueueConfig {
@@ -43,7 +42,6 @@ interface QueueSummary {
43
42
  processing: number;
44
43
  dlq: number;
45
44
  isProcessing: boolean;
46
- activeProcessing: number;
47
45
  configuration: {
48
46
  batchSize: number;
49
47
  batchWindowMs: number;
@@ -94,6 +92,7 @@ interface QueueManager {
94
92
  interface QueueDecoratorOptions {
95
93
  config: QueueConfig;
96
94
  queues: Record<string, QueueService>;
95
+ dirtyQueues: Set<string>;
97
96
  saveQueueState?: (queueName: string, queueService: QueueService) => Promise<void>;
98
97
  loadQueueState?: (queueName: string) => Promise<void>;
99
98
  scheduleProcessing: (queueName: string, queueService: QueueService) => void;
@@ -866,12 +866,14 @@ var errorToString = (error) => {
866
866
 
867
867
  // ../../utils/src/logging/index.ts
868
868
  var currentLogLevel = logLevels[env("LOG_LEVEL")?.toLowerCase()] ?? logLevels.info;
869
+ var module = "default";
870
+ var requestId = "";
869
871
  var logger = {
870
872
  fatal: (message, meta) => {
871
873
  if (currentLogLevel >= logLevels.fatal) {
872
874
  console.error(
873
875
  `[${(/* @__PURE__ */ new Date()).toTimeString()}]`,
874
- `${chalk.bgRed.white(" fatal ")}${EOL}`,
876
+ `${chalk.bgRed.white(" fatal ")} - ${module || "unknown module"} - ${requestId || "no request ID"} - ${EOL}`,
875
877
  errorToString(message),
876
878
  meta ? `${EOL}${JSON.stringify(meta, null, 2)}` : ""
877
879
  );
@@ -881,7 +883,7 @@ var logger = {
881
883
  if (currentLogLevel >= logLevels.error) {
882
884
  console.error(
883
885
  `[${(/* @__PURE__ */ new Date()).toTimeString()}]`,
884
- `${chalk.bgRed.white(" error ")}${EOL}`,
886
+ `${chalk.bgRed.white(" error ")} - ${module || "unknown module"} - ${requestId || "no request ID"} - ${EOL}`,
885
887
  errorToString(message),
886
888
  meta ? `${EOL}${JSON.stringify(meta, null, 2)}` : ""
887
889
  );
@@ -891,7 +893,7 @@ var logger = {
891
893
  if (currentLogLevel >= logLevels.warn) {
892
894
  console.warn(
893
895
  `[${(/* @__PURE__ */ new Date()).toTimeString()}]`,
894
- `${chalk.bgYellow.black(" warn ")}${EOL}`,
896
+ `${chalk.bgYellow.black(" warn ")} - ${module || "unknown module"} - ${requestId || "no request ID"} - ${EOL}`,
895
897
  errorToString(message),
896
898
  meta ? `${EOL}${JSON.stringify(meta, null, 2)}` : ""
897
899
  );
@@ -901,7 +903,7 @@ var logger = {
901
903
  if (currentLogLevel >= logLevels.info) {
902
904
  console.info(
903
905
  `[${(/* @__PURE__ */ new Date()).toTimeString()}]`,
904
- `${chalk.bgBlue.white(" info ")}${EOL}`,
906
+ `${chalk.bgBlue.white(" info ")} - ${module || "unknown module"} - ${requestId || "no request ID"} - ${EOL}`,
905
907
  message,
906
908
  meta ? `${EOL}${JSON.stringify(meta, null, 2)}` : ""
907
909
  );
@@ -911,7 +913,7 @@ var logger = {
911
913
  if (currentLogLevel >= logLevels.debug) {
912
914
  console.debug(
913
915
  `[${(/* @__PURE__ */ new Date()).toTimeString()}]`,
914
- `${chalk.bgWhite.black(" debug ")}${EOL}`,
916
+ `${chalk.bgWhite.black(" debug ")} - ${module || "unknown module"} - ${requestId || "no request ID"} - ${EOL}`,
915
917
  message,
916
918
  meta ? `${EOL}${JSON.stringify(meta, null, 2)}` : ""
917
919
  );
@@ -943,15 +945,14 @@ var mergeConfig = (defaultConfig, userConfig) => ({
943
945
  ...userConfig?.persistence || {}
944
946
  }
945
947
  });
946
- var createQueueService = (handler, module) => ({
948
+ var createQueueService = (handler, module2) => ({
947
949
  handler,
948
950
  incoming: [],
949
951
  processing: [],
950
952
  dlq: [],
951
953
  timeoutId: null,
952
954
  processingBatch: false,
953
- activeProcessing: 0,
954
- module
955
+ module: module2
955
956
  });
956
957
  var createQueueMessage = (body, headers) => {
957
958
  const id = Date.now().toString() + Math.random().toString(36).substring(2, 7);
@@ -997,7 +998,6 @@ var createQueueSummary = (queueService, config) => ({
997
998
  processing: queueService.processing.length,
998
999
  dlq: queueService.dlq.length,
999
1000
  isProcessing: queueService.processingBatch,
1000
- activeProcessing: queueService.activeProcessing,
1001
1001
  configuration: {
1002
1002
  batchSize: config.batchSize,
1003
1003
  batchWindowMs: config.batchWindowMs,
@@ -1039,7 +1039,7 @@ var logError = (message, messageId, queueName, error) => {
1039
1039
  logger.error(`${logSymbols.error} ${chalk2.red(message)} ${chalk2.yellow(messageId)} in queue ${chalk2.magenta(queueName)}: ${error}`);
1040
1040
  };
1041
1041
  var logRetryScheduled = (messageId, delayMs) => {
1042
- logger.info(`${chalk2.blue("\u23F1\uFE0F Scheduling retry")} for message ${chalk2.yellow(messageId)} in ${chalk2.cyan(delayMs)}ms`);
1042
+ logger.debug(`${chalk2.blue("\u23F1\uFE0F Scheduling retry")} for message ${chalk2.yellow(messageId)} in ${chalk2.cyan(delayMs)}ms`);
1043
1043
  };
1044
1044
  var logDLQOperation = (operation, count, queueName) => {
1045
1045
  logger.warn(`${logSymbols.warning} ${chalk2.yellow(operation)} ${chalk2.red(count)} messages from DLQ for ${chalk2.green(queueName)}`);
@@ -1072,7 +1072,7 @@ var handleProcessingError = async (queueName, message, queueService, error, conf
1072
1072
  }, delayMs);
1073
1073
  }
1074
1074
  };
1075
- var createBatchProcessor = (processMessage, config, saveQueueState) => async (queueName, queueService) => {
1075
+ var createBatchProcessor = (processMessage, config, dirtyQueues) => async (queueName, queueService) => {
1076
1076
  if (queueService.timeoutId) {
1077
1077
  clearTimeout(queueService.timeoutId);
1078
1078
  queueService.timeoutId = null;
@@ -1090,8 +1090,8 @@ var createBatchProcessor = (processMessage, config, saveQueueState) => async (qu
1090
1090
  await Promise.all(messagesToProcess.map((message) => {
1091
1091
  return processMessage(queueName, message, queueService);
1092
1092
  }));
1093
- if (config.persistence.enabled && saveQueueState) {
1094
- await saveQueueState(queueName);
1093
+ if (config.persistence.enabled && dirtyQueues) {
1094
+ dirtyQueues.add(queueName);
1095
1095
  }
1096
1096
  } finally {
1097
1097
  queueService.processingBatch = false;
@@ -1133,18 +1133,21 @@ var restoreDates = (messages) => {
1133
1133
  if (msg.failedAt) msg.failedAt = new Date(msg.failedAt);
1134
1134
  });
1135
1135
  };
1136
- var createPersistenceInitializer = (config, loadAllQueueStates, saveAllQueueStates) => async () => {
1136
+ var createPersistenceInitializer = (config, loadAllQueueStates, saveDirtyQueueStates, saveAllQueueStates, intervalIdHolder) => async () => {
1137
1137
  try {
1138
1138
  await fs.mkdir(config.persistence.directory, { recursive: true });
1139
1139
  if (config.persistence.loadOnStartup) {
1140
1140
  await loadAllQueueStates();
1141
1141
  }
1142
1142
  if (config.persistence.saveInterval > 0) {
1143
- setInterval(saveAllQueueStates, config.persistence.saveInterval);
1143
+ intervalIdHolder.id = setInterval(saveDirtyQueueStates, config.persistence.saveInterval);
1144
1144
  }
1145
1145
  if (config.persistence.saveOnShutdown) {
1146
1146
  const shutdownHandler = async () => {
1147
1147
  logger.debug("Saving queue state before shutdown...");
1148
+ if (intervalIdHolder.id) {
1149
+ clearInterval(intervalIdHolder.id);
1150
+ }
1148
1151
  await saveAllQueueStates();
1149
1152
  process.exit(0);
1150
1153
  };
@@ -1153,7 +1156,8 @@ var createPersistenceInitializer = (config, loadAllQueueStates, saveAllQueueStat
1153
1156
  }
1154
1157
  logger.debug(`${logSymbols2.success} ${chalk3.green("Queue persistence initialized:")} ${chalk3.yellow(config.persistence.directory)}`);
1155
1158
  } catch (error) {
1156
- logger.error(`${logSymbols2.error} ${chalk3.red("Failed to initialize queue persistence:")} ${chalk3.yellow(error.message)}`);
1159
+ console.log("[logger]", logger);
1160
+ logger.error(`${logSymbols2.error} ${chalk3.red("Failed to initialize queue persistence:")} ${chalk3.yellow(error?.message)}`);
1157
1161
  }
1158
1162
  };
1159
1163
  var createQueueStateSaver = (config) => async (queueName, queueService) => {
@@ -1165,9 +1169,9 @@ var createQueueStateSaver = (config) => async (queueName, queueService) => {
1165
1169
  const tempFilePath = path.join(config.persistence.directory, `${queueName}.${now}temp.json`);
1166
1170
  await fs.writeFile(tempFilePath, JSON.stringify(queueData, null, 2), "utf8");
1167
1171
  await fs.rename(tempFilePath, queueFilePath);
1168
- logger.info(`${logSymbols2.info} ${chalk3.blue("Queue state saved:")} ${chalk3.green(queueName)}`);
1172
+ logger.debug(`${logSymbols2.info} ${chalk3.blue("Queue state saved:")} ${chalk3.green(queueName)}`);
1169
1173
  } catch (error) {
1170
- logger.error(`Failed to save queue state for ${queueName}:`, error);
1174
+ logger.error(`Failed to save queue state for ${queueName}:`, error?.message);
1171
1175
  }
1172
1176
  };
1173
1177
  var createAllQueuesStateSaver = (config, saveQueueState) => (queues) => async () => {
@@ -1177,7 +1181,21 @@ var createAllQueuesStateSaver = (config, saveQueueState) => (queues) => async ()
1177
1181
  }
1178
1182
  logger.debug(`${logSymbols2.success} ${chalk3.green("All queue states saved to")} ${chalk3.yellow(config.persistence.directory)}`);
1179
1183
  } catch (error) {
1180
- logger.error(`${logSymbols2.error} ${chalk3.red("Failed to save all queue states:")} ${chalk3.yellow(error.message)}`);
1184
+ logger.error(`${logSymbols2.error} ${chalk3.red("Failed to save all queue states:")} ${chalk3.yellow(error?.message)}`);
1185
+ }
1186
+ };
1187
+ var createDirtyQueuesStateSaver = (saveQueueState, dirtyQueues) => (queues) => async () => {
1188
+ if (dirtyQueues.size === 0) return;
1189
+ try {
1190
+ const queueNames = [...dirtyQueues];
1191
+ dirtyQueues.clear();
1192
+ for (const queueName of queueNames) {
1193
+ if (queues[queueName]) {
1194
+ await saveQueueState(queueName, queues[queueName]);
1195
+ }
1196
+ }
1197
+ } catch (error) {
1198
+ logger.error(`${logSymbols2.error} ${chalk3.red("Failed to save dirty queue states:")} ${chalk3.yellow(error?.message)}`);
1181
1199
  }
1182
1200
  };
1183
1201
  var createQueueStateLoader = (config, scheduleProcessing, processMessage) => (queues) => async (queueName) => {
@@ -1216,7 +1234,7 @@ var createQueueStateLoader = (config, scheduleProcessing, processMessage) => (qu
1216
1234
  }
1217
1235
  }
1218
1236
  } catch (error) {
1219
- logger.error(`Failed to load queue state for ${queueName}:`, error);
1237
+ logger.error(`Failed to load queue state for ${queueName}:`, error?.message);
1220
1238
  }
1221
1239
  };
1222
1240
  var createAllQueuesStateLoader = (config, loadQueueState) => async () => {
@@ -1229,7 +1247,7 @@ var createAllQueuesStateLoader = (config, loadQueueState) => async () => {
1229
1247
  }
1230
1248
  logger.debug("All queue states loaded");
1231
1249
  } catch (error) {
1232
- logger.error("Failed to load queue states:", error);
1250
+ logger.error("Failed to load queue states:", error?.message);
1233
1251
  }
1234
1252
  };
1235
1253
 
@@ -1297,6 +1315,7 @@ var isValidQueueName = (queueName) => {
1297
1315
  var createQueueManager = ({
1298
1316
  config,
1299
1317
  queues,
1318
+ dirtyQueues,
1300
1319
  saveQueueState,
1301
1320
  loadQueueState,
1302
1321
  scheduleProcessing,
@@ -1315,7 +1334,7 @@ var createQueueManager = ({
1315
1334
  throw new Error("Handler must be a function");
1316
1335
  }
1317
1336
  };
1318
- const addQueue = async (queueName, handler, module) => {
1337
+ const addQueue = async (queueName, handler, module2) => {
1319
1338
  try {
1320
1339
  validateQueueName(queueName);
1321
1340
  validateHandler(handler);
@@ -1323,7 +1342,7 @@ var createQueueManager = ({
1323
1342
  logger.warn(`${logSymbols3.warning} ${chalk4.yellow("Queue already exists:")} ${chalk4.magenta(queueName)}.`);
1324
1343
  return;
1325
1344
  }
1326
- queues[queueName] = createQueueService(handler, module);
1345
+ queues[queueName] = createQueueService(handler, module2);
1327
1346
  if (config.persistence.enabled && loadQueueState) {
1328
1347
  await loadQueueState(queueName);
1329
1348
  }
@@ -1365,10 +1384,10 @@ var createQueueManager = ({
1365
1384
  return false;
1366
1385
  }
1367
1386
  };
1368
- const listQueues = (module) => {
1387
+ const listQueues = (module2) => {
1369
1388
  return Object.keys(queues).filter((queueName) => {
1370
1389
  const queueService = queues[queueName];
1371
- return !module || queueService.module === module;
1390
+ return !module2 || queueService.module === module2;
1372
1391
  }).sort();
1373
1392
  };
1374
1393
  const getQueueStats = (queueName) => {
@@ -1407,8 +1426,8 @@ var createQueueManager = ({
1407
1426
  queues[queueName].incoming.push(message);
1408
1427
  scheduleProcessing(queueName, queues[queueName]);
1409
1428
  handleImmediateProcessing(queues[queueName], config, processBatch, queueName);
1410
- if (config.persistence.enabled && saveQueueState) {
1411
- await saveQueueState(queueName, queues[queueName]);
1429
+ if (config.persistence.enabled) {
1430
+ dirtyQueues.add(queueName);
1412
1431
  }
1413
1432
  return {
1414
1433
  id: message.id,
@@ -1429,8 +1448,8 @@ var createQueueManager = ({
1429
1448
  }
1430
1449
  logDLQOperation("Moving", processedCount, queueName);
1431
1450
  scheduleProcessing(queueName, queues[queueName]);
1432
- if (saveQueueState) {
1433
- await saveQueueState(queueName, queues[queueName]);
1451
+ if (config.persistence.enabled) {
1452
+ dirtyQueues.add(queueName);
1434
1453
  }
1435
1454
  return {
1436
1455
  status: "success",
@@ -1451,8 +1470,8 @@ var createQueueManager = ({
1451
1470
  };
1452
1471
  }
1453
1472
  logDLQOperation("Purging", purgedCount, queueName);
1454
- if (saveQueueState) {
1455
- await saveQueueState(queueName, queues[queueName]);
1473
+ if (config.persistence.enabled) {
1474
+ dirtyQueues.add(queueName);
1456
1475
  }
1457
1476
  return {
1458
1477
  status: "success",
@@ -1483,14 +1502,18 @@ var createQueueDecorator = (queueManager) => (app) => {
1483
1502
  var queuesPlugin = fsPlugin(async (app, options) => {
1484
1503
  const config = mergeConfig(DEFAULT_CONFIG, options.config);
1485
1504
  const queues = {};
1505
+ const dirtyQueues = /* @__PURE__ */ new Set();
1506
+ const intervalIdHolder = {};
1486
1507
  const processMessage = createProcessMessageHandler(config);
1487
1508
  const saveQueueState = config.persistence.enabled ? createQueueStateSaver(config) : void 0;
1488
1509
  const saveAllQueueStates = config.persistence.enabled && saveQueueState ? createAllQueuesStateSaver(config, saveQueueState)(queues) : async () => {
1489
1510
  };
1511
+ const saveDirtyQueueStates = config.persistence.enabled && saveQueueState ? createDirtyQueuesStateSaver(saveQueueState, dirtyQueues)(queues) : async () => {
1512
+ };
1490
1513
  const processBatch = createBatchProcessor(
1491
1514
  processMessage,
1492
1515
  config,
1493
- saveQueueState ? (queueName) => saveQueueState(queueName, queues[queueName]) : void 0
1516
+ config.persistence.enabled ? dirtyQueues : void 0
1494
1517
  );
1495
1518
  const scheduleProcessing = createProcessingScheduler(processBatch, config);
1496
1519
  const loadQueueState = config.persistence.enabled ? createQueueStateLoader(
@@ -1501,11 +1524,12 @@ var queuesPlugin = fsPlugin(async (app, options) => {
1501
1524
  };
1502
1525
  const loadAllQueueStates = config.persistence.enabled ? createAllQueuesStateLoader(config, loadQueueState) : async () => {
1503
1526
  };
1504
- const initializePersistence = config.persistence.enabled ? createPersistenceInitializer(config, loadAllQueueStates, saveAllQueueStates) : async () => {
1527
+ const initializePersistence = config.persistence.enabled ? createPersistenceInitializer(config, loadAllQueueStates, saveDirtyQueueStates, saveAllQueueStates, intervalIdHolder) : async () => {
1505
1528
  };
1506
1529
  const queueManager = createQueueManager({
1507
1530
  config,
1508
1531
  queues,
1532
+ dirtyQueues,
1509
1533
  saveQueueState: saveQueueState ? (queueName) => saveQueueState(queueName, queues[queueName]) : void 0,
1510
1534
  loadQueueState,
1511
1535
  scheduleProcessing,