Skip to content

Commit 2da4f57

Browse files
committed
using standard logging library, reuse session for performance
1 parent 0194b8d commit 2da4f57

File tree

8 files changed

+51
-51
lines changed

8 files changed

+51
-51
lines changed

examples/visitgoogle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ $browser.findElement($driver.By.name('btnK')).click();
1111
$browser.wait($driver.until.titleIs('Google'), 1000);
1212
$browser.getTitle().then(function(title) {
1313
console.log("title is: " + title);
14+
console.log('Finished running script!');
1415
});
15-
console.log('Finished running script!');

index.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,33 @@ const fs = require('fs');
44

55
const chromium = require('./lib/chromium');
66
const sandbox = require('./lib/sandbox');
7-
const { log } = require('./lib/helpers');
7+
const log = require('lambda-log');
88

9-
console.log('Loading function');
9+
log.info('Loading function');
10+
11+
// Create new session (spawns chromium and webdriver)
12+
$browser = chromium.createSession();
1013

1114
exports.handler = (event, context, callback) => {
1215
context.callbackWaitsForEmptyEventLoop = false;
1316

14-
if (process.env.CLEAR_TMP) {
15-
log('attempting to clear /tmp directory')
16-
log(child.execSync('rm -rf /tmp/core*').toString());
17+
/*if (process.env.CLEAR_TMP) {
18+
log.info('attempting to clear /tmp directory')
19+
log.info(child.execSync('rm -rf /tmp/core*').toString());
20+
}*/
21+
22+
if (process.env.DEBUG_ENV || process.env.SAM_LOCAL) {
23+
log.config.debug = true;
24+
log.config.dev = true;
1725
}
1826

19-
if (process.env.DEBUG_ENV) {
20-
//log(child.execSync('set').toString());
21-
log(child.execSync('pwd').toString());
22-
log(child.execSync('ls -lhtra .').toString());
23-
log(child.execSync('ls -lhtra /tmp').toString());
27+
if (process.env.LOG_DEBUG) {
28+
log.debug(child.execSync('pwd').toString());
29+
log.debug(child.execSync('ls -lhtra .').toString());
30+
log.debug(child.execSync('ls -lhtra /tmp').toString());
2431
}
2532

26-
log('Received event:', JSON.stringify(event, null, 2));
33+
log.info(`Received event: ${JSON.stringify(event, null, 2)}`);
2734

2835
// Read input
2936
const inputParam = event.Base64Script || process.env.BASE64_SCRIPT;
@@ -32,17 +39,13 @@ exports.handler = (event, context, callback) => {
3239
}
3340

3441
const inputBuffer = Buffer.from(inputParam, 'base64').toString('utf8');
35-
if (process.env.DEBUG_ENV) {
36-
log(`Executing "${inputBuffer}"`);
37-
}
42+
log.debug(`Executing script "${inputBuffer}"`);
3843

39-
// Start selenium webdriver session
40-
$browser = chromium.createSession();
4144
sandbox.executeScript(inputBuffer, $browser, webdriver, function(err) {
42-
if (process.env.DEBUG_ENV) {
43-
log(child.execSync('ps aux').toString());
45+
if (process.env.LOG_DEBUG) {
46+
log.debug(child.execSync('ps aux').toString());
47+
log.debug(child.execSync('cat /tmp/chromedriver.log').toString())
4448
}
45-
4649
if (err) {
4750
callback(err, null);
4851
}

lib/chromium.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const child = require('child_process');
2-
const { log } = require('./helpers');
32
const os = require('os');
43
const path = require('path');
54
const chrome = require('selenium-webdriver/chrome');
@@ -44,9 +43,15 @@ const defaultChromeFlags = [
4443
const HEADLESS_CHROME_PATH = 'bin/headless-chromium';
4544
const CHROMEDRIVER_PATH = '/var/task/bin/chromedriver';
4645
exports.createSession = function() {
47-
const service = new chrome.ServiceBuilder(CHROMEDRIVER_PATH)
48-
.enableVerboseLogging()
49-
.build();
46+
var service;
47+
if (process.env.LOG_DEBUG || process.env.SAM_LOCAL) {
48+
service = new chrome.ServiceBuilder(CHROMEDRIVER_PATH)
49+
.loggingTo('/tmp/chromedriver.log')
50+
.build();
51+
} else {
52+
service = new chrome.ServiceBuilder(CHROMEDRIVER_PATH)
53+
.build();
54+
}
5055

5156
const options = new chrome.Options();
5257

@@ -60,16 +65,3 @@ exports.createSession = function() {
6065
return chrome.Driver.createSession(options, service);
6166
}
6267

63-
const spawnProcess = function(localPath, flags) {
64-
const opts = {
65-
cwd: os.tmpdir(),
66-
shell: true,
67-
detached: true,
68-
};
69-
70-
const proc = child.spawn(path.join(process.env.LAMBDA_TASK_ROOT, localPath),
71-
flags,
72-
opts
73-
);
74-
return proc;
75-
};

lib/helpers.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/sandbox.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const vm = require('vm');
2+
const log = require('lambda-log');
23

34
exports.executeScript = function(scriptText, browser, driver, cb) {
45
// Create Sandbox VM
@@ -14,7 +15,7 @@ exports.executeScript = function(scriptText, browser, driver, cb) {
1415
try {
1516
script.runInContext(scriptContext);
1617
} catch (e) {
17-
console.log('[script error]', e);
18+
log.error(`[script error] ${e}`);
1819
return cb(e, null);
1920
}
2021

@@ -27,8 +28,13 @@ exports.executeScript = function(scriptText, browser, driver, cb) {
2728
}
2829
});
2930
*/
30-
31-
$browser.quit().then(function() {
32-
cb(null);
31+
// https://github.com/GoogleChrome/puppeteer/issues/1825#issuecomment-372241101
32+
// Reuse existing session, likely some edge cases around this...
33+
browser.manage().deleteAllCookies().then(function() {
34+
return $browser.get('about:blank').then(function() {
35+
cb(null);
36+
}).catch(function(err) {
37+
cb(err);
38+
});
3339
});
3440
}

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambdium",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "headless chromium in lambda prototype",
55
"main": "index.js",
66
"scripts": {
@@ -9,6 +9,7 @@
99
"author": "Clay Smith",
1010
"license": "ISC",
1111
"dependencies": {
12+
"lambda-log": "^1.3.0",
1213
"selenium-webdriver": "^3.6.0"
1314
}
1415
}

template.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ Resources:
99
Runtime: nodejs6.10
1010
FunctionName: lambdium
1111
Description: headless chromium running selenium
12-
MemorySize: 1024
13-
Timeout: 10
12+
MemorySize: 1156
13+
Timeout: 20
1414
Environment:
1515
Variables:
16-
DEBUG_ENV: "true"
1716
CLEAR_TMP: "true"
1817
CodeUri: .

0 commit comments

Comments
 (0)