@event-driven-io/emmett-esdb 0.42.0-rc.2 → 0.42.1-alpha.1

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
@@ -718,19 +718,39 @@ var reactor = (options) => {
718
718
  type,
719
719
  init,
720
720
  start: async (startOptions) => {
721
- if (isActive) return;
721
+ if (isActive) {
722
+ console.log(
723
+ `Processor ${processorId} with instance id ${instanceId} is already active. Start request ignored.`
724
+ );
725
+ return;
726
+ }
727
+ console.log(
728
+ `Starting processor ${processorId} with instance id ${instanceId}`
729
+ );
722
730
  await init(startOptions);
723
731
  isActive = true;
724
732
  closeSignal = onShutdown(() => close({}));
725
- if (lastCheckpoint !== null)
733
+ if (lastCheckpoint !== null) {
734
+ console.log(
735
+ `Processor ${processorId} started with instance id ${instanceId}, checkpoint: ${JSONParser.stringify(lastCheckpoint)}`
736
+ );
726
737
  return {
727
738
  lastCheckpoint
728
739
  };
740
+ }
729
741
  return await processingScope(async (context) => {
730
742
  if (hooks.onStart) {
743
+ console.log(
744
+ `Executing onStart hook for processor ${processorId} with instance id ${instanceId}`
745
+ );
731
746
  await hooks.onStart(context);
732
747
  }
733
- if (startFrom && startFrom !== "CURRENT") return startFrom;
748
+ if (startFrom && startFrom !== "CURRENT") {
749
+ console.log(
750
+ `Processor ${processorId} with instance id ${instanceId} starting from: ${JSONParser.stringify(startFrom)}`
751
+ );
752
+ return startFrom;
753
+ }
734
754
  if (checkpoints) {
735
755
  const readResult = await _optionalChain([checkpoints, 'optionalAccess', _27 => _27.read, 'call', _28 => _28(
736
756
  {
@@ -741,7 +761,15 @@ var reactor = (options) => {
741
761
  )]);
742
762
  lastCheckpoint = readResult.lastCheckpoint;
743
763
  }
744
- if (lastCheckpoint === null) return "BEGINNING";
764
+ if (lastCheckpoint === null) {
765
+ console.log(
766
+ `Processor ${processorId} with instance id ${instanceId} starting from: BEGINNING`
767
+ );
768
+ return "BEGINNING";
769
+ }
770
+ console.log(
771
+ `Checkpoint read for processor ${processorId} with instance id ${instanceId}: ${JSONParser.stringify(lastCheckpoint)}`
772
+ );
745
773
  return {
746
774
  lastCheckpoint
747
775
  };
@@ -753,48 +781,64 @@ var reactor = (options) => {
753
781
  },
754
782
  handle: async (messages, partialContext) => {
755
783
  if (!isActive) return Promise.resolve();
756
- return await processingScope(async (context) => {
757
- let result = void 0;
758
- for (const message2 of messages) {
759
- if (wasMessageHandled(message2, lastCheckpoint)) continue;
760
- const upcasted = upcastRecordedMessage(
761
- // TODO: Make it smarter
762
- message2,
763
- _optionalChain([options, 'access', _29 => _29.messageOptions, 'optionalAccess', _30 => _30.schema, 'optionalAccess', _31 => _31.versioning])
764
- );
765
- if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
766
- continue;
767
- const messageProcessingResult = await eachMessage(upcasted, context);
768
- if (checkpoints) {
769
- const storeCheckpointResult = await checkpoints.store(
770
- {
771
- processorId,
772
- version,
773
- message: upcasted,
774
- lastCheckpoint,
775
- partition
776
- },
784
+ try {
785
+ return await processingScope(async (context) => {
786
+ let result = void 0;
787
+ for (const message2 of messages) {
788
+ if (wasMessageHandled(message2, lastCheckpoint)) continue;
789
+ const upcasted = upcastRecordedMessage(
790
+ // TODO: Make it smarter
791
+ message2,
792
+ _optionalChain([options, 'access', _29 => _29.messageOptions, 'optionalAccess', _30 => _30.schema, 'optionalAccess', _31 => _31.versioning])
793
+ );
794
+ if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
795
+ continue;
796
+ const messageProcessingResult = await eachMessage(
797
+ upcasted,
777
798
  context
778
799
  );
779
- if (storeCheckpointResult.success) {
780
- lastCheckpoint = storeCheckpointResult.newCheckpoint;
800
+ if (checkpoints) {
801
+ const storeCheckpointResult = await checkpoints.store(
802
+ {
803
+ processorId,
804
+ version,
805
+ message: upcasted,
806
+ lastCheckpoint,
807
+ partition
808
+ },
809
+ context
810
+ );
811
+ if (storeCheckpointResult.success) {
812
+ lastCheckpoint = storeCheckpointResult.newCheckpoint;
813
+ }
781
814
  }
815
+ if (messageProcessingResult && messageProcessingResult.type === "STOP") {
816
+ isActive = false;
817
+ result = messageProcessingResult;
818
+ break;
819
+ }
820
+ if (stopAfter && stopAfter(upcasted)) {
821
+ isActive = false;
822
+ result = { type: "STOP", reason: "Stop condition reached" };
823
+ break;
824
+ }
825
+ if (messageProcessingResult && messageProcessingResult.type === "SKIP")
826
+ continue;
782
827
  }
783
- if (messageProcessingResult && messageProcessingResult.type === "STOP") {
784
- isActive = false;
785
- result = messageProcessingResult;
786
- break;
787
- }
788
- if (stopAfter && stopAfter(upcasted)) {
789
- isActive = false;
790
- result = { type: "STOP", reason: "Stop condition reached" };
791
- break;
792
- }
793
- if (messageProcessingResult && messageProcessingResult.type === "SKIP")
794
- continue;
795
- }
796
- return result;
797
- }, partialContext);
828
+ return result;
829
+ }, partialContext);
830
+ } catch (error2) {
831
+ console.log(
832
+ `Error during message processing for processor ${processorId} with instance id ${instanceId}. Stopping the processor.`,
833
+ error2
834
+ );
835
+ isActive = false;
836
+ return {
837
+ type: "STOP",
838
+ error: error2,
839
+ reason: "Error during message processing"
840
+ };
841
+ }
798
842
  }
799
843
  };
800
844
  };