@event-driven-io/emmett 0.43.0-beta.15 → 0.43.0-beta.16

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/index.cjs CHANGED
@@ -1071,14 +1071,27 @@ const reactor = (options) => {
1071
1071
  canHandle,
1072
1072
  init,
1073
1073
  start: async (startOptions) => {
1074
- if (isActive) return;
1074
+ if (isActive) {
1075
+ console.log(`Processor ${processorId} with instance id ${instanceId} is already active. Start request ignored.`);
1076
+ return;
1077
+ }
1078
+ console.log(`Starting processor ${processorId} with instance id ${instanceId}`);
1075
1079
  await init(startOptions);
1076
1080
  isActive = true;
1077
1081
  closeSignal = onShutdown(() => close(startOptions));
1078
- if (lastCheckpoint !== null) return { lastCheckpoint };
1082
+ if (lastCheckpoint !== null) {
1083
+ console.log(`Processor ${processorId} started with instance id ${instanceId}, checkpoint: ${JSONSerializer.serialize(lastCheckpoint)}`);
1084
+ return { lastCheckpoint };
1085
+ }
1079
1086
  return await processingScope(async (context) => {
1080
- if (hooks.onStart) await hooks.onStart(context);
1081
- if (startFrom && startFrom !== "CURRENT") return startFrom;
1087
+ if (hooks.onStart) {
1088
+ console.log(`Executing onStart hook for processor ${processorId} with instance id ${instanceId}`);
1089
+ await hooks.onStart(context);
1090
+ }
1091
+ if (startFrom && startFrom !== "CURRENT") {
1092
+ console.log(`Processor ${processorId} with instance id ${instanceId} starting from: ${JSONSerializer.serialize(startFrom)}`);
1093
+ return startFrom;
1094
+ }
1082
1095
  if (checkpoints) lastCheckpoint = (await checkpoints?.read({
1083
1096
  processorId,
1084
1097
  partition
@@ -1086,7 +1099,11 @@ const reactor = (options) => {
1086
1099
  ...startOptions,
1087
1100
  ...context
1088
1101
  })).lastCheckpoint;
1089
- if (lastCheckpoint === null) return "BEGINNING";
1102
+ if (lastCheckpoint === null) {
1103
+ console.log(`Processor ${processorId} with instance id ${instanceId} starting from: BEGINNING`);
1104
+ return "BEGINNING";
1105
+ }
1106
+ console.log(`Checkpoint read for processor ${processorId} with instance id ${instanceId}: ${JSONSerializer.serialize(lastCheckpoint)}`);
1090
1107
  return { lastCheckpoint };
1091
1108
  }, startOptions);
1092
1109
  },
@@ -1096,34 +1113,44 @@ const reactor = (options) => {
1096
1113
  },
1097
1114
  handle: async (messages, partialContext) => {
1098
1115
  if (!isActive) return Promise.resolve();
1099
- return await processingScope(async (context) => {
1100
- const messagesAboveCheckpoint = messages.filter((message) => !wasMessageHandled(message, lastCheckpoint));
1101
- const upcastedMessages = messagesAboveCheckpoint.map((message) => upcastRecordedMessage(message, options.messageOptions?.schema?.versioning)).filter((upcasted) => !canHandle || canHandle.includes(upcasted.type));
1102
- const stopMessageIndex = isCustomBatch && stopAfter ? upcastedMessages.findIndex(stopAfter) : -1;
1103
- const unhandledMessages = stopMessageIndex !== -1 ? upcastedMessages.slice(0, stopMessageIndex + 1) : upcastedMessages;
1104
- const batchResult = await eachBatch(unhandledMessages, context);
1105
- const messageProcessingResult = batchResult?.type === "STOP" ? batchResult : stopMessageIndex !== -1 ? {
1116
+ try {
1117
+ return await processingScope(async (context) => {
1118
+ const messagesAboveCheckpoint = messages.filter((message) => !wasMessageHandled(message, lastCheckpoint));
1119
+ const upcastedMessages = messagesAboveCheckpoint.map((message) => upcastRecordedMessage(message, options.messageOptions?.schema?.versioning)).filter((upcasted) => !canHandle || canHandle.includes(upcasted.type));
1120
+ const stopMessageIndex = isCustomBatch && stopAfter ? upcastedMessages.findIndex(stopAfter) : -1;
1121
+ const unhandledMessages = stopMessageIndex !== -1 ? upcastedMessages.slice(0, stopMessageIndex + 1) : upcastedMessages;
1122
+ const batchResult = await eachBatch(unhandledMessages, context);
1123
+ const messageProcessingResult = batchResult?.type === "STOP" ? batchResult : stopMessageIndex !== -1 ? {
1124
+ type: "STOP",
1125
+ reason: "Stop condition reached",
1126
+ lastSuccessfulMessage: unhandledMessages[stopMessageIndex]
1127
+ } : batchResult;
1128
+ const isStop = messageProcessingResult && messageProcessingResult.type === "STOP";
1129
+ const checkpointMessage = messageProcessingResult?.type === "STOP" ? messageProcessingResult.lastSuccessfulMessage : messagesAboveCheckpoint[messagesAboveCheckpoint.length - 1];
1130
+ if (checkpointMessage && checkpoints) {
1131
+ const storeCheckpointResult = await checkpoints.store({
1132
+ processorId,
1133
+ version,
1134
+ message: checkpointMessage,
1135
+ lastCheckpoint,
1136
+ partition
1137
+ }, context);
1138
+ if (storeCheckpointResult.success) lastCheckpoint = storeCheckpointResult.newCheckpoint;
1139
+ }
1140
+ if (isStop) {
1141
+ isActive = false;
1142
+ return messageProcessingResult;
1143
+ }
1144
+ }, partialContext);
1145
+ } catch (error) {
1146
+ console.log(`Error during message processing for processor ${processorId} with instance id ${instanceId}. Stopping the processor.`, error);
1147
+ isActive = false;
1148
+ return {
1106
1149
  type: "STOP",
1107
- reason: "Stop condition reached",
1108
- lastSuccessfulMessage: unhandledMessages[stopMessageIndex]
1109
- } : batchResult;
1110
- const isStop = messageProcessingResult && messageProcessingResult.type === "STOP";
1111
- const checkpointMessage = messageProcessingResult?.type === "STOP" ? messageProcessingResult.lastSuccessfulMessage : messagesAboveCheckpoint[messagesAboveCheckpoint.length - 1];
1112
- if (checkpointMessage && checkpoints) {
1113
- const storeCheckpointResult = await checkpoints.store({
1114
- processorId,
1115
- version,
1116
- message: checkpointMessage,
1117
- lastCheckpoint,
1118
- partition
1119
- }, context);
1120
- if (storeCheckpointResult.success) lastCheckpoint = storeCheckpointResult.newCheckpoint;
1121
- }
1122
- if (isStop) {
1123
- isActive = false;
1124
- return messageProcessingResult;
1125
- }
1126
- }, partialContext);
1150
+ error,
1151
+ reason: "Error during message processing"
1152
+ };
1153
+ }
1127
1154
  }
1128
1155
  };
1129
1156
  };