|
| 1 | +/** |
| 2 | + * Process batch and partially report failed items |
| 3 | + */ |
| 4 | +import { DynamoDBRecord, KinesisStreamRecord, SQSRecord } from 'aws-lambda'; |
| 5 | +import { |
| 6 | + BasePartialProcessor, |
| 7 | + BatchProcessingError, |
| 8 | + DATA_CLASS_MAPPING, |
| 9 | + DEFAULT_RESPONSE, |
| 10 | + EventSourceDataClassTypes, |
| 11 | + EventType, |
| 12 | + ItemIdentifier, |
| 13 | + BatchResponse, |
| 14 | +} from '.'; |
| 15 | + |
| 16 | +abstract class BasePartialBatchProcessor extends BasePartialProcessor { |
| 17 | + public COLLECTOR_MAPPING; |
| 18 | + |
| 19 | + public batchResponse: BatchResponse; |
| 20 | + |
| 21 | + public eventType: keyof typeof EventType; |
| 22 | + |
| 23 | + /** |
| 24 | + * Initializes base batch processing class |
| 25 | + * @param eventType Whether this is SQS, DynamoDB stream, or Kinesis data stream event |
| 26 | + */ |
| 27 | + public constructor(eventType: keyof typeof EventType) { |
| 28 | + super(); |
| 29 | + this.eventType = eventType; |
| 30 | + this.batchResponse = DEFAULT_RESPONSE; |
| 31 | + this.COLLECTOR_MAPPING = { |
| 32 | + [EventType.SQS]: () => this.collectSqsFailures(), |
| 33 | + [EventType.KinesisDataStreams]: () => this.collectKinesisFailures(), |
| 34 | + [EventType.DynamoDBStreams]: () => this.collectDynamoDBFailures(), |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Report messages to be deleted in case of partial failures |
| 40 | + */ |
| 41 | + public clean(): void { |
| 42 | + if (!this.hasMessagesToReport()) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (this.entireBatchFailed()) { |
| 47 | + throw new BatchProcessingError( |
| 48 | + 'All records failed processing. ' + |
| 49 | + this.exceptions.length + |
| 50 | + ' individual errors logged separately below.', |
| 51 | + this.exceptions |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + const messages: ItemIdentifier[] = this.getMessagesToReport(); |
| 56 | + this.batchResponse = { batchItemFailures: messages }; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Collects identifiers of failed items for a DynamoDB stream |
| 61 | + * @returns list of identifiers for failed items |
| 62 | + */ |
| 63 | + public collectDynamoDBFailures(): ItemIdentifier[] { |
| 64 | + const failures: ItemIdentifier[] = []; |
| 65 | + |
| 66 | + for (const msg of this.failureMessages) { |
| 67 | + const msgId = (msg as DynamoDBRecord).dynamodb?.SequenceNumber; |
| 68 | + if (msgId) { |
| 69 | + failures.push({ itemIdentifier: msgId }); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return failures; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Collects identifiers of failed items for a Kinesis stream |
| 78 | + * @returns list of identifiers for failed items |
| 79 | + */ |
| 80 | + public collectKinesisFailures(): ItemIdentifier[] { |
| 81 | + const failures: ItemIdentifier[] = []; |
| 82 | + |
| 83 | + for (const msg of this.failureMessages) { |
| 84 | + const msgId = (msg as KinesisStreamRecord).kinesis.sequenceNumber; |
| 85 | + failures.push({ itemIdentifier: msgId }); |
| 86 | + } |
| 87 | + |
| 88 | + return failures; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Collects identifiers of failed items for an SQS batch |
| 93 | + * @returns list of identifiers for failed items |
| 94 | + */ |
| 95 | + public collectSqsFailures(): ItemIdentifier[] { |
| 96 | + const failures: ItemIdentifier[] = []; |
| 97 | + |
| 98 | + for (const msg of this.failureMessages) { |
| 99 | + const msgId = (msg as SQSRecord).messageId; |
| 100 | + failures.push({ itemIdentifier: msgId }); |
| 101 | + } |
| 102 | + |
| 103 | + return failures; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Determines whether all records in a batch failed to process |
| 108 | + * @returns true if all records resulted in exception results |
| 109 | + */ |
| 110 | + public entireBatchFailed(): boolean { |
| 111 | + return this.exceptions.length == this.records.length; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Collects identifiers for failed batch items |
| 116 | + * @returns formatted messages to use in batch deletion |
| 117 | + */ |
| 118 | + public getMessagesToReport(): ItemIdentifier[] { |
| 119 | + return this.COLLECTOR_MAPPING[this.eventType](); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Determines if any records failed to process |
| 124 | + * @returns true if any records resulted in exception |
| 125 | + */ |
| 126 | + public hasMessagesToReport(): boolean { |
| 127 | + if (this.failureMessages.length != 0) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + // console.debug('All ' + this.successMessages.length + ' records successfully processed'); |
| 132 | + |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Remove results from previous execution |
| 138 | + */ |
| 139 | + public prepare(): void { |
| 140 | + this.successMessages.length = 0; |
| 141 | + this.failureMessages.length = 0; |
| 142 | + this.exceptions.length = 0; |
| 143 | + this.batchResponse = DEFAULT_RESPONSE; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @returns Batch items that failed processing, if any |
| 148 | + */ |
| 149 | + public response(): BatchResponse { |
| 150 | + return this.batchResponse; |
| 151 | + } |
| 152 | + |
| 153 | + public toBatchType( |
| 154 | + record: EventSourceDataClassTypes, |
| 155 | + eventType: keyof typeof EventType |
| 156 | + ): SQSRecord | KinesisStreamRecord | DynamoDBRecord { |
| 157 | + return DATA_CLASS_MAPPING[eventType](record); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +export { BasePartialBatchProcessor }; |
0 commit comments