Skip to content

Commit

Permalink
Fix potential hard timeouts (#2072)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmgdr authored and celo-ci-bot-user committed Dec 5, 2019
1 parent 6980fd8 commit ddb7f4a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Binary file modified packages/web/server-env-config.js.enc
Binary file not shown.
9 changes: 6 additions & 3 deletions packages/web/server/EventHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import airtableInit, { AirRecord } from '../server/airtable'

import getConfig from 'next/config'
import { EventProps } from '../fullstack/EventProps'
import Sentry from '../fullstack/sentry'
import airtableInit, { AirRecord } from '../server/airtable'
import { abort } from '../src/utils/abortableFetch'
const TABLE_NAME = 'Community Calendar'
// Intermediate step Event With all String Values
interface IncomingEvent {
Expand Down Expand Up @@ -57,7 +57,10 @@ export interface RawAirTableEvent {
}

export default async function getFormattedEvents() {
const eventData = await fetchEventsFromAirtable()
const eventData = await Promise.race([
fetchEventsFromAirtable(),
abort('Events from Airtable', 1000),
])
return splitEvents(normalizeEvents(eventData as RawAirTableEvent[]))
}

Expand Down
16 changes: 12 additions & 4 deletions packages/web/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,21 @@ function wwwRedirect(req, res, nextAction) {
})

server.get('/proxy/medium', async (_, res) => {
const articlesdata = await getFormattedMediumArticles()
res.json(articlesdata)
try {
const articlesdata = await getFormattedMediumArticles()
res.json(articlesdata)
} catch (e) {
res.status(e.statusCode || 500).json({ message: e.message || 'unknownError' })
}
})

server.get('/proxy/events/', async (_, res) => {
const events = await getFormattedEvents()
res.json(events)
try {
const events = await getFormattedEvents()
res.json(events)
} catch (e) {
res.status(e.statusCode || 500).json({ message: e.message || 'unknownError' })
}
})

server.get('*', (req, res) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/utils/abortableFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export default async function abortableFetch(url: string, options = {}) {
return Promise.race([fetch(url, { ...options }), abort(url)])
}

export async function abort(url: string, milliseconds = 800) {
export async function abort(message: string, milliseconds = 800) {
return new Promise((_, reject) =>
setTimeout(() => {
reject(new Error(`from abortableFetch: Took to Long to Fetch ${url}`))
reject(new Error(`from abortableFetch: Took to Long to Fetch ${message}`))
}, milliseconds)
)
}

0 comments on commit ddb7f4a

Please sign in to comment.