@event-driven-io/emmett-postgresql 0.42.0 → 0.42.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
@@ -657,19 +657,39 @@ var reactor = (options) => {
657
657
  type,
658
658
  init,
659
659
  start: async (startOptions) => {
660
- if (isActive) return;
660
+ if (isActive) {
661
+ console.log(
662
+ `Processor ${processorId} with instance id ${instanceId} is already active. Start request ignored.`
663
+ );
664
+ return;
665
+ }
666
+ console.log(
667
+ `Starting processor ${processorId} with instance id ${instanceId}`
668
+ );
661
669
  await init(startOptions);
662
670
  isActive = true;
663
671
  closeSignal = onShutdown(() => close({}));
664
- if (lastCheckpoint !== null)
672
+ if (lastCheckpoint !== null) {
673
+ console.log(
674
+ `Processor ${processorId} started with instance id ${instanceId}, checkpoint: ${JSONParser.stringify(lastCheckpoint)}`
675
+ );
665
676
  return {
666
677
  lastCheckpoint
667
678
  };
679
+ }
668
680
  return await processingScope(async (context) => {
669
681
  if (hooks.onStart) {
682
+ console.log(
683
+ `Executing onStart hook for processor ${processorId} with instance id ${instanceId}`
684
+ );
670
685
  await hooks.onStart(context);
671
686
  }
672
- if (startFrom && startFrom !== "CURRENT") return startFrom;
687
+ if (startFrom && startFrom !== "CURRENT") {
688
+ console.log(
689
+ `Processor ${processorId} with instance id ${instanceId} starting from: ${JSONParser.stringify(startFrom)}`
690
+ );
691
+ return startFrom;
692
+ }
673
693
  if (checkpoints) {
674
694
  const readResult = await _optionalChain([checkpoints, 'optionalAccess', _20 => _20.read, 'call', _21 => _21(
675
695
  {
@@ -680,7 +700,15 @@ var reactor = (options) => {
680
700
  )]);
681
701
  lastCheckpoint = readResult.lastCheckpoint;
682
702
  }
683
- if (lastCheckpoint === null) return "BEGINNING";
703
+ if (lastCheckpoint === null) {
704
+ console.log(
705
+ `Processor ${processorId} with instance id ${instanceId} starting from: BEGINNING`
706
+ );
707
+ return "BEGINNING";
708
+ }
709
+ console.log(
710
+ `Checkpoint read for processor ${processorId} with instance id ${instanceId}: ${JSONParser.stringify(lastCheckpoint)}`
711
+ );
684
712
  return {
685
713
  lastCheckpoint
686
714
  };
@@ -692,48 +720,64 @@ var reactor = (options) => {
692
720
  },
693
721
  handle: async (messages, partialContext) => {
694
722
  if (!isActive) return Promise.resolve();
695
- return await processingScope(async (context) => {
696
- let result = void 0;
697
- for (const message2 of messages) {
698
- if (wasMessageHandled(message2, lastCheckpoint)) continue;
699
- const upcasted = upcastRecordedMessage(
700
- // TODO: Make it smarter
701
- message2,
702
- _optionalChain([options, 'access', _22 => _22.messageOptions, 'optionalAccess', _23 => _23.schema, 'optionalAccess', _24 => _24.versioning])
703
- );
704
- if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
705
- continue;
706
- const messageProcessingResult = await eachMessage(upcasted, context);
707
- if (checkpoints) {
708
- const storeCheckpointResult = await checkpoints.store(
709
- {
710
- processorId,
711
- version,
712
- message: upcasted,
713
- lastCheckpoint,
714
- partition
715
- },
723
+ try {
724
+ return await processingScope(async (context) => {
725
+ let result = void 0;
726
+ for (const message2 of messages) {
727
+ if (wasMessageHandled(message2, lastCheckpoint)) continue;
728
+ const upcasted = upcastRecordedMessage(
729
+ // TODO: Make it smarter
730
+ message2,
731
+ _optionalChain([options, 'access', _22 => _22.messageOptions, 'optionalAccess', _23 => _23.schema, 'optionalAccess', _24 => _24.versioning])
732
+ );
733
+ if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
734
+ continue;
735
+ const messageProcessingResult = await eachMessage(
736
+ upcasted,
716
737
  context
717
738
  );
718
- if (storeCheckpointResult.success) {
719
- lastCheckpoint = storeCheckpointResult.newCheckpoint;
739
+ if (checkpoints) {
740
+ const storeCheckpointResult = await checkpoints.store(
741
+ {
742
+ processorId,
743
+ version,
744
+ message: upcasted,
745
+ lastCheckpoint,
746
+ partition
747
+ },
748
+ context
749
+ );
750
+ if (storeCheckpointResult.success) {
751
+ lastCheckpoint = storeCheckpointResult.newCheckpoint;
752
+ }
720
753
  }
754
+ if (messageProcessingResult && messageProcessingResult.type === "STOP") {
755
+ isActive = false;
756
+ result = messageProcessingResult;
757
+ break;
758
+ }
759
+ if (stopAfter && stopAfter(upcasted)) {
760
+ isActive = false;
761
+ result = { type: "STOP", reason: "Stop condition reached" };
762
+ break;
763
+ }
764
+ if (messageProcessingResult && messageProcessingResult.type === "SKIP")
765
+ continue;
721
766
  }
722
- if (messageProcessingResult && messageProcessingResult.type === "STOP") {
723
- isActive = false;
724
- result = messageProcessingResult;
725
- break;
726
- }
727
- if (stopAfter && stopAfter(upcasted)) {
728
- isActive = false;
729
- result = { type: "STOP", reason: "Stop condition reached" };
730
- break;
731
- }
732
- if (messageProcessingResult && messageProcessingResult.type === "SKIP")
733
- continue;
734
- }
735
- return result;
736
- }, partialContext);
767
+ return result;
768
+ }, partialContext);
769
+ } catch (error2) {
770
+ console.log(
771
+ `Error during message processing for processor ${processorId} with instance id ${instanceId}. Stopping the processor.`,
772
+ error2
773
+ );
774
+ isActive = false;
775
+ return {
776
+ type: "STOP",
777
+ error: error2,
778
+ reason: "Error during message processing"
779
+ };
780
+ }
737
781
  }
738
782
  };
739
783
  };
@@ -828,32 +872,40 @@ var postgreSQLEventStoreMessageBatchPuller = ({
828
872
  let isRunning = false;
829
873
  let start;
830
874
  const pullMessages = async (options) => {
831
- const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? await _asyncNullishCoalesce((await readLastMessageGlobalPosition(executor)).currentGlobalPosition, async () => ( 0n)) : options.startFrom.lastCheckpoint;
832
- const readMessagesOptions = {
833
- after,
834
- batchSize
835
- };
836
- let waitTime = 100;
837
- while (isRunning && !_optionalChain([signal, 'optionalAccess', _37 => _37.aborted])) {
838
- const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
839
- if (messages.length > 0) {
840
- const result = await eachBatch(messages);
841
- if (result && result.type === "STOP") {
875
+ try {
876
+ const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? await _asyncNullishCoalesce((await readLastMessageGlobalPosition(executor)).currentGlobalPosition, async () => ( 0n)) : options.startFrom.lastCheckpoint;
877
+ const readMessagesOptions = {
878
+ after,
879
+ batchSize
880
+ };
881
+ let waitTime = 100;
882
+ while (isRunning && !_optionalChain([signal, 'optionalAccess', _37 => _37.aborted])) {
883
+ const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
884
+ if (messages.length > 0) {
885
+ const result = await eachBatch(messages);
886
+ if (result && result.type === "STOP") {
887
+ isRunning = false;
888
+ break;
889
+ }
890
+ }
891
+ readMessagesOptions.after = currentGlobalPosition;
892
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
893
+ if (_optionalChain([stopWhen, 'optionalAccess', _38 => _38.noMessagesLeft]) === true && !areMessagesLeft) {
894
+ console.log(
895
+ `No messages left to process after reaching global position ${currentGlobalPosition}. Stopping the puller.`
896
+ );
842
897
  isRunning = false;
843
898
  break;
844
899
  }
900
+ if (!areMessagesLeft) {
901
+ waitTime = Math.min(waitTime * 2, 1e3);
902
+ } else {
903
+ waitTime = pullingFrequencyInMs;
904
+ }
845
905
  }
846
- readMessagesOptions.after = currentGlobalPosition;
847
- await new Promise((resolve) => setTimeout(resolve, waitTime));
848
- if (_optionalChain([stopWhen, 'optionalAccess', _38 => _38.noMessagesLeft]) === true && !areMessagesLeft) {
849
- isRunning = false;
850
- break;
851
- }
852
- if (!areMessagesLeft) {
853
- waitTime = Math.min(waitTime * 2, 1e3);
854
- } else {
855
- waitTime = pullingFrequencyInMs;
856
- }
906
+ } catch (error) {
907
+ console.log("Error occurred during message pulling:", error);
908
+ throw error;
857
909
  }
858
910
  };
859
911
  return {
@@ -886,6 +938,9 @@ var zipPostgreSQLEventStoreMessageBatchPullerStartFrom = (options) => {
886
938
 
887
939
 
888
940
 
941
+
942
+
943
+
889
944
  // src/eventStore/consumers/postgreSQLProcessor.ts
890
945
 
891
946
 
@@ -1157,6 +1212,9 @@ var tryAcquireProcessorLock = async (execute, options) => {
1157
1212
  })
1158
1213
  )
1159
1214
  );
1215
+ console.log(
1216
+ `Lock acquisition attempt for processor '${options.processorId}' with processor instance '${options.processorInstanceId}' and lock '${options.lockKey}' resulted in: ${acquired}`
1217
+ );
1160
1218
  return acquired ? { acquired: true, checkpoint } : { acquired: false };
1161
1219
  };
1162
1220
  var tryAcquireProcessorLockWithRetry = async (execute, options) => {
@@ -1185,6 +1243,9 @@ var releaseProcessorLock = async (execute, options) => {
1185
1243
  })
1186
1244
  )
1187
1245
  );
1246
+ console.log(
1247
+ `Lock for processor '${options.processorId}' with processor instance '${options.processorInstanceId}' and lock '${options.lockKey}' released: ${result}`
1248
+ );
1188
1249
  return result;
1189
1250
  };
1190
1251
 
@@ -1198,6 +1259,9 @@ var postgreSQLProcessorLock = (options) => {
1198
1259
  return {
1199
1260
  tryAcquire: async (context) => {
1200
1261
  if (acquired) {
1262
+ console.log(
1263
+ `Lock for processor '${options.processorId}' is already acquired by this instance. Reusing the lock.`
1264
+ );
1201
1265
  return true;
1202
1266
  }
1203
1267
  const result = await tryAcquireProcessorLockWithRetry(context.execute, {
@@ -1205,6 +1269,9 @@ var postgreSQLProcessorLock = (options) => {
1205
1269
  lockKey
1206
1270
  });
1207
1271
  if (!result.acquired && _optionalChain([options, 'access', _45 => _45.lockAcquisitionPolicy, 'optionalAccess', _46 => _46.type]) !== "skip") {
1272
+ console.log(
1273
+ `Failed to acquire lock for processor '${options.processorId}' with policy '${_optionalChain([options, 'access', _47 => _47.lockAcquisitionPolicy, 'optionalAccess', _48 => _48.type])}'.`
1274
+ );
1208
1275
  throw new EmmettError(
1209
1276
  `Failed to acquire lock for processor '${options.processorId}'`
1210
1277
  );
@@ -1213,12 +1280,17 @@ var postgreSQLProcessorLock = (options) => {
1213
1280
  return acquired;
1214
1281
  },
1215
1282
  release: async (context) => {
1216
- if (!acquired) return;
1283
+ if (!acquired) {
1284
+ console.log(
1285
+ `Lock for processor '${options.processorId}' is not acquired by this instance. Skipping release.`
1286
+ );
1287
+ return;
1288
+ }
1217
1289
  const { projection: projection2, ...releaseOptions } = options;
1218
1290
  await releaseProcessorLock(context.execute, {
1219
1291
  ...releaseOptions,
1220
1292
  lockKey,
1221
- projectionName: _optionalChain([projection2, 'optionalAccess', _47 => _47.name])
1293
+ projectionName: _optionalChain([projection2, 'optionalAccess', _49 => _49.name])
1222
1294
  });
1223
1295
  acquired = false;
1224
1296
  }
@@ -1889,7 +1961,7 @@ var appendToStream = (pool, streamName, streamType, messages, options) => pool.w
1889
1961
  return { success: false, result: { success: false } };
1890
1962
  try {
1891
1963
  const expectedStreamVersion = toExpectedVersion(
1892
- _optionalChain([options, 'optionalAccess', _48 => _48.expectedStreamVersion])
1964
+ _optionalChain([options, 'optionalAccess', _50 => _50.expectedStreamVersion])
1893
1965
  );
1894
1966
  const messagesToAppend = messages.map((e) => ({
1895
1967
  ...e,
@@ -1929,7 +2001,7 @@ var appendToStream = (pool, streamName, streamType, messages, options) => pool.w
1929
2001
  globalPosition
1930
2002
  };
1931
2003
  });
1932
- if (_optionalChain([options, 'optionalAccess', _49 => _49.beforeCommitHook]))
2004
+ if (_optionalChain([options, 'optionalAccess', _51 => _51.beforeCommitHook]))
1933
2005
  await options.beforeCommitHook(messagesToAppend, { transaction });
1934
2006
  return {
1935
2007
  success: true,
@@ -1970,8 +2042,8 @@ var appendEventsRaw = (execute, streamId, streamType, messages, options) => _dum
1970
2042
  messageKinds: messages.map((e) => _dumbo.sql.call(void 0, "%L", e.kind === "Event" ? "E" : "C")).join(","),
1971
2043
  streamId,
1972
2044
  streamType,
1973
- expectedStreamPosition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _50 => _50.expectedStreamVersion, 'optionalAccess', _51 => _51.toString, 'call', _52 => _52()]), () => ( "NULL")),
1974
- partition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _53 => _53.partition]), () => ( defaultTag))
2045
+ expectedStreamPosition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _52 => _52.expectedStreamVersion, 'optionalAccess', _53 => _53.toString, 'call', _54 => _54()]), () => ( "NULL")),
2046
+ partition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _55 => _55.partition]), () => ( defaultTag))
1975
2047
  })
1976
2048
  )
1977
2049
  );
@@ -4059,7 +4131,7 @@ var readProcessorCheckpoint = async (execute, options) => {
4059
4131
  FROM ${processorsTable.name}
4060
4132
  WHERE partition = %L AND processor_id = %L AND version = %s
4061
4133
  LIMIT 1`,
4062
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _54 => _54.partition]), () => ( defaultTag)),
4134
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _56 => _56.partition]), () => ( defaultTag)),
4063
4135
  options.processorId,
4064
4136
  _nullishCoalesce(options.version, () => ( 1))
4065
4137
  )
@@ -4073,9 +4145,9 @@ var readProcessorCheckpoint = async (execute, options) => {
4073
4145
  // src/eventStore/schema/readStream.ts
4074
4146
 
4075
4147
  var readStream = async (execute, streamId, options) => {
4076
- const fromCondition = _optionalChain([options, 'optionalAccess', _55 => _55.from]) ? `AND stream_position >= ${options.from}` : "";
4148
+ const fromCondition = _optionalChain([options, 'optionalAccess', _57 => _57.from]) ? `AND stream_position >= ${options.from}` : "";
4077
4149
  const to = Number(
4078
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _56 => _56.to]), () => ( (_optionalChain([options, 'optionalAccess', _57 => _57.maxCount]) ? (_nullishCoalesce(options.from, () => ( 0n))) + options.maxCount : NaN)))
4150
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _58 => _58.to]), () => ( (_optionalChain([options, 'optionalAccess', _59 => _59.maxCount]) ? (_nullishCoalesce(options.from, () => ( 0n))) + options.maxCount : NaN)))
4079
4151
  );
4080
4152
  const toCondition = !isNaN(to) ? `AND stream_position <= ${to}` : "";
4081
4153
  const events = await _dumbo.mapRows.call(void 0,
@@ -4086,7 +4158,7 @@ var readStream = async (execute, streamId, options) => {
4086
4158
  WHERE stream_id = %L AND partition = %L AND is_archived = FALSE ${fromCondition} ${toCondition}
4087
4159
  ORDER BY stream_position ASC`,
4088
4160
  streamId,
4089
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _58 => _58.partition]), () => ( defaultTag))
4161
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _60 => _60.partition]), () => ( defaultTag))
4090
4162
  )
4091
4163
  ),
4092
4164
  (row) => {
@@ -4107,7 +4179,7 @@ var readStream = async (execute, streamId, options) => {
4107
4179
  kind: "Event",
4108
4180
  metadata
4109
4181
  };
4110
- return upcastRecordedMessage(event, _optionalChain([options, 'optionalAccess', _59 => _59.schema, 'optionalAccess', _60 => _60.versioning]));
4182
+ return upcastRecordedMessage(event, _optionalChain([options, 'optionalAccess', _61 => _61.schema, 'optionalAccess', _62 => _62.versioning]));
4111
4183
  }
4112
4184
  );
4113
4185
  return events.length > 0 ? {
@@ -4132,10 +4204,10 @@ var streamExists = async (execute, streamId, options) => {
4132
4204
  WHERE stream_id = %L AND partition = %L AND is_archived = FALSE)
4133
4205
  `,
4134
4206
  streamId,
4135
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _61 => _61.partition]), () => ( defaultTag))
4207
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _63 => _63.partition]), () => ( defaultTag))
4136
4208
  )
4137
4209
  );
4138
- return _nullishCoalesce(_optionalChain([queryResult, 'access', _62 => _62.rows, 'access', _63 => _63[0], 'optionalAccess', _64 => _64.exists]), () => ( false));
4210
+ return _nullishCoalesce(_optionalChain([queryResult, 'access', _64 => _64.rows, 'access', _65 => _65[0], 'optionalAccess', _66 => _66.exists]), () => ( false));
4139
4211
  };
4140
4212
 
4141
4213
  // src/eventStore/schema/index.ts
@@ -4180,7 +4252,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
4180
4252
  };
4181
4253
  const nestedPool = _dumbo.dumbo.call(void 0, { connectionString, connection: tx.connection });
4182
4254
  try {
4183
- if (_optionalChain([hooks, 'optionalAccess', _65 => _65.onBeforeSchemaCreated])) {
4255
+ if (_optionalChain([hooks, 'optionalAccess', _67 => _67.onBeforeSchemaCreated])) {
4184
4256
  await hooks.onBeforeSchemaCreated(context);
4185
4257
  }
4186
4258
  const result = await _dumbo.runPostgreSQLMigrations.call(void 0,
@@ -4188,7 +4260,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
4188
4260
  eventStoreSchemaMigrations,
4189
4261
  options
4190
4262
  );
4191
- if (_optionalChain([hooks, 'optionalAccess', _66 => _66.onAfterSchemaCreated])) {
4263
+ if (_optionalChain([hooks, 'optionalAccess', _68 => _68.onAfterSchemaCreated])) {
4192
4264
  await hooks.onAfterSchemaCreated(context);
4193
4265
  }
4194
4266
  return result;
@@ -4203,7 +4275,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
4203
4275
  var truncateTables = async (execute, options) => {
4204
4276
  await execute.command(
4205
4277
  _dumbo.rawSql.call(void 0,
4206
- `TRUNCATE TABLE ${streamsTable.name}, ${messagesTable.name}, ${processorsTable.name}, ${projectionsTable.name} CASCADE${_optionalChain([options, 'optionalAccess', _67 => _67.resetSequences]) ? "; ALTER SEQUENCE emt_global_message_position RESTART WITH 1" : ""};`
4278
+ `TRUNCATE TABLE ${streamsTable.name}, ${messagesTable.name}, ${processorsTable.name}, ${projectionsTable.name} CASCADE${_optionalChain([options, 'optionalAccess', _69 => _69.resetSequences]) ? "; ALTER SEQUENCE emt_global_message_position RESTART WITH 1" : ""};`
4207
4279
  )
4208
4280
  );
4209
4281
  };
@@ -4221,7 +4293,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4221
4293
  };
4222
4294
  const pool = "dumbo" in poolOptions ? poolOptions.dumbo : _dumbo.dumbo.call(void 0, poolOptions);
4223
4295
  let migrateSchema = void 0;
4224
- const autoGenerateSchema = _optionalChain([options, 'access', _68 => _68.schema, 'optionalAccess', _69 => _69.autoMigration]) === void 0 || _optionalChain([options, 'access', _70 => _70.schema, 'optionalAccess', _71 => _71.autoMigration]) !== "None";
4296
+ const autoGenerateSchema = _optionalChain([options, 'access', _70 => _70.schema, 'optionalAccess', _71 => _71.autoMigration]) === void 0 || _optionalChain([options, 'access', _72 => _72.schema, 'optionalAccess', _73 => _73.autoMigration]) !== "None";
4225
4297
  const inlineProjections = (_nullishCoalesce(options.projections, () => ( []))).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
4226
4298
  const migrate = async (migrationOptions) => {
4227
4299
  if (!migrateSchema) {
@@ -4230,7 +4302,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4230
4302
  pool,
4231
4303
  {
4232
4304
  onBeforeSchemaCreated: async (context) => {
4233
- if (_optionalChain([options, 'access', _72 => _72.hooks, 'optionalAccess', _73 => _73.onBeforeSchemaCreated])) {
4305
+ if (_optionalChain([options, 'access', _74 => _74.hooks, 'optionalAccess', _75 => _75.onBeforeSchemaCreated])) {
4234
4306
  await options.hooks.onBeforeSchemaCreated(context);
4235
4307
  }
4236
4308
  },
@@ -4245,7 +4317,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4245
4317
  });
4246
4318
  }
4247
4319
  }
4248
- if (_optionalChain([options, 'access', _74 => _74.hooks, 'optionalAccess', _75 => _75.onAfterSchemaCreated])) {
4320
+ if (_optionalChain([options, 'access', _76 => _76.hooks, 'optionalAccess', _77 => _77.onAfterSchemaCreated])) {
4249
4321
  await options.hooks.onAfterSchemaCreated(context);
4250
4322
  }
4251
4323
  }
@@ -4279,13 +4351,13 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4279
4351
  truncate: (truncateOptions) => pool.withTransaction(async (transaction) => {
4280
4352
  await ensureSchemaExists();
4281
4353
  await truncateTables(transaction.execute, truncateOptions);
4282
- if (_optionalChain([truncateOptions, 'optionalAccess', _76 => _76.truncateProjections])) {
4354
+ if (_optionalChain([truncateOptions, 'optionalAccess', _78 => _78.truncateProjections])) {
4283
4355
  const projectionContext = await transactionToPostgreSQLProjectionHandlerContext(
4284
4356
  connectionString,
4285
4357
  pool,
4286
4358
  transaction
4287
4359
  );
4288
- for (const projection2 of _nullishCoalesce(_optionalChain([options, 'optionalAccess', _77 => _77.projections]), () => ( []))) {
4360
+ for (const projection2 of _nullishCoalesce(_optionalChain([options, 'optionalAccess', _79 => _79.projections]), () => ( []))) {
4289
4361
  if (projection2.projection.truncate)
4290
4362
  await projection2.projection.truncate(projectionContext);
4291
4363
  }
@@ -4295,7 +4367,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4295
4367
  },
4296
4368
  async aggregateStream(streamName, options2) {
4297
4369
  const { evolve, initialState, read } = options2;
4298
- const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _78 => _78.expectedStreamVersion]);
4370
+ const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _80 => _80.expectedStreamVersion]);
4299
4371
  let state = initialState();
4300
4372
  const result = await this.readStream(
4301
4373
  streamName,
@@ -4333,7 +4405,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4333
4405
  pool,
4334
4406
  streamName,
4335
4407
  streamType,
4336
- downcastRecordedMessages(events, _optionalChain([options2, 'optionalAccess', _79 => _79.schema, 'optionalAccess', _80 => _80.versioning])),
4408
+ downcastRecordedMessages(events, _optionalChain([options2, 'optionalAccess', _81 => _81.schema, 'optionalAccess', _82 => _82.versioning])),
4337
4409
  {
4338
4410
  ...options2,
4339
4411
  beforeCommitHook
@@ -4343,7 +4415,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4343
4415
  throw new ExpectedVersionConflictError(
4344
4416
  -1n,
4345
4417
  //TODO: Return actual version in case of error
4346
- _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _81 => _81.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
4418
+ _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _83 => _83.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
4347
4419
  );
4348
4420
  return {
4349
4421
  nextExpectedStreamVersion: appendResult.nextStreamPosition,
@@ -4426,7 +4498,7 @@ var PostgreSQLProjectionSpec = {
4426
4498
  const allEvents = [];
4427
4499
  const run = async (pool) => {
4428
4500
  let globalPosition = 0n;
4429
- const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _82 => _82.numberOfTimes]), () => ( 1));
4501
+ const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _84 => _84.numberOfTimes]), () => ( 1));
4430
4502
  for (const event of [
4431
4503
  ...givenEvents,
4432
4504
  ...Array.from({ length: numberOfTimes }).flatMap(() => events)
@@ -4484,18 +4556,18 @@ var PostgreSQLProjectionSpec = {
4484
4556
  if (!isErrorConstructor(args[0])) {
4485
4557
  assertTrue(
4486
4558
  args[0](error),
4487
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _83 => _83.toString, 'call', _84 => _84()])}`
4559
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _85 => _85.toString, 'call', _86 => _86()])}`
4488
4560
  );
4489
4561
  return;
4490
4562
  }
4491
4563
  assertTrue(
4492
4564
  error instanceof args[0],
4493
- `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _85 => _85.toString, 'call', _86 => _86()])}`
4565
+ `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _87 => _87.toString, 'call', _88 => _88()])}`
4494
4566
  );
4495
4567
  if (args[1]) {
4496
4568
  assertTrue(
4497
4569
  args[1](error),
4498
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _87 => _87.toString, 'call', _88 => _88()])}`
4570
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _89 => _89.toString, 'call', _90 => _90()])}`
4499
4571
  );
4500
4572
  }
4501
4573
  } finally {
@@ -4514,7 +4586,7 @@ var eventInStream = (streamName, event) => {
4514
4586
  ...event,
4515
4587
  metadata: {
4516
4588
  ..._nullishCoalesce(event.metadata, () => ( {})),
4517
- streamName: _nullishCoalesce(_optionalChain([event, 'access', _89 => _89.metadata, 'optionalAccess', _90 => _90.streamName]), () => ( streamName))
4589
+ streamName: _nullishCoalesce(_optionalChain([event, 'access', _91 => _91.metadata, 'optionalAccess', _92 => _92.streamName]), () => ( streamName))
4518
4590
  }
4519
4591
  };
4520
4592
  };
@@ -4643,7 +4715,7 @@ var postgreSQLRawSQLProjection = (options) => {
4643
4715
  var postgreSQLCheckpointer = () => ({
4644
4716
  read: async (options, context) => {
4645
4717
  const result = await readProcessorCheckpoint(context.execute, options);
4646
- return { lastCheckpoint: _optionalChain([result, 'optionalAccess', _91 => _91.lastProcessedCheckpoint]) };
4718
+ return { lastCheckpoint: _optionalChain([result, 'optionalAccess', _93 => _93.lastProcessedCheckpoint]) };
4647
4719
  },
4648
4720
  store: async (options, context) => {
4649
4721
  const newPosition = getCheckpoint(options.message);
@@ -4661,13 +4733,13 @@ var postgreSQLProcessingScope = (options) => {
4661
4733
  const processorConnectionString = options.connectionString;
4662
4734
  const processorPool = options.pool;
4663
4735
  const processingScope = async (handler, partialContext) => {
4664
- const connection = _optionalChain([partialContext, 'optionalAccess', _92 => _92.connection]);
4665
- const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess', _93 => _93.connectionString])));
4736
+ const connection = _optionalChain([partialContext, 'optionalAccess', _94 => _94.connection]);
4737
+ const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess', _95 => _95.connectionString])));
4666
4738
  if (!connectionString)
4667
4739
  throw new EmmettError(
4668
4740
  `PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
4669
4741
  );
4670
- const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess', _94 => _94.pool]) : processorPool), () => ( processorPool));
4742
+ const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess', _96 => _96.pool]) : processorPool), () => ( processorPool));
4671
4743
  if (!pool)
4672
4744
  throw new EmmettError(
4673
4745
  `PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
@@ -4708,11 +4780,11 @@ var wrapHooksWithProcessorLocks = (hooks, processorLock) => ({
4708
4780
  ..._nullishCoalesce(hooks, () => ( {})),
4709
4781
  onStart: async (context) => {
4710
4782
  await processorLock.tryAcquire({ execute: context.execute });
4711
- if (_optionalChain([hooks, 'optionalAccess', _95 => _95.onStart])) await hooks.onStart(context);
4783
+ if (_optionalChain([hooks, 'optionalAccess', _97 => _97.onStart])) await hooks.onStart(context);
4712
4784
  },
4713
- onClose: _optionalChain([hooks, 'optionalAccess', _96 => _96.onClose]) || processorLock ? async (context) => {
4785
+ onClose: _optionalChain([hooks, 'optionalAccess', _98 => _98.onClose]) || processorLock ? async (context) => {
4714
4786
  await processorLock.release({ execute: context.execute });
4715
- if (_optionalChain([hooks, 'optionalAccess', _97 => _97.onClose])) await hooks.onClose(context);
4787
+ if (_optionalChain([hooks, 'optionalAccess', _99 => _99.onClose])) await hooks.onClose(context);
4716
4788
  } : void 0
4717
4789
  });
4718
4790
  var postgreSQLProjector = (options) => {
@@ -4737,13 +4809,13 @@ var postgreSQLProjector = (options) => {
4737
4809
  version: _nullishCoalesce(options.projection.version, () => ( version)),
4738
4810
  handlingType: "async"
4739
4811
  } : void 0,
4740
- lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _98 => _98.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
4741
- lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _99 => _99.timeoutSeconds])
4812
+ lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _100 => _100.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
4813
+ lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _101 => _101.timeoutSeconds])
4742
4814
  });
4743
4815
  const hooks = wrapHooksWithProcessorLocks(
4744
4816
  {
4745
4817
  ..._nullishCoalesce(options.hooks, () => ( {})),
4746
- onInit: options.projection.init !== void 0 || _optionalChain([options, 'access', _100 => _100.hooks, 'optionalAccess', _101 => _101.onInit]) ? async (context) => {
4818
+ onInit: options.projection.init !== void 0 || _optionalChain([options, 'access', _102 => _102.hooks, 'optionalAccess', _103 => _103.onInit]) ? async (context) => {
4747
4819
  if (options.projection.init)
4748
4820
  await options.projection.init({
4749
4821
  version: _nullishCoalesce(options.projection.version, () => ( version)),
@@ -4754,16 +4826,16 @@ var postgreSQLProjector = (options) => {
4754
4826
  migrationOptions: options.migrationOptions
4755
4827
  }
4756
4828
  });
4757
- if (_optionalChain([options, 'access', _102 => _102.hooks, 'optionalAccess', _103 => _103.onInit]))
4829
+ if (_optionalChain([options, 'access', _104 => _104.hooks, 'optionalAccess', _105 => _105.onInit]))
4758
4830
  await options.hooks.onInit({
4759
4831
  ...context,
4760
4832
  migrationOptions: options.migrationOptions
4761
4833
  });
4762
- } : _optionalChain([options, 'access', _104 => _104.hooks, 'optionalAccess', _105 => _105.onInit]),
4834
+ } : _optionalChain([options, 'access', _106 => _106.hooks, 'optionalAccess', _107 => _107.onInit]),
4763
4835
  onClose: close ? async (context) => {
4764
- if (_optionalChain([options, 'access', _106 => _106.hooks, 'optionalAccess', _107 => _107.onClose])) await _optionalChain([options, 'access', _108 => _108.hooks, 'optionalAccess', _109 => _109.onClose, 'call', _110 => _110(context)]);
4836
+ if (_optionalChain([options, 'access', _108 => _108.hooks, 'optionalAccess', _109 => _109.onClose])) await _optionalChain([options, 'access', _110 => _110.hooks, 'optionalAccess', _111 => _111.onClose, 'call', _112 => _112(context)]);
4765
4837
  if (close) await close();
4766
- } : _optionalChain([options, 'access', _111 => _111.hooks, 'optionalAccess', _112 => _112.onClose])
4838
+ } : _optionalChain([options, 'access', _113 => _113.hooks, 'optionalAccess', _114 => _114.onClose])
4767
4839
  },
4768
4840
  processorLock
4769
4841
  );
@@ -4799,16 +4871,16 @@ var postgreSQLReactor = (options) => {
4799
4871
  partition,
4800
4872
  processorInstanceId,
4801
4873
  projection: void 0,
4802
- lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _113 => _113.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
4803
- lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _114 => _114.timeoutSeconds])
4874
+ lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _115 => _115.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
4875
+ lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _116 => _116.timeoutSeconds])
4804
4876
  });
4805
4877
  const hooks = wrapHooksWithProcessorLocks(
4806
4878
  {
4807
4879
  ..._nullishCoalesce(options.hooks, () => ( {})),
4808
4880
  onClose: close ? async (context) => {
4809
- if (_optionalChain([options, 'access', _115 => _115.hooks, 'optionalAccess', _116 => _116.onClose])) await _optionalChain([options, 'access', _117 => _117.hooks, 'optionalAccess', _118 => _118.onClose, 'call', _119 => _119(context)]);
4881
+ if (_optionalChain([options, 'access', _117 => _117.hooks, 'optionalAccess', _118 => _118.onClose])) await _optionalChain([options, 'access', _119 => _119.hooks, 'optionalAccess', _120 => _120.onClose, 'call', _121 => _121(context)]);
4810
4882
  if (close) await close();
4811
- } : _optionalChain([options, 'access', _120 => _120.hooks, 'optionalAccess', _121 => _121.onClose])
4883
+ } : _optionalChain([options, 'access', _122 => _122.hooks, 'optionalAccess', _123 => _123.onClose])
4812
4884
  },
4813
4885
  processorLock
4814
4886
  );
@@ -4856,16 +4928,24 @@ var postgreSQLEventStoreConsumer = (options) => {
4856
4928
  };
4857
4929
  const result = await Promise.allSettled(
4858
4930
  activeProcessors.map(async (s) => {
4859
- return await s.handle(messagesBatch, {
4860
- connection: {
4861
- connectionString: options.connectionString,
4862
- pool
4863
- }
4864
- });
4931
+ try {
4932
+ return await s.handle(messagesBatch, {
4933
+ connection: {
4934
+ connectionString: options.connectionString,
4935
+ pool
4936
+ }
4937
+ });
4938
+ } catch (error) {
4939
+ console.log(
4940
+ `Error during message batch processing for processor: ${s.id}`,
4941
+ error
4942
+ );
4943
+ throw error;
4944
+ }
4865
4945
  })
4866
4946
  );
4867
4947
  return result.some(
4868
- (r) => r.status === "fulfilled" && _optionalChain([r, 'access', _122 => _122.value, 'optionalAccess', _123 => _123.type]) !== "STOP"
4948
+ (r) => r.status === "fulfilled" && _optionalChain([r, 'access', _124 => _124.value, 'optionalAccess', _125 => _125.type]) !== "STOP"
4869
4949
  ) ? void 0 : {
4870
4950
  type: "STOP"
4871
4951
  };
@@ -4884,7 +4964,7 @@ var postgreSQLEventStoreConsumer = (options) => {
4884
4964
  if (!isRunning) return;
4885
4965
  isRunning = false;
4886
4966
  if (messagePuller) {
4887
- _optionalChain([abortController, 'optionalAccess', _124 => _124.abort, 'call', _125 => _125()]);
4967
+ _optionalChain([abortController, 'optionalAccess', _126 => _126.abort, 'call', _127 => _127()]);
4888
4968
  await messagePuller.stop();
4889
4969
  messagePuller = void 0;
4890
4970
  abortController = null;
@@ -4897,7 +4977,24 @@ var postgreSQLEventStoreConsumer = (options) => {
4897
4977
  const postgresProcessors = processors;
4898
4978
  for (const processor of postgresProcessors) {
4899
4979
  if (processor.init) {
4900
- await processor.init(processorContext);
4980
+ try {
4981
+ await processor.init(processorContext);
4982
+ } catch (error) {
4983
+ console.log(
4984
+ `Error during processor initialization for processor: ${processor.id}. Stopping it.`,
4985
+ error
4986
+ );
4987
+ await processor.close(processorContext).catch((closeError) => {
4988
+ console.log(
4989
+ `Error during processor cleanup after failed initialization for processor: ${processor.id}`,
4990
+ closeError
4991
+ );
4992
+ });
4993
+ console.log(
4994
+ `Processor ${processor.id} stopped successfully after failed initialization.`
4995
+ );
4996
+ throw error;
4997
+ }
4901
4998
  }
4902
4999
  }
4903
5000
  isInitialized = true;
@@ -4926,40 +5023,63 @@ var postgreSQLEventStoreConsumer = (options) => {
4926
5023
  return processor;
4927
5024
  },
4928
5025
  start: () => {
4929
- if (isRunning) return start;
4930
- if (processors.length === 0)
5026
+ if (isRunning) {
5027
+ console.log(
5028
+ "Consumer is already running. Returning the existing start promise."
5029
+ );
5030
+ return start;
5031
+ }
5032
+ if (processors.length === 0) {
5033
+ console.log(
5034
+ "Cannot start consumer without at least a single processor"
5035
+ );
4931
5036
  throw new EmmettError(
4932
5037
  "Cannot start consumer without at least a single processor"
4933
5038
  );
5039
+ }
4934
5040
  isRunning = true;
4935
5041
  abortController = new AbortController();
4936
5042
  messagePuller = postgreSQLEventStoreMessageBatchPuller({
4937
5043
  stopWhen: options.stopWhen,
4938
5044
  executor: pool.execute,
4939
5045
  eachBatch,
4940
- batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _126 => _126.batchSize]), () => ( DefaultPostgreSQLEventStoreProcessorBatchSize)),
4941
- pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _127 => _127.pullingFrequencyInMs]), () => ( DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs)),
5046
+ batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _128 => _128.batchSize]), () => ( DefaultPostgreSQLEventStoreProcessorBatchSize)),
5047
+ pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _129 => _129.pullingFrequencyInMs]), () => ( DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs)),
4942
5048
  signal: abortController.signal
4943
5049
  });
4944
5050
  start = (async () => {
4945
5051
  if (!isRunning) return;
4946
5052
  if (!isInitialized) {
5053
+ console.log("Initializing consumer before starting message pulling.");
4947
5054
  await init();
4948
5055
  }
4949
5056
  const startFrom = zipPostgreSQLEventStoreMessageBatchPullerStartFrom(
4950
5057
  await Promise.all(
4951
5058
  processors.map(async (o) => {
4952
- const result = await o.start({
4953
- execute: pool.execute,
4954
- connection: {
4955
- connectionString: options.connectionString,
4956
- pool
4957
- }
4958
- });
4959
- return result;
5059
+ try {
5060
+ const result = await o.start({
5061
+ execute: pool.execute,
5062
+ connection: {
5063
+ connectionString: options.connectionString,
5064
+ pool
5065
+ }
5066
+ });
5067
+ return result;
5068
+ } catch (error) {
5069
+ console.log(
5070
+ `Error during processor start position retrieval for processor: ${o.id}. Stopping it.`,
5071
+ error
5072
+ );
5073
+ throw error;
5074
+ }
4960
5075
  })
4961
5076
  )
4962
5077
  );
5078
+ console.log(
5079
+ `Starting message pulling with start position: ${_dumbo.JSONSerializer.serialize(
5080
+ startFrom
5081
+ )}. Waiting for messages...`
5082
+ );
4963
5083
  await messagePuller.start({ startFrom });
4964
5084
  await stopProcessors();
4965
5085
  isRunning = false;