I have written a function in node.js that works well when I run it locally (~10s to run).
As I want to run it every hour, I have deployed it on Google Cloud Platform. But, there, I always have a TimeOut error.
Therefore, do you have any advice on:
- what I should change in my function to make it more efficient?
- a alternate way to automate my function so it runs every hour?
FYI my cloud function has the following characteristics:
- Node js 8
- Memory: 2Go
- Timeout: 540 seconds
and the following form:
exports.launchSearch = (req, res) => {
const puppeteer = require('puppeteer');
const url = require('./pageInformation').url;
const pageLocation = require('./pageInformation').location;
const userInformation = require('./userInformation').information;
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto(url);
// Part 1
await page.click(pageLocation['...']);
await page.type(pageLocation['...'], userInformation['...']);
await page.waitFor(pageLocation['...']);
await page.click(pageLocation['...']);
... ~20 other "page.click" or "page.select"
// Part 2
var continueLoop = true;
while (continueLoop) {
var list = await page.$x(pageLocation['...']);
if (list.length > 0) {
await list[0].click();
var found = true;
var continueLoop = false;
} else {
var afficher = await page.$x(pageLocation['...']);
if (afficher.length > 0) {
await afficher[0].click();
} else {
var continueLoop = false;
var found = false;
};
};
};
// Part 3
if (found) {
await page.waitForXPath(pageLocation['...']);
const xxx = await page.$x(pageLocation['...']);
await xxx[0].click();
... 5 other blocks with exact same 3 lines, but with other elements to click
};
await browser.close();
})();
};
I have tried to run it part by part; sometimes it times out at the end of Part 1, sometimes at the end of Part 2. But the whole script never entirely completed.