-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfail-pin.js
More file actions
52 lines (50 loc) · 1.59 KB
/
fail-pin.js
File metadata and controls
52 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import retry from 'p-retry'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocumentClient, UpdateCommand } from '@aws-sdk/lib-dynamodb'
/**
* @param {import('aws-lambda').SQSEvent} sqsEvent
* @returns {Promise<import('aws-lambda').SQSBatchResponse>}
*/
export async function sqsPinQueueDeadLetterHandler (sqsEvent) {
const {
TABLE_NAME: table = '',
DYNAMO_DB_ENDPOINT: dbEndpoint = undefined
} = process.env
const dynamo = new DynamoDBClient({ endpoint: dbEndpoint })
/** @type {import('aws-lambda').SQSBatchItemFailure[]} */
const batchItemFailures = []
for (const msg of sqsEvent.Records) {
const { cid } = JSON.parse(msg.body)
try {
await retry(() => updatePinStatus(dynamo, table, cid, 'failed'), { retries: 3 })
} catch (err) {
console.error(err)
batchItemFailures.push({ itemIdentifier: msg.messageId })
}
}
// https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting
return { batchItemFailures }
}
/**
* Update the pin status for a given CID
*
* @param {DynamoDBClient} dynamo
* @param {string} table
* @param {string} cid
* @param {import('./schema').Pin["status"]} status
*/
export async function updatePinStatus (dynamo, table, cid, status) {
const client = DynamoDBDocumentClient.from(dynamo)
return client.send(new UpdateCommand({
TableName: table,
Key: { cid },
ExpressionAttributeNames: {
'#status': 'status'
},
ExpressionAttributeValues: {
':s': status
},
UpdateExpression: 'set #status = :s',
ReturnValues: 'ALL_NEW'
}))
}