Skip to content

Commit 24b0ca1

Browse files
authored
docs(batch): added flow charts (#1640)
1 parent ca40c46 commit 24b0ca1

8 files changed

+281
-27
lines changed

docs/snippets/batch/accessProcessedMessages.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ export const handler = async (
2222
event: SQSEvent,
2323
context: Context
2424
): Promise<SQSBatchResponse> => {
25-
const batch = event.Records;
25+
const batch = event.Records; // (1)!
2626

27-
processor.register(batch, recordHandler, { context });
27+
processor.register(batch, recordHandler, { context }); // (2)!
2828
const processedMessages = processor.process();
2929

3030
for (const message of processedMessages) {
3131
const status: 'success' | 'fail' = message[0];
32+
const error = message[1];
3233
const record = message[2];
3334

34-
logger.info('Processed record', { status, record });
35+
logger.info('Processed record', { status, record, error });
3536
}
3637

3738
return processor.response();

docs/snippets/batch/customPartialProcessor.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import type {
1212
SuccessResponse,
1313
FailureResponse,
14-
EventSourceType,
14+
BaseRecord,
1515
} from '@aws-lambda-powertools/batch';
1616
import type { SQSEvent, Context, SQSBatchResponse } from 'aws-lambda';
1717

@@ -27,7 +27,7 @@ class MyPartialProcessor extends BasePartialProcessor {
2727
}
2828

2929
public async asyncProcessRecord(
30-
_record: EventSourceType
30+
_record: BaseRecord
3131
): Promise<SuccessResponse | FailureResponse> {
3232
throw new Error('Not implemented');
3333
}
@@ -69,9 +69,7 @@ class MyPartialProcessor extends BasePartialProcessor {
6969
* Here we are keeping the status of each run, `this.handler` is
7070
* the function that is passed when calling `processor.register()`.
7171
*/
72-
public processRecord(
73-
record: EventSourceType
74-
): SuccessResponse | FailureResponse {
72+
public processRecord(record: BaseRecord): SuccessResponse | FailureResponse {
7573
try {
7674
const result = this.handler(record);
7775

docs/snippets/batch/gettingStartedDynamoDBStreams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
DynamoDBBatchResponse,
1212
} from 'aws-lambda';
1313

14-
const processor = new BatchProcessor(EventType.DynamoDBStreams);
14+
const processor = new BatchProcessor(EventType.DynamoDBStreams); // (1)!
1515
const logger = new Logger();
1616

1717
const recordHandler = (record: DynamoDBRecord): void => {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
BatchProcessor,
3+
EventType,
4+
processPartialResponse,
5+
} from '@aws-lambda-powertools/batch';
6+
import { Logger } from '@aws-lambda-powertools/logger';
7+
import type {
8+
SQSEvent,
9+
SQSRecord,
10+
Context,
11+
SQSBatchResponse,
12+
} from 'aws-lambda';
13+
14+
const processor = new BatchProcessor(EventType.SQS);
15+
const logger = new Logger();
16+
17+
class InvalidPayload extends Error {
18+
public constructor(message: string) {
19+
super(message);
20+
this.name = 'InvalidPayload';
21+
}
22+
}
23+
24+
const recordHandler = (record: SQSRecord): void => {
25+
const payload = record.body;
26+
if (payload) {
27+
const item = JSON.parse(payload);
28+
logger.info('Processed item', { item });
29+
} else {
30+
// prettier-ignore
31+
throw new InvalidPayload('Payload does not contain minumum required fields'); // (1)!
32+
}
33+
};
34+
35+
export const handler = async (
36+
event: SQSEvent,
37+
context: Context
38+
): Promise<SQSBatchResponse> => {
39+
// prettier-ignore
40+
return processPartialResponse(event, recordHandler, processor, { // (2)!
41+
context,
42+
});
43+
};

docs/snippets/batch/gettingStartedKinesis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
KinesisStreamBatchResponse,
1212
} from 'aws-lambda';
1313

14-
const processor = new BatchProcessor(EventType.KinesisDataStreams);
14+
const processor = new BatchProcessor(EventType.KinesisDataStreams); // (1)!
1515
const logger = new Logger();
1616

1717
const recordHandler = (record: KinesisStreamRecord): void => {

docs/snippets/batch/gettingStartedSQS.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import type {
1111
SQSBatchResponse,
1212
} from 'aws-lambda';
1313

14-
const processor = new BatchProcessor(EventType.SQS);
14+
const processor = new BatchProcessor(EventType.SQS); // (1)!
1515
const logger = new Logger();
1616

17-
const recordHandler = (record: SQSRecord): void => {
17+
// prettier-ignore
18+
const recordHandler = (record: SQSRecord): void => { // (2)!
1819
const payload = record.body;
1920
if (payload) {
2021
const item = JSON.parse(payload);
@@ -26,7 +27,8 @@ export const handler = async (
2627
event: SQSEvent,
2728
context: Context
2829
): Promise<SQSBatchResponse> => {
29-
return processPartialResponse(event, recordHandler, processor, {
30+
// prettier-ignore
31+
return processPartialResponse(event, recordHandler, processor, { // (3)!
3032
context,
3133
});
3234
};

docs/snippets/batch/gettingStartedSQSFifo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
SQSBatchResponse,
1111
} from 'aws-lambda';
1212

13-
const processor = new SqsFifoPartialProcessor();
13+
const processor = new SqsFifoPartialProcessor(); // (1)!
1414
const logger = new Logger();
1515

1616
const recordHandler = (record: SQSRecord): void => {

0 commit comments

Comments
 (0)