-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(batch): add async processor (#1616)
* feat(batch): add async processor * tests: improved unit tests * chore: removed docstring + edited test handler
- Loading branch information
1 parent
49bf172
commit 4e6c0d2
Showing
19 changed files
with
819 additions
and
461 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
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,31 @@ | ||
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor'; | ||
import type { BaseRecord, FailureResponse, SuccessResponse } from './types'; | ||
|
||
/** | ||
* Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB | ||
*/ | ||
class AsyncBatchProcessor extends BasePartialBatchProcessor { | ||
public async asyncProcessRecord( | ||
record: BaseRecord | ||
): Promise<SuccessResponse | FailureResponse> { | ||
try { | ||
const data = this.toBatchType(record, this.eventType); | ||
const result = await this.handler(data, this.options); | ||
|
||
return this.successHandler(record, result); | ||
} catch (error) { | ||
return this.failureHandler(record, error as Error); | ||
} | ||
} | ||
|
||
/** | ||
* Process a record with instance's handler | ||
* @param record Batch record to be processed | ||
* @returns response of success or failure | ||
*/ | ||
public processRecord(_record: BaseRecord): SuccessResponse | FailureResponse { | ||
throw new Error('Not implemented. Use asyncProcess() instead.'); | ||
} | ||
} | ||
|
||
export { AsyncBatchProcessor }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor'; | ||
import { EventType } from './constants'; | ||
import type { | ||
BaseRecord, | ||
BatchProcessingOptions, | ||
PartialItemFailureResponse, | ||
} from './types'; | ||
|
||
/** | ||
* Higher level function to handle batch event processing | ||
* @param event Lambda's original event | ||
* @param recordHandler Callable function to process each record from the batch | ||
* @param processor Batch processor to handle partial failure cases | ||
* @returns Lambda Partial Batch Response | ||
*/ | ||
const asyncProcessPartialResponse = async ( | ||
event: { Records: BaseRecord[] }, | ||
recordHandler: CallableFunction, | ||
processor: BasePartialBatchProcessor, | ||
options?: BatchProcessingOptions | ||
): Promise<PartialItemFailureResponse> => { | ||
if (!event.Records) { | ||
const eventTypes: string = Object.values(EventType).toString(); | ||
throw new Error( | ||
'Failed to convert event to record batch for processing.\nPlease ensure batch event is a valid ' + | ||
eventTypes + | ||
' event.' | ||
); | ||
} | ||
|
||
processor.register(event.Records, recordHandler, options); | ||
|
||
await processor.asyncProcess(); | ||
|
||
return processor.response(); | ||
}; | ||
|
||
export { asyncProcessPartialResponse }; |
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.