@punks/backend-entity-manager 0.0.324 → 0.0.326

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.
@@ -1,9 +1,9 @@
1
1
  import { PipelineCompletedStepState } from "../../../../types";
2
2
  export declare class PipelineUtils<TPipelineInput, TPipelineOutput, TContext> {
3
- getStepByKey: (step: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, key: string) => PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>;
4
- getStepInputByKey: <TStepInput>(step: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, key: string) => TStepInput;
5
- getStepOutputByKey: <TStepOutput>(step: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, key: string) => TStepOutput;
6
- getStep: (step: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, index: number) => PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>;
7
- getStepInput: <TStepInput>(step: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, index: number) => TStepInput;
8
- getStepOutput: <TStepOutput>(step: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, index: number) => TStepOutput;
3
+ getStepByKey: (state: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, key: string) => PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>;
4
+ getStepInputByKey: <TStepInput>(state: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, key: string) => TStepInput;
5
+ getStepOutputByKey: <TStepOutput>(state: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, key: string) => TStepOutput;
6
+ getStep: (state: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, index: number) => PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>;
7
+ getStepInput: <TStepInput>(state: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, index: number) => TStepInput;
8
+ getStepOutput: <TStepOutput>(state: PipelineCompletedStepState<TPipelineInput, TContext, unknown, unknown>, index: number) => TStepOutput;
9
9
  }
@@ -1,10 +1,10 @@
1
1
  import { ILockRepository } from "../../../../abstractions";
2
- import { ExclusiveOperationResult, ExecuteExclusiveInput } from "./types";
2
+ import { ExclusiveOperationResult, ExecuteExclusiveInput, ExecuteSequentialInput } from "./types";
3
3
  export declare class OperationLockService {
4
4
  private readonly operations;
5
5
  private readonly logger;
6
6
  constructor(operations: ILockRepository);
7
+ executeSequential: <T>(input: ExecuteSequentialInput<T>) => Promise<T>;
7
8
  executeExclusive: <T>(input: ExecuteExclusiveInput<T>) => Promise<ExclusiveOperationResult<T>>;
8
- private executeOperation;
9
9
  private isLockExpired;
10
10
  }
@@ -10,3 +10,12 @@ export declare class ExclusiveOperationResult<T> {
10
10
  skipped: boolean;
11
11
  result?: T;
12
12
  }
13
+ export interface ExecuteSequentialInput<T> {
14
+ lockUid: string;
15
+ requestedBy?: string;
16
+ lockTimeout?: number;
17
+ lockPolling?: number;
18
+ operation: () => Promise<T>;
19
+ }
20
+ export declare class ExecuteSequentialTimeoutError extends Error {
21
+ }
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Log, csvParse, excelParse, excelBuild, csvBuild, isNullOrUndefined, addTime, newUuid as newUuid$1, buildObject, toDict, sort, byField, toArrayDict, toItemsDict, ensureTailingSlash, ensureStartSlash, removeUndefinedProps, sleep } from '@punks/backend-core';
1
+ import { Log, csvParse, excelParse, excelBuild, csvBuild, isNullOrUndefined, addTime, newUuid as newUuid$1, buildObject, toDict, sleep, sort, byField, toArrayDict, toItemsDict, ensureTailingSlash, ensureStartSlash, removeUndefinedProps } from '@punks/backend-core';
2
2
  import { MoreThanOrEqual, Equal, Not, IsNull, And, MoreThan, LessThanOrEqual, LessThan, ILike, In, Or, Repository } from 'typeorm';
3
3
  import { applyDecorators, Injectable, SetMetadata, createParamDecorator, Global, Module, Scope, Inject, Logger, StreamableFile, HttpException, HttpStatus } from '@nestjs/common';
4
4
  import { Reflector } from '@nestjs/core';
@@ -21947,10 +21947,37 @@ function subDays(dirtyDate, dirtyAmount) {
21947
21947
  }
21948
21948
 
21949
21949
  var OperationLockService_1;
21950
+ const DEFAULT_LOCK_POLLING = 100;
21950
21951
  let OperationLockService = OperationLockService_1 = class OperationLockService {
21951
21952
  constructor(operations) {
21952
21953
  this.operations = operations;
21953
21954
  this.logger = Log.getLogger(OperationLockService_1.name);
21955
+ this.executeSequential = async (input) => {
21956
+ const lock = await this.operations.acquireLock({
21957
+ lockUid: input.lockUid,
21958
+ requestedBy: input.requestedBy,
21959
+ });
21960
+ if (!lock.available &&
21961
+ input.lockTimeout &&
21962
+ this.isLockExpired(lock.lockItem, new Date(), input.lockTimeout)) {
21963
+ await this.operations.releaseLock({
21964
+ lockUid: input.lockUid,
21965
+ });
21966
+ return await this.executeSequential(input);
21967
+ }
21968
+ if (!lock.available) {
21969
+ await sleep(input.lockPolling ?? DEFAULT_LOCK_POLLING);
21970
+ return await this.executeSequential(input);
21971
+ }
21972
+ try {
21973
+ return await input.operation();
21974
+ }
21975
+ finally {
21976
+ await this.operations.releaseLock({
21977
+ lockUid: input.lockUid,
21978
+ });
21979
+ }
21980
+ };
21954
21981
  this.executeExclusive = async (input) => {
21955
21982
  const lock = await this.operations.acquireLock({
21956
21983
  lockUid: input.lockUid,
@@ -21973,7 +22000,7 @@ let OperationLockService = OperationLockService_1 = class OperationLockService {
21973
22000
  try {
21974
22001
  return {
21975
22002
  skipped: false,
21976
- result: await this.executeOperation(input),
22003
+ result: await input.operation(),
21977
22004
  };
21978
22005
  }
21979
22006
  finally {
@@ -21982,9 +22009,6 @@ let OperationLockService = OperationLockService_1 = class OperationLockService {
21982
22009
  });
21983
22010
  }
21984
22011
  };
21985
- this.executeOperation = async (input) => {
21986
- return await input.operation();
21987
- };
21988
22012
  this.isLockExpired = (item, refDate, timeoutMinutes) => {
21989
22013
  return differenceInMinutes(refDate, item.createdOn) > timeoutMinutes;
21990
22014
  };
@@ -23531,6 +23555,7 @@ class PipelineStepOperationOptionsBuilder {
23531
23555
  }
23532
23556
  build() {
23533
23557
  return {
23558
+ key: this.operation.key,
23534
23559
  name: this.operation.name,
23535
23560
  operation: this.operation,
23536
23561
  rollbackOperations: this.rollbackOperations,
@@ -34306,32 +34331,32 @@ const buildCompletedStepsSequence = (step) => {
34306
34331
  };
34307
34332
  class PipelineUtils {
34308
34333
  constructor() {
34309
- this.getStepByKey = (step, key) => {
34310
- const sequence = buildCompletedStepsSequence(step);
34334
+ this.getStepByKey = (state, key) => {
34335
+ const sequence = buildCompletedStepsSequence(state);
34311
34336
  const matchingStep = sequence.find((x) => x.reference.key === key);
34312
34337
  if (!matchingStep) {
34313
34338
  throw new Error(`Step key ${key} not found`);
34314
34339
  }
34315
34340
  return matchingStep;
34316
34341
  };
34317
- this.getStepInputByKey = (step, key) => {
34318
- return this.getStepByKey(step, key).stepInput;
34342
+ this.getStepInputByKey = (state, key) => {
34343
+ return this.getStepByKey(state, key).stepInput;
34319
34344
  };
34320
- this.getStepOutputByKey = (step, key) => {
34321
- return this.getStepByKey(step, key).stepOutput;
34345
+ this.getStepOutputByKey = (state, key) => {
34346
+ return this.getStepByKey(state, key).stepOutput;
34322
34347
  };
34323
- this.getStep = (step, index) => {
34324
- const sequence = buildCompletedStepsSequence(step);
34348
+ this.getStep = (state, index) => {
34349
+ const sequence = buildCompletedStepsSequence(state);
34325
34350
  if (index >= sequence.length) {
34326
34351
  throw new Error(`Step index ${index} is out of range`);
34327
34352
  }
34328
34353
  return sequence[index];
34329
34354
  };
34330
- this.getStepInput = (step, index) => {
34331
- return this.getStep(step, index).stepInput;
34355
+ this.getStepInput = (state, index) => {
34356
+ return this.getStep(state, index).stepInput;
34332
34357
  };
34333
- this.getStepOutput = (step, index) => {
34334
- return this.getStep(step, index).stepOutput;
34358
+ this.getStepOutput = (state, index) => {
34359
+ return this.getStep(state, index).stepOutput;
34335
34360
  };
34336
34361
  }
34337
34362
  }