0

I have the following code that works:

var Path = require('path')
var Phantomjs = require('phantomjs2')
var phantomjsPath = Phantomjs.path
var childArgs = [
      Path.join(__dirname, 'phantomjs-worker.js'),
      'http://...some login url...',
      3000, //login timeout
      'http://...some address to render as image...',
      5000, //address timeout
      '...output image file path...',
      1000, //page width
      1000, //page height
      1234 //some id
    ]            
var child = Proc.spawn(phantomjsPath, childArgs, { cwd: process.cwd() })

Inside phantom-worker.js:

var argsOffset = 0;
var login = system.args[argsOffset + 1];
var logintimeout = system.args[argsOffset + 2];
var address = system.args[argsOffset + 3];
var addresstimeout = system.args[argsOffset + 4];
var output = system.args[argsOffset + 5];
var pageWidth = parseInt(system.args[argsOffset + 6]);
var pageHeight = parseInt(system.args[argsOffset + 7]);
var pageId = system.args[argsOffset + 8];
...

I need to be able to pass in

--ignore-ssl-errors=true --ssl-protocol=tlsv1

as well.

I tried adding these as the first 2 arguments but it doesn't work. It starts looking for the output image file path for some reason and obviously fails.

Is there any way to pass in these command line arguments along with the script and its child arguments?

7
  • Shouldn't argsOffset = 2 in that case? Commented May 17, 2017 at 12:31
  • Tried that as well. It still counts the script as index 0 and the following items from 1. Commented May 17, 2017 at 13:30
  • Putting phantomjs params as first elements of childArgs (before Path.join) seems working. Maybe your case is more complicated, sorry. Commented May 17, 2017 at 13:55
  • I'll try that again. Thanks Commented May 17, 2017 at 16:56
  • 1
    Yes, actually got it working simply with the arguments as the first few arguments, and no need to change the offset. I'll post the answer shortly. The issue was related to phantom crashing due to low memory and the parent app looking for the rendered file which obviously wasn't there. Commented May 31, 2017 at 20:03

1 Answer 1

1

The following works as expected. The error was not in this part of the code. Rather, the phantom process was exiting due to memory issues causing the rendered image file to be not created.

var Path = require('path')
var Phantomjs = require('phantomjs2')
var phantomjsPath = Phantomjs.path
var childArgs = [
      '--ignore-ssl-errors=true',
      '--ssl-protocol=any',
      '--web-security=false',
      Path.join(__dirname, 'phantomjs-worker.js'),
      'http://...some login url...',
      3000, //login timeout
      'http://...some address to render as image...',
      5000, //address timeout
      '...output image file path...',
      1000, //page width
      1000, //page height
      1234 //some id
    ]            
var child = Proc.spawn(phantomjsPath, childArgs, { cwd: process.cwd() })

Worker:

var webpage = require('webpage'),
    system = require('system');

var login = system.args[1];
var logintimeout = system.args[2];
var address = system.args[3];
var addresstimeout = system.args[4];
var output = system.args[5];
var pageWidth = parseInt(system.args[6]);
var pageHeight = parseInt(system.args[7]);
var pageId = system.args[8];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.