@event-driven-io/emmett-postgresql 0.42.1-alpha.1 → 0.42.2

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
@@ -1074,6 +1074,9 @@ CREATE OR REPLACE FUNCTION emt_try_acquire_processor_lock(
1074
1074
  RETURNS TABLE (acquired BOOLEAN, checkpoint TEXT)
1075
1075
  LANGUAGE plpgsql
1076
1076
  AS $emt_try_acquire_processor_lock$
1077
+ DECLARE
1078
+ current_position TEXT;
1079
+ v_current_time TIMESTAMPTZ := clock_timestamp();
1077
1080
  BEGIN
1078
1081
  RETURN QUERY
1079
1082
  WITH lock_check AS (
@@ -1091,16 +1094,16 @@ BEGIN
1091
1094
  created_at,
1092
1095
  last_updated
1093
1096
  )
1094
- SELECT p_processor_id, p_partition, p_version, p_processor_instance_id, 'running', '${bigInt.toNormalizedString(0n)}', '0'::xid8, now(), now()
1097
+ SELECT p_processor_id, p_partition, p_version, p_processor_instance_id, 'running', '${bigInt.toNormalizedString(0n)}', '0'::xid8, v_current_time, v_current_time
1095
1098
  WHERE (SELECT lock_acquired FROM lock_check) = true
1096
1099
  ON CONFLICT (processor_id, partition, version) DO UPDATE
1097
1100
  SET processor_instance_id = p_processor_instance_id,
1098
1101
  status = 'running',
1099
- last_updated = now()
1102
+ last_updated = v_current_time
1100
1103
  WHERE ${processorsTable.name}.processor_instance_id = p_processor_instance_id
1101
1104
  OR ${processorsTable.name}.processor_instance_id = '${unknownTag}'
1102
1105
  OR ${processorsTable.name}.status = 'stopped'
1103
- OR ${processorsTable.name}.last_updated < now() - (p_lock_timeout_seconds || ' seconds')::interval
1106
+ OR ${processorsTable.name}.last_updated < v_current_time - (p_lock_timeout_seconds || ' seconds')::interval
1104
1107
  RETURNING last_processed_checkpoint
1105
1108
  ),
1106
1109
  projection_status AS (
@@ -2745,7 +2748,7 @@ BEGIN
2745
2748
  END $$;
2746
2749
  `);
2747
2750
  var migration_0_42_0_FromSubscriptionsToProcessors = _dumbo.sqlMigration.call(void 0,
2748
- "emt:postgresql:eventstore:0.42.1-alpha.1:from-subscriptions-to-processors",
2751
+ "emt:postgresql:eventstore:0.42.0:from-subscriptions-to-processors",
2749
2752
  [migration_0_42_0_FromSubscriptionsToProcessorsSQL]
2750
2753
  );
2751
2754
  var migration_0_42_0_2_AddProcessorProjectionFunctionsSQL = _dumbo.rawSql.call(void 0, `
@@ -3022,9 +3025,83 @@ END;
3022
3025
  $emt_deactivate_projection$;
3023
3026
  `);
3024
3027
  var migration_0_42_0_2_AddProcessorProjectionFunctions = _dumbo.sqlMigration.call(void 0,
3025
- "emt:postgresql:eventstore:0.42.1-alpha.1-2:add-processor-projection-functions",
3028
+ "emt:postgresql:eventstore:0.42.0-2:add-processor-projection-functions",
3026
3029
  [migration_0_42_0_2_AddProcessorProjectionFunctionsSQL]
3027
3030
  );
3031
+ var migration_0_42_0_3_FixProcessorLockTimeoutSQL = _dumbo.rawSql.call(void 0, `
3032
+ CREATE OR REPLACE FUNCTION emt_try_acquire_processor_lock(
3033
+ p_lock_key BIGINT,
3034
+ p_processor_id TEXT,
3035
+ p_version INT,
3036
+ p_partition TEXT DEFAULT '${defaultTag}',
3037
+ p_processor_instance_id TEXT DEFAULT 'emt:unknown',
3038
+ p_projection_name TEXT DEFAULT NULL,
3039
+ p_projection_type VARCHAR(1) DEFAULT NULL,
3040
+ p_projection_kind TEXT DEFAULT NULL,
3041
+ p_lock_timeout_seconds INT DEFAULT 300
3042
+ )
3043
+ RETURNS TABLE (acquired BOOLEAN, checkpoint TEXT)
3044
+ LANGUAGE plpgsql
3045
+ AS $emt_try_acquire_processor_lock$
3046
+ DECLARE
3047
+ current_position TEXT;
3048
+ v_current_time TIMESTAMPTZ := clock_timestamp();
3049
+ BEGIN
3050
+ RETURN QUERY
3051
+ WITH lock_check AS (
3052
+ SELECT pg_try_advisory_xact_lock(p_lock_key) AS lock_acquired
3053
+ ),
3054
+ ownership_check AS (
3055
+ INSERT INTO emt_processors (
3056
+ processor_id,
3057
+ partition,
3058
+ version,
3059
+ processor_instance_id,
3060
+ status,
3061
+ last_processed_checkpoint,
3062
+ last_processed_transaction_id,
3063
+ created_at,
3064
+ last_updated
3065
+ )
3066
+ SELECT p_processor_id, p_partition, p_version, p_processor_instance_id, 'running', '0000000000000000000', '0'::xid8, v_current_time, v_current_time
3067
+ WHERE (SELECT lock_acquired FROM lock_check) = true
3068
+ ON CONFLICT (processor_id, partition, version) DO UPDATE
3069
+ SET processor_instance_id = p_processor_instance_id,
3070
+ status = 'running',
3071
+ last_updated = v_current_time
3072
+ WHERE emt_processors.processor_instance_id = p_processor_instance_id
3073
+ OR emt_processors.processor_instance_id = 'emt:unknown'
3074
+ OR emt_processors.status = 'stopped'
3075
+ OR emt_processors.last_updated < v_current_time - (p_lock_timeout_seconds || ' seconds')::interval
3076
+ RETURNING last_processed_checkpoint
3077
+ ),
3078
+ projection_status AS (
3079
+ INSERT INTO emt_projections (
3080
+ name,
3081
+ partition,
3082
+ version,
3083
+ type,
3084
+ kind,
3085
+ status,
3086
+ definition
3087
+ )
3088
+ SELECT p_projection_name, p_partition, p_version, p_projection_type, p_projection_kind, 'async_processing', '{}'::jsonb
3089
+ WHERE p_projection_name IS NOT NULL
3090
+ AND (SELECT last_processed_checkpoint FROM ownership_check) IS NOT NULL
3091
+ ON CONFLICT (name, partition, version) DO UPDATE
3092
+ SET status = 'async_processing'
3093
+ RETURNING name
3094
+ )
3095
+ SELECT
3096
+ (SELECT COUNT(*) > 0 FROM ownership_check),
3097
+ (SELECT oc.last_processed_checkpoint FROM ownership_check oc);
3098
+ END;
3099
+ $emt_try_acquire_processor_lock$;
3100
+ `);
3101
+ var migration_0_42_0_3_FixProcessorLockTimeout = _dumbo.sqlMigration.call(void 0,
3102
+ "emt:postgresql:eventstore:0.42.0-3:fix-processor-lock-timeout",
3103
+ [migration_0_42_0_3_FixProcessorLockTimeoutSQL]
3104
+ );
3028
3105
 
3029
3106
  // src/eventStore/schema/migrations/0_42_0/0_42_0.snapshot.ts
3030
3107
 
@@ -3348,6 +3425,9 @@ CREATE OR REPLACE FUNCTION emt_try_acquire_processor_lock(
3348
3425
  RETURNS TABLE (acquired BOOLEAN, checkpoint TEXT)
3349
3426
  LANGUAGE plpgsql
3350
3427
  AS $emt_try_acquire_processor_lock$
3428
+ DECLARE
3429
+ current_position TEXT;
3430
+ v_current_time TIMESTAMPTZ := clock_timestamp();
3351
3431
  BEGIN
3352
3432
  RETURN QUERY
3353
3433
  WITH lock_check AS (
@@ -3365,16 +3445,16 @@ BEGIN
3365
3445
  created_at,
3366
3446
  last_updated
3367
3447
  )
3368
- SELECT p_processor_id, p_partition, p_version, p_processor_instance_id, 'running', '0000000000000000000', '0'::xid8, now(), now()
3448
+ SELECT p_processor_id, p_partition, p_version, p_processor_instance_id, 'running', '0000000000000000000', '0'::xid8, v_current_time, v_current_time
3369
3449
  WHERE (SELECT lock_acquired FROM lock_check) = true
3370
3450
  ON CONFLICT (processor_id, partition, version) DO UPDATE
3371
3451
  SET processor_instance_id = p_processor_instance_id,
3372
3452
  status = 'running',
3373
- last_updated = now()
3453
+ last_updated = v_current_time
3374
3454
  WHERE emt_processors.processor_instance_id = p_processor_instance_id
3375
3455
  OR emt_processors.processor_instance_id = 'emt:unknown'
3376
3456
  OR emt_processors.status = 'stopped'
3377
- OR emt_processors.last_updated < now() - (p_lock_timeout_seconds || ' seconds')::interval
3457
+ OR emt_processors.last_updated < v_current_time - (p_lock_timeout_seconds || ' seconds')::interval
3378
3458
  RETURNING last_processed_checkpoint
3379
3459
  ),
3380
3460
  projection_status AS (
@@ -4236,6 +4316,7 @@ var eventStoreSchemaMigrations = [
4236
4316
  migration_0_38_7_and_older,
4237
4317
  migration_0_42_0_FromSubscriptionsToProcessors,
4238
4318
  migration_0_42_0_2_AddProcessorProjectionFunctions,
4319
+ migration_0_42_0_3_FixProcessorLockTimeout,
4239
4320
  schemaMigration
4240
4321
  ];
4241
4322
  var createEventStoreSchema = (connectionString, pool, hooks, options) => {
@@ -4969,8 +5050,12 @@ var postgreSQLEventStoreConsumer = (options) => {
4969
5050
  messagePuller = void 0;
4970
5051
  abortController = null;
4971
5052
  }
4972
- await start;
4973
- await stopProcessors();
5053
+ try {
5054
+ await start;
5055
+ } catch (error) {
5056
+ console.log("Error during consumer stop:", error);
5057
+ await stopProcessors();
5058
+ }
4974
5059
  };
4975
5060
  const init = async () => {
4976
5061
  if (isInitialized) return;