Repository navigation
fix: lambda script #61
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| #!/bin/bash | ||
| set -e | ||
| docker buildx build --platform linux/arm64 --provenance=false -f Dockerfile.script -t lambda-script:test . | ||
| docker run --platform linux/arm64 -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \ | ||
| --entrypoint /aws-lambda/aws-lambda-rie \ | ||
| lambda-script:test \ | ||
| /usr/local/bin/npx aws-lambda-ric dist/lambda-handler.handler | ||
| docker buildx build --platform linux/amd64 --provenance=false -f Dockerfile.script -t pwp/pexels-image-scraper-lambda . | ||
| # docker run --platform linux/amd64 -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \ | ||
| # --entrypoint /aws-lambda/aws-lambda-rie \ | ||
| # lambda-script:test \ | ||
| # /usr/local/bin/npx aws-lambda-ric dist/lambda-handler.handler | ||
|
|
||
| docker tag pwp/pexels-image-scraper-lambda:latest 279892746640.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest | ||
|
|
||
| docker push 279892746640.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,18 +51,68 @@ async function findLoadMoreButton( | |
|
|
||
| async function getImageLinksPlaywright(url: string): Promise<string[]> { | ||
| let browser = null; | ||
| let context = null; | ||
| let page = null; | ||
|
|
||
| // Set environment variable to use system Chromium in Lambda | ||
| process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH || '/ms-playwright/chromium-*/chrome-linux/chrome'; | ||
|
||
|
|
||
| try { | ||
| console.log("Environment:", { | ||
| PLAYWRIGHT_BROWSERS_PATH: process.env.PLAYWRIGHT_BROWSERS_PATH, | ||
| PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH, | ||
| NODE_ENV: process.env.NODE_ENV, | ||
| }); | ||
|
|
||
| console.log("Launching browser..."); | ||
| const launchStart = Date.now(); | ||
| browser = await chromium.launch({ | ||
| headless: true, | ||
| args: ["--disable-gpu", "--disable-blink-features=AutomationControlled"], | ||
| timeout: 120000, | ||
| args: [ | ||
| "--disable-gpu", | ||
| "--disable-blink-features=AutomationControlled", | ||
| "--no-sandbox", | ||
| "--disable-setuid-sandbox", | ||
| "--disable-dev-shm-usage", | ||
| "--disable-software-rasterizer", | ||
| "--disable-extensions", | ||
| "--disable-background-networking", | ||
| "--disable-sync", | ||
| "--metrics-recording-only", | ||
| "--mute-audio", | ||
| "--no-first-run", | ||
| "--safebrowsing-disable-auto-update", | ||
| "--disable-component-update", | ||
| "--disable-default-apps", | ||
| "--no-zygote", | ||
| "--disable-accelerated-2d-canvas", | ||
| "--disable-webgl", | ||
| ], | ||
| }); | ||
| const launchDuration = Date.now() - launchStart; | ||
| console.log(`Browser launched successfully in ${launchDuration}ms`); | ||
|
|
||
| if (!browser.isConnected()) { | ||
| throw new Error("Browser disconnected after launch"); | ||
| } | ||
|
|
||
| const context = await browser.newContext({ | ||
| console.log("Creating browser context..."); | ||
| context = await browser.newContext({ | ||
| userAgent: | ||
| "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", | ||
| }); | ||
|
|
||
| const page = await context.newPage(); | ||
| console.log("Browser context created successfully"); | ||
|
|
||
| console.log("Creating new page..."); | ||
| try { | ||
| page = await context.newPage(); | ||
| console.log("New page created successfully"); | ||
| } catch (pageError) { | ||
| console.error("Failed to create new page"); | ||
| console.error(`Browser connected: ${browser.isConnected()}`); | ||
| throw pageError; | ||
| } | ||
|
Comment on lines
+108
to
+115
|
||
|
|
||
| console.log("Fetching URL with Playwright..."); | ||
| await page.goto(url, { | ||
|
|
@@ -128,10 +178,27 @@ async function getImageLinksPlaywright(url: string): Promise<string[]> { | |
| return processedImgLinks; | ||
| } catch (error) { | ||
| console.error(`An error occurred: ${error}`); | ||
| if (error instanceof Error) { | ||
| console.error(`Error stack: ${error.stack}`); | ||
| } | ||
| return []; | ||
| } finally { | ||
| if (browser) { | ||
| await browser.close(); | ||
| console.log("Cleaning up resources..."); | ||
| try { | ||
| if (page) { | ||
| await page.close(); | ||
| console.log("Page closed"); | ||
| } | ||
| if (context) { | ||
| await context.close(); | ||
| console.log("Context closed"); | ||
| } | ||
| if (browser) { | ||
| await browser.close(); | ||
| console.log("Browser closed"); | ||
| } | ||
| } catch (cleanupError) { | ||
| console.error(`Error during cleanup: ${cleanupError}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AWS account ID (279892746640) is hardcoded in the script. This should be parameterized using an environment variable (e.g., AWS_ACCOUNT_ID) to avoid exposing sensitive account information and to make the script reusable across different environments.