Skip to content

docs(batch): added flow charts #1640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/snippets/batch/accessProcessedMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ export const handler = async (
event: SQSEvent,
context: Context
): Promise<SQSBatchResponse> => {
const batch = event.Records;
const batch = event.Records; // (1)!

processor.register(batch, recordHandler, { context });
processor.register(batch, recordHandler, { context }); // (2)!
const processedMessages = processor.process();

for (const message of processedMessages) {
const status: 'success' | 'fail' = message[0];
const error = message[1];
const record = message[2];

logger.info('Processed record', { status, record });
logger.info('Processed record', { status, record, error });
}

return processor.response();
Expand Down
8 changes: 3 additions & 5 deletions docs/snippets/batch/customPartialProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import type {
SuccessResponse,
FailureResponse,
EventSourceType,
BaseRecord,
} from '@aws-lambda-powertools/batch';
import type { SQSEvent, Context, SQSBatchResponse } from 'aws-lambda';

Expand All @@ -27,7 +27,7 @@ class MyPartialProcessor extends BasePartialProcessor {
}

public async asyncProcessRecord(
_record: EventSourceType
_record: BaseRecord
): Promise<SuccessResponse | FailureResponse> {
throw new Error('Not implemented');
}
Expand Down Expand Up @@ -69,9 +69,7 @@ class MyPartialProcessor extends BasePartialProcessor {
* Here we are keeping the status of each run, `this.handler` is
* the function that is passed when calling `processor.register()`.
*/
public processRecord(
record: EventSourceType
): SuccessResponse | FailureResponse {
public processRecord(record: BaseRecord): SuccessResponse | FailureResponse {
try {
const result = this.handler(record);

Expand Down
2 changes: 1 addition & 1 deletion docs/snippets/batch/gettingStartedDynamoDBStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
DynamoDBBatchResponse,
} from 'aws-lambda';

const processor = new BatchProcessor(EventType.DynamoDBStreams);
const processor = new BatchProcessor(EventType.DynamoDBStreams); // (1)!
const logger = new Logger();

const recordHandler = (record: DynamoDBRecord): void => {
Expand Down
43 changes: 43 additions & 0 deletions docs/snippets/batch/gettingStartedErrorHandling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
BatchProcessor,
EventType,
processPartialResponse,
} from '@aws-lambda-powertools/batch';
import { Logger } from '@aws-lambda-powertools/logger';
import type {
SQSEvent,
SQSRecord,
Context,
SQSBatchResponse,
} from 'aws-lambda';

const processor = new BatchProcessor(EventType.SQS);
const logger = new Logger();

class InvalidPayload extends Error {
public constructor(message: string) {
super(message);
this.name = 'InvalidPayload';
}
}

const recordHandler = (record: SQSRecord): void => {
const payload = record.body;
if (payload) {
const item = JSON.parse(payload);
logger.info('Processed item', { item });
} else {
// prettier-ignore
throw new InvalidPayload('Payload does not contain minumum required fields'); // (1)!
}
};

export const handler = async (
event: SQSEvent,
context: Context
): Promise<SQSBatchResponse> => {
// prettier-ignore
return processPartialResponse(event, recordHandler, processor, { // (2)!
context,
});
};
2 changes: 1 addition & 1 deletion docs/snippets/batch/gettingStartedKinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
KinesisStreamBatchResponse,
} from 'aws-lambda';

const processor = new BatchProcessor(EventType.KinesisDataStreams);
const processor = new BatchProcessor(EventType.KinesisDataStreams); // (1)!
const logger = new Logger();

const recordHandler = (record: KinesisStreamRecord): void => {
Expand Down
8 changes: 5 additions & 3 deletions docs/snippets/batch/gettingStartedSQS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import type {
SQSBatchResponse,
} from 'aws-lambda';

const processor = new BatchProcessor(EventType.SQS);
const processor = new BatchProcessor(EventType.SQS); // (1)!
const logger = new Logger();

const recordHandler = (record: SQSRecord): void => {
// prettier-ignore
const recordHandler = (record: SQSRecord): void => { // (2)!
const payload = record.body;
if (payload) {
const item = JSON.parse(payload);
Expand All @@ -26,7 +27,8 @@ export const handler = async (
event: SQSEvent,
context: Context
): Promise<SQSBatchResponse> => {
return processPartialResponse(event, recordHandler, processor, {
// prettier-ignore
return processPartialResponse(event, recordHandler, processor, { // (3)!
context,
});
};
Expand Down
2 changes: 1 addition & 1 deletion docs/snippets/batch/gettingStartedSQSFifo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
SQSBatchResponse,
} from 'aws-lambda';

const processor = new SqsFifoPartialProcessor();
const processor = new SqsFifoPartialProcessor(); // (1)!
const logger = new Logger();

const recordHandler = (record: SQSRecord): void => {
Expand Down
Loading