Skip to content

Commit

Permalink
infra: remove try-catch on lambda invokes, to have lambdas crash
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolam committed Mar 22, 2019
1 parent dbab5cd commit 1434d03
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 83 deletions.
83 changes: 38 additions & 45 deletions src/serverless-webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,63 +44,56 @@ function invokeLambda(payload) {
module.exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false

try {
const name =
event.headers['x-github-event'] || event.headers['X-GitHub-Event']
const payload =
typeof event.body === 'string' ? JSON.parse(event.body) : event.body
const name =
event.headers['x-github-event'] || event.headers['X-GitHub-Event']
const payload =
typeof event.body === 'string' ? JSON.parse(event.body) : event.body

if (name === 'installation') {
await trackInstall(payload)
if (name === 'installation') {
await trackInstall(payload)

return {
statusCode: 200,
body: 'Tracked install count',
}
return {
statusCode: 200,
body: 'Tracked install count',
}
}

if (name !== 'issue_comment') {
return {
statusCode: 201,
body: 'Not an issue comment, exiting',
}
if (name !== 'issue_comment') {
return {
statusCode: 201,
body: 'Not an issue comment, exiting',
}
}

if (payload.action !== 'created') {
return {
statusCode: 201,
body: 'Not a comment creation, exiting',
}
if (payload.action !== 'created') {
return {
statusCode: 201,
body: 'Not a comment creation, exiting',
}
}

if (payload.sender.type !== 'User') {
return {
statusCode: 201,
body: 'Not from a user, exiting',
}
if (payload.sender.type !== 'User') {
return {
statusCode: 201,
body: 'Not from a user, exiting',
}
}

const commentBody = payload.comment.body
if (!isMessageForBot(commentBody)) {
return {
statusCode: 202,
body: 'Message not for us, exiting',
}
const commentBody = payload.comment.body
if (!isMessageForBot(commentBody)) {
return {
statusCode: 202,
body: 'Message not for us, exiting',
}
}

await invokeLambda({
name,
payload,
})
await invokeLambda({
name,
payload,
})

return {
statusCode: 200,
body: 'Accepted and processing comment',
}
} catch (error) {
return {
statusCode: 500,
body: error.message,
}
return {
statusCode: 200,
body: 'Accepted and processing comment',
}
}
37 changes: 15 additions & 22 deletions src/tasks/stats/serverless-stats-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,20 @@ async function getStats(probot) {
module.exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false

try {
const probot = getProbot()
const stats = await getStats(probot)
console.log(stats) // eslint-disable-line no-console

await s3
.putObject({
Bucket: process.env.STATS_BUCKET,
Key: 'stats.json',
Body: JSON.stringify(stats),
})
.promise()

return {
statusCode: 200,
body: JSON.stringify(stats),
}
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error.message),
}
const probot = getProbot()
const stats = await getStats(probot)
console.log(stats) // eslint-disable-line no-console

await s3
.putObject({
Bucket: process.env.STATS_BUCKET,
Key: 'stats.json',
Body: JSON.stringify(stats),
})
.promise()

return {
statusCode: 200,
body: JSON.stringify(stats),
}
}
25 changes: 9 additions & 16 deletions src/tasks/stats/serverless-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@ const s3 = new AWS.S3({ apiVersion: '2006-03-01' })
module.exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false

try {
const data = await s3
.getObject({
Bucket: process.env.STATS_BUCKET,
Key: 'stats.json',
})
.promise()
const data = await s3
.getObject({
Bucket: process.env.STATS_BUCKET,
Key: 'stats.json',
})
.promise()

return {
statusCode: 200,
body: data.Body.toString('utf-8'),
}
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error.message),
}
return {
statusCode: 200,
body: data.Body.toString('utf-8'),
}
}

0 comments on commit 1434d03

Please sign in to comment.