Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified retrieval of basic/advanced headers #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/handlers/handle-scheduled-checkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ async function handleInternal(event: AWSLambda.SQSEvent) {
// TODO: is the message nested within the EventBridge message that triggered this message?
assert(Queue.isMessage(body), 'Invalid message');

const basicHeaders = await SwClient.getBasicHeaders();
const advancedHeaders = await SwGenerateHeaders.generateHeaders(body.reservation);

const basicHeaders = await getHeadersWithRetry(
() => SwClient.getBasicHeaders(), "basic headers", 10);
console.debug('basicHeaders', basicHeaders);

const advancedHeaders = await getHeadersWithRetry(
() => SwGenerateHeaders.generateHeaders(body.reservation), "advanced headers", 10);

console.debug('advancedHeaders', advancedHeaders);

const checkinDateTime = Luxon.DateTime.fromSeconds(body.checkin_available_epoch);
Expand Down Expand Up @@ -119,3 +123,23 @@ function adjustMessageVisibilityTimeout(input: AdjustMessageVisibilityTimeoutInp

return client.send(command);
}

async function getHeadersWithRetry<HeaderType>(func: () => Promise<HeaderType>,
headerName: string, maxRetries: int)
{
const waitMs = util.promisify(setTimeout);
while(true)
{
try{
const headers = await func();
return headers;
}
catch(error){
if(maxRetries <= 0)
throw error;
Copy link
Contributor

@swtools0 swtools0 May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than throwing an error, just return here, then update the caller to check if the result is truthy. The handleInternal function should get to decide how to deal with headers not being found (probably throw an error there).

console.debug(`Failed getting ${headerName}. Retrying in 5s...\nException details: ${error}`)
await waitMs(5000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment about why we are waiting

maxRetries--;
}
}
}
2 changes: 2 additions & 0 deletions src/lib/sw-generate-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export async function generateHeaders(reservation: Reservation.Reservation) {
});
const page = await browser.newPage();

page.setDefaultNavigationTimeout(60000);

await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
Expand Down