Reputation: 279
I am currently running a function on aws lambda. The strange thing is, after each successful execution, it will result in a time out if I run it again. And after each time out runs, when I run the test again, it became successful.
At times when the function resulted in time out, lambda didn't seem to do anything. I put a few console.log in my function and none of them got printed.
My function can usually be finished in around 30 seconds, so timing out a 60 seconds period seems rather strange to me.
Does anyone know what could have happened?
This is the code for my function:
const puppeteer = require('puppeteer-lambda');
const test1 = require("./test1.js");
const test2 = require("./test2.js")
exports.handler = async function(event, context) {
let browser = await puppeteer.getBrowser({headless: true});
//run test
let tests = [test1.test, test2.test];
//run all test cases in parallel
let result = await Promise.all(tests.map(test => test(browser)));
//output logs of each test case
for (let log of result) {
console.log(log);
}
await browser.close();
console.log("All tests finish running");
}
Upvotes: 2
Views: 945
Reputation: 279
As a followup:
One way I found successful is like what @K Mo suggested -- never close the browser. However, my test performs log in to certain websites, and reusing the same browser results in inconsistent behavior since log in is required for the first run but not for the second run.
What I eventually did is to use puppeteer instead of puppeteer-lambda. I used a plugin from https://github.com/Tchangang/puppeteer-lambda-launcher to help me configure and download chrome from a s3 bucket.
Thank everyone in the comment for the help! My test is now running well!
Upvotes: 0
Reputation: 2155
There are known issues with using puppeteer-lambda to open a browser, close it then open it again.
What you are seeing is the second invocation of your Lambda failing because it is being run in the same container as the first 'successful' invocation.
Then why would the third try work? Because the second invocation of the Lambda is killed due to timeout, probably along with whatever temp info that is causing the error in the first place.
You could try instantiating the browser outside of the call handler and just never closing it, so it is available for the next time you run the tests. I don't know if there will be any time out issues with this though.
Upvotes: 1