Possibilities for Managing PhantomJS Processes from Java

207 views
Skip to first unread message

Gerald Wiltse

unread,
Feb 23, 2016, 12:28:16 PM2/23/16
to phantomjs
Our monitoring servers run between 10 and 40 PhantomJS processes at a time (from our Java application.on Windows Server 2012R2).  We have 20 such servers.  They are automating an Oracle ERP application with unique Javascript components, so we have to use GhostDriver for all the verification steps.  We have a recurring problem where about 2-3 times per week, various conditions in the application can cause a phantomjs process to go rogue and start taking 100% CPU until it's killed on one of the servers.  We've tried a lot of different settings around driver.close, driver.quit, pageloadtimeout, and it seems that there will always be conditions that can hang either ghostdriver or phantom (or something).  Sadly, we have been unable to find any way to identify the PID upon creation through the API from our Java program.  If we could do that, we could use process methods to ensure closure and termination of the process from outside the process before moving on.

We're looking for any advice in solving this problem.  We understand that this really isn't really one of the "intended use cases" of PhantomJS, so it may be hard to find advice or help with this issue, so thanks in advance to anyone who is willing to provide assistance. 


Anuj Bhatia

unread,
Feb 24, 2016, 12:56:37 AM2/24/16
to phantomjs
I've been using the Apache Commons Exec (https://commons.apache.org/proper/commons-exec/) library to invoke Phantom JS executable from Java. So far it has been working great without any problems. 

You can use the watch dog feature of this API (https://commons.apache.org/proper/commons-exec/apidocs/org/apache/commons/exec/ExecuteWatchdog.html) to automatically kill processes in case they do not finish processing after a fixed timeout. 

Hope this helps.
 

On Tue, Feb 23, 2016 at 10:58 PM Gerald Wiltse <jerry...@gmail.com> wrote:
Our monitoring servers run between 10 and 40 PhantomJS processes at a time (from our Java application.on Windows Server 2012R2).  We have 20 such servers.  They are automating an Oracle ERP application with unique Javascript components, so we have to use GhostDriver for all the verification steps.  We have a recurring problem where about 2-3 times per week, various conditions in the application can cause a phantomjs process to go rogue and start taking 100% CPU until it's killed on one of the servers.  We've tried a lot of different settings around driver.close, driver.quit, pageloadtimeout, and it seems that there will always be conditions that can hang either ghostdriver or phantom (or something).  Sadly, we have been unable to find any way to identify the PID upon creation through the API from our Java program.  If we could do that, we could use process methods to ensure closure and termination of the process from outside the process before moving on.

We're looking for any advice in solving this problem.  We understand that this really isn't really one of the "intended use cases" of PhantomJS, so it may be hard to find advice or help with this issue, so thanks in advance to anyone who is willing to provide assistance. 


--
You received this message because you are subscribed to the Google Groups "phantomjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phantomjs+...@googlegroups.com.
Visit this group at https://groups.google.com/group/phantomjs.
For more options, visit https://groups.google.com/d/optout.

Gerald Wiltse

unread,
Feb 24, 2016, 8:57:28 AM2/24/16
to phan...@googlegroups.com
Thanks for sharing!

Is your environment similar to mine?  (Windows, 10-40 simultaneous instances, etc?)  Do you obtain the PID through the phantomjs cli API as a safety measure, or just trust in ExecuteWatchdog? 

Do you have any code snippets you would be willing to share?  



Gerald R. Wiltse
jerry...@gmail.com


--
You received this message because you are subscribed to a topic in the Google Groups "phantomjs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phantomjs/5_6i-nOkM7E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phantomjs+...@googlegroups.com.

Anuj Bhatia

unread,
Feb 25, 2016, 11:39:48 PM2/25/16
to phan...@googlegroups.com
Log My environment is Windows with around 8-10 simultaneous pahntom js processes launched from the phantom js cli. I don't bother with getting the PID of the phantom JS process, it's all managed under the hood by commons exec.

Here's a code snippet:

CommandLine cmd = new CommandLine(mwProps.getProperty("phantonjs.exec"));
cmd.addArguments(getPhantomJSOptions(mwProps));
cmd.addArguments(new String[] {mwProps.getProperty("phantonjs.rasterizejs"), location, outputFile.getCanonicalPath(),
mwProps.getProperty("phantonjs.outputPaperSize") });

logger.info("Launching="+cmd);

DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0); //assume exit code 0 is a successful exit

PumpStreamHandler psh = new PumpStreamHandler(new ExecLogHangler(), new ExecLogHangler());
executor.setStreamHandler(psh);
long timeout = 9*60*1000L;
if(mwProps.getProperty("phantonjs.execTimeout")!=null) {
timeout = Long.parseLong(mwProps.getProperty("phantonjs.execTimeout"));
}
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
executor.setWatchdog(watchdog);
executor.execute(cmd);

And the log handler is a simple implementation of commons exec LogOutputStream:
public static class ExecLogHangler extends LogOutputStream {

public ExecLogHangler() {
}

@Override
protected void processLine(String line, int logLevel) {
logger.info(line);
}
}

Reply all
Reply to author
Forward
0 new messages