-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create dev environment * Add webhook support * Add test webhook * Deploy webhook handler * Add runPriority back * Add trailId to trail status response * Fix * Save webhook run state * Create a cache for each request rather than lambda instance
- Loading branch information
Showing
22 changed files
with
611 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
PROJECT=TrailStatusAppDev | ||
|
||
TLD=localhost | ||
TLD= | ||
API_SUBDOMAIN= | ||
API_ENDPOINT=https://${API_SUBDOMAIN}.${TLD} | ||
SSL_ARN= | ||
|
||
# Removes dynamodb tables when tearing down | ||
USER_RESOURCE_REMOVAL_POLICY=destroy | ||
USER_RESOURCE_REMOVAL_POLICY=retain | ||
|
||
API_PORT=4000 | ||
API_ENDPOINT=https://${TLD}:${API_PORT} | ||
|
||
FRONTEND_PORT=3000 | ||
FRONTEND_ENDPOINT=https://${TLD}:${FRONTEND_PORT} | ||
FRONTEND_ENDPOINT=https://${TLD} |
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,12 @@ | ||
PROJECT=TrailStatusAppDev | ||
|
||
TLD=localhost | ||
|
||
# Removes dynamodb tables when tearing down | ||
USER_RESOURCE_REMOVAL_POLICY=destroy | ||
|
||
API_PORT=4000 | ||
API_ENDPOINT=https://${TLD}:${API_PORT} | ||
|
||
FRONTEND_PORT=3000 | ||
FRONTEND_ENDPOINT=https://${TLD}:${FRONTEND_PORT} |
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ data | |
.cache | ||
out | ||
*.env | ||
*.env.local | ||
*.env.dev | ||
*.env.production | ||
*.log | ||
|
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,82 @@ | ||
import fetch from 'node-fetch'; | ||
import withSQSHandler from '../withSQSHandler'; | ||
import WebhookModel from '../models/WebhookModel'; | ||
import TrailStatusModel from '../models/TrailStatusModel'; | ||
|
||
export default withSQSHandler(async event => { | ||
if (!Array.isArray(event?.Records) || !event.Records.length) { | ||
console.info(`Received empty messages, do nothing.`); | ||
return; | ||
} | ||
|
||
// Messages should be grouped by trail id so cache the trail status to | ||
// avoid unecessary db lookups while processing the webhooks for a trail. | ||
const getTrailStatus = createTrailStatusCache(); | ||
|
||
for (const message of event.Records) { | ||
let webhookId: string | null = null; | ||
try { | ||
({ webhookId } = JSON.parse(message.body)); | ||
} catch (err) { | ||
console.error(`Failed to parse message body '${message.body}'`); | ||
} | ||
|
||
if (!webhookId) { | ||
console.error(`Missing webhookId in message body '${message.body}'`); | ||
continue; | ||
} | ||
|
||
const webhook = await WebhookModel.get(webhookId); | ||
if (!webhook) { | ||
console.error(`Failed to find webhook for '${webhookId}'`); | ||
continue; | ||
} | ||
|
||
const trailStatus = await getTrailStatus(webhook.trailId); | ||
if (!trailStatus) { | ||
console.error(`Failed to find trail for '${webhook.trailId}'`); | ||
continue; | ||
} | ||
|
||
const res = await fetch(`${webhook.url}`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(trailStatus), | ||
}); | ||
|
||
// If the webhook fails throw an error. This triggers the message batch to be retried. | ||
// There should only be one message per batch but this handles multiple. If multiple | ||
// messages are sent in the batch and one fails, the entire batch is re-tried. Webhooks | ||
// should not expect to messages to be delivered only once. | ||
if (!res.ok) { | ||
const errorMessage = `Failed to process webhook '${webhookId}', invalid response '${res.status}' from '${webhook.url}'`; | ||
await webhook.save({ | ||
lastRanAt: new Date().toISOString(), | ||
error: errorMessage, | ||
}); | ||
|
||
throw new Error(errorMessage); | ||
} | ||
|
||
await webhook.save({ | ||
lastRanAt: new Date().toISOString(), | ||
error: '', | ||
}); | ||
|
||
console.info( | ||
`Successfully ran webhook '${webhookId}', received status '${res.status}' from '${webhook.url}'`, | ||
); | ||
} | ||
}); | ||
|
||
const createTrailStatusCache = () => { | ||
const cache: Record<string, TrailStatusModel | null> = {}; | ||
return async (trailId: string) => { | ||
if (trailId in cache) { | ||
return cache[trailId]; | ||
} | ||
const result = await TrailStatusModel.get(trailId); | ||
cache[trailId] = result; | ||
return result; | ||
}; | ||
}; |
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,8 @@ | ||
import { json } from '../responses'; | ||
import withApiHandler from '../withApiHandler'; | ||
|
||
export default withApiHandler([], async event => { | ||
const payload = event.body; | ||
console.info(`Test webhook received payload '${event.body}'`); | ||
return json(payload); | ||
}); |
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.