Skip to content

Commit

Permalink
Adding timeout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Jan 28, 2025
1 parent 341fc09 commit a2aa744
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
65 changes: 53 additions & 12 deletions src/api/signup-submit.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { google } from 'googleapis';
import { readFileSync } from 'fs';

function checkPromiseState(promise) {
return promise
.then(() => "fulfilled")
.catch(() => "rejected");
}


async function updateAttendees(api, calendarId, eventId, newAttendees) {
console.log(`Adding attendees to `, eventId, newAttendees, new Date().toISOString)
console.log(`Adding attendees to `, eventId, newAttendees, new Date().toISOString())

await api.events.patch({
const response = await api.events.patch({
calendarId: calendarId,
eventId: eventId,
resource: {
Expand All @@ -13,7 +20,14 @@ async function updateAttendees(api, calendarId, eventId, newAttendees) {
sendUpdates: 'externalOnly'
})

console.log(`Attendees added to `, eventId, new Date().toISOString)
console.log(`Response from patch:`, response)

if (response.status !== 200) {
console.error(`Failed to update event ${eventId}`);
throw new Error(`Error: ${response.status} ${response.statusText}`);
} else {
return 'ok';
}
}

async function addAttendeeToEvent(api, calendarId, event, email, addForReal) {
Expand All @@ -29,7 +43,7 @@ async function addAttendeeToEvent(api, calendarId, event, email, addForReal) {
const eventId = event.id

if (addForReal) {
return updateAttendees(api, calendarId, eventId, newAttendees)
return await updateAttendees(api, calendarId, eventId, newAttendees)
}
}

Expand All @@ -53,10 +67,18 @@ async function updateEventRequest(api, calendarId, eventId, email) {

console.log(`Master Event:`, masterEvent)

await addAttendeeToEvent(api, calendarId, masterEvent, email, true)
return await addAttendeeToEvent(api, calendarId, masterEvent, email, true)
}

export default async function handler(req, res) {

/**
* After 10 seconds we return a response to the user
*/
const timeout = 10000;

const TIMEOUT_OCCURRED = 'TIMEOUT'

console.log(`submitted form`, req.body, __filename)

const calendarId =
Expand Down Expand Up @@ -87,13 +109,32 @@ export default async function handler(req, res) {

const individualEvents = eventId.split(',')

try {
for (const individualEventId of individualEvents) {
await updateEventRequest(api, calendarId, individualEventId, req.body.email)
}
const timeoutPromise = new Promise((resolve) => {
setTimeout(() => { resolve(TIMEOUT_OCCURRED) }, timeout)
});

const updatePromises = individualEvents.map(async (e) => await updateEventRequest(api, calendarId, e, req.body.email))

const results = await Promise.race([
Promise.allSettled(updatePromises),
timeoutPromise
])

res.json(`success`)
} catch (err) {
res.json(`Error updating calendar: ${err}`)
console.log(`Results at time of return: |${results}|`)
console.log("UpdatePromises at time of return: ", updatePromises)

if (results === TIMEOUT_OCCURRED) {
const successfulUpdates = updatePromises.filter(result => checkPromiseState(result) === "fulfilled");
res.json(`Timeout reached. ${successfulUpdates.length} updates completed within 10 seconds - the others should complete shortly.` + errorDetails)
} else {
const failedUpdates = results.filter(result => result.status === "rejected");
const errorDetails = JSON.stringify(failedUpdates)

if (failedUpdates.length > 0) {
res.json(`${failedUpdates.length} updates failed:` + errorDetails);
} else {
res.json(`success`);
}
}

}
7 changes: 5 additions & 2 deletions src/pages/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function App({ location }) {
headers: {
"content-type": `application/json`,
},
signal: AbortSignal.timeout(50000)
signal: AbortSignal.timeout(120000)
})
.then(res => res.json())
.then(body => {
Expand Down Expand Up @@ -49,7 +49,10 @@ export default function App({ location }) {
)
} else {
return (
<div className="signup form-container"><pre>{isSubmitted}</pre></div>
<div className="signup result-container">
<h2>Possible Issues</h2>
<h3>If you need support, please relay this info to help@finos.org:</h3>
<pre>{isSubmitted}</pre></div>
)
}
} else {
Expand Down
15 changes: 14 additions & 1 deletion src/styles/signup.css
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
.signup.form-container {
max-width: 400px;
margin: 0 auto;
margin: 2rem auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.signup.result-container {
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin: 2rem;
}

pre {
text-wrap: auto;
overflow: scroll;
}

.signup h2 {
text-align: center;
margin-bottom: 20px;
Expand Down

0 comments on commit a2aa744

Please sign in to comment.