AWS Lambda上的PhantomJS始终超时

Nat*_*orn 5 lambda amazon-web-services node.js phantomjs

我尝试在AWS Lambda上创建一个任务,从PhantomJS创建PDF文件,然后再将其上传到AWS S3.

现在,我尝试在Lambda上运行它,但它始终是Timeout.

我的Lambda有128mb的ram.运行时是node.js 4.4.3.

这是我从Lambda得到的错误

"errorMessage": "2017-03-01T08:05:56.255Z dfd4cfe8-fe55-11e6-bf24-e7edf412e037 Task timed out after 10.00 seconds"
Run Code Online (Sandbox Code Playgroud)

这些也是日志输出

REPORT RequestId: dfd4cfe8-fe55-11e6-bf24-e7edf412e037  Duration: 10000.08 ms   Billed Duration: 10000 ms   Memory Size: 128 MB Max Memory Used: 29 MB


2017-03-01T08:05:56.255Z dfd4cfe8-fe55-11e6-bf24-e7edf412e037 Task timed out after 10.00 seconds
Run Code Online (Sandbox Code Playgroud)

这是我的代码.

Index.js

var childProcess = require('child_process');
var path = require('path');

exports.handler = function(event, context, callback) {

     // Set the path as described here: https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];

     // Set the path to the phantomjs binary
     var phantomPath = path.join(__dirname, 'phantomjs_linux-x86_64');

     // Arguments for the phantom script
    var processArgs = [
         path.join(__dirname, 'phantom-script.js'),
         event.url
    ];

    // Launc the child process
    childProcess.execFile(phantomPath, processArgs, function(error, stdout, stderr) {
        if (error) {
            context.fail(error);
            return;
        }
        if (stderr) {
            context.fail(error);
            return;
        }
        context.succeed(stdout);
    });
}
Run Code Online (Sandbox Code Playgroud)

幻像的script.js

 var system = require('system');
 var args = system.args;

 // Example of how to get arguments passed from node script
 // args[0] would be this file's name: phantom-script.js

 const url = "https://google.com";

 system.stdout.write('hello from phantom!');

 console.log("task start, target url = " + url);

 console.time("execute time");
 phantom.create().then(function(ph) {
     console.time("create Page");
     ph.createPage().then(function(page) {
         console.timeEnd("create Page");
         console.time("open website");
         page.open(url).then(function(status) {
             console.timeEnd("open website");
             console.time("render as pdf");
             page.render('google.pdf').then(function() {
                 console.timeEnd("render as pdf");
                 console.log('Page Rendered');
                 ph.exit();

                 // send to s3
                 console.timeEnd("execute time");
             });
         });
     });
 });


 // Send some info node's childProcess' stdout
 system.stdout.write('hello from phantom!')

 phantom.exit();
Run Code Online (Sandbox Code Playgroud)

我尝试按照这个答案做我的工作,但它不起作用.

我没有得到任何记录,phantom-script.js因为它不是触发器,但我的任务总是超时.

Nat*_*orn 3

在我花了很多时间在上面之后。我找到了包名称Phantomjs-Prebuilt,您可以通过npm. 您必须npm install在具有节点版本 4.x 的 amazon linux 实例或 docker amazon linux 上执行此操作(lambda 使用节点版本 4.3)。否则它将无法在 lambda 上运行。

然后,我像这样更新了我的代码。

索引.js

var phantomjs = require('phantomjs-prebuilt')

exports.handler = function(event, context, callback) {
    var sourceUrl = "https://example.com"
    var program = phantomjs.exec('phantom-script.js', sourceUrl)
    program.stdout.pipe(process.stdout)
    program.stderr.pipe(process.stderr)
    program.on('exit', code => {
        // do something here after you phantomjs finish.
        return
    })
}
Run Code Online (Sandbox Code Playgroud)

幻影脚本.js

var system = require('system')
var args = system.args

// Example of how to get arguments passed from node script
// args[0] would be this file's name: phantom-script.js

var url = args[1] // received sourceUrl value

// Send some info node's childProcess' stdout
system.stdout.write('phantomJS running\r\n')

var webpage = require('webpage').create()

webpage.paperSize = {
    format: 'A4',
    orientation: 'landscape'
}

webpage.open(url, function(status) {
    system.stdout.write('start open page\r\n')
    webpage.render('/tmp/web.pdf', {
        format: 'pdf',
        quality: '100'
    })
    system.stdout.write('finish render page\r\n')
    phantom.exit()
})
Run Code Online (Sandbox Code Playgroud)

在 lambda 上,您可以写入文件的地方是/tmp文件夹,这就是我将文件保存在那里的原因。

我通过 lambda 和 192mb 的内存运行这个程序。做工真的很好啊 我可以使用此设置创建包含 500 张图像的网页的屏幕截图。最重要的是确保您的 lambda 能够连接互联网。

仅供参考,我意识到当 phantom-script.js (我在其中编写幻像脚本的文件)出现错误时,您的 lambda 将冻结直到超时。这就是为什么我总是从 lambda 得到这样的回应Task timed out after 10.00 seconds。