-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api-elasticsearch-tasks): data synchronization (#4337)
1 parent
3e443da
commit 9bd4fa2
Showing
67 changed files
with
1,479 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/api-dynamodb-to-elasticsearch/src/SynchronizationBuilder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
Context, | ||
IDeleteOperationParams, | ||
IInsertOperationParams, | ||
IModifyOperationParams, | ||
IOperations | ||
} from "~/types"; | ||
import { Operations } from "~/Operations"; | ||
import { executeWithRetry, IExecuteWithRetryParams } from "~/executeWithRetry"; | ||
import { ITimer } from "@webiny/handler-aws"; | ||
|
||
export type ISynchronizationBuilderExecuteWithRetryParams = Omit< | ||
IExecuteWithRetryParams, | ||
"context" | "timer" | "maxRunningTime" | "operations" | ||
>; | ||
|
||
export interface ISynchronizationBuilder { | ||
insert(params: IInsertOperationParams): void; | ||
delete(params: IDeleteOperationParams): void; | ||
build: () => (params?: ISynchronizationBuilderExecuteWithRetryParams) => Promise<void>; | ||
} | ||
|
||
export interface ISynchronizationBuilderParams { | ||
timer: ITimer; | ||
context: Pick<Context, "elasticsearch">; | ||
} | ||
|
||
export class SynchronizationBuilder implements ISynchronizationBuilder { | ||
private readonly timer: ITimer; | ||
private readonly context: Pick<Context, "elasticsearch">; | ||
private readonly operations: IOperations; | ||
|
||
public constructor(params: ISynchronizationBuilderParams) { | ||
this.timer = params.timer; | ||
this.context = params.context; | ||
this.operations = new Operations(); | ||
} | ||
|
||
public insert(params: IInsertOperationParams): void { | ||
return this.operations.insert(params); | ||
} | ||
|
||
public modify(params: IModifyOperationParams): void { | ||
return this.operations.modify(params); | ||
} | ||
|
||
public delete(params: IDeleteOperationParams): void { | ||
return this.operations.delete(params); | ||
} | ||
|
||
public build() { | ||
return async (params?: ISynchronizationBuilderExecuteWithRetryParams) => { | ||
if (this.operations.total === 0) { | ||
return; | ||
} | ||
await executeWithRetry({ | ||
...params, | ||
maxRunningTime: this.timer.getRemainingMilliseconds(), | ||
timer: this.timer, | ||
context: this.context, | ||
operations: this.operations | ||
}); | ||
this.operations.clear(); | ||
}; | ||
} | ||
} | ||
|
||
export const createSynchronizationBuilder = ( | ||
params: ISynchronizationBuilderParams | ||
): ISynchronizationBuilder => { | ||
return new SynchronizationBuilder(params); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.