2

I'm using the phantomjs gem: https://github.com/colszowka/phantomjs-gem

When the script is launched from the controller:

Phantomjs.run('./public/javascripts/test.js')

The rails server hangs while the script is running, waiting for it to finish.

The phantomjs script, 'test.js' looks like this:

var page = require('webpage').create();
page.viewportSize = { width: 1920, height: 1080 };
page.open('http://localhost:3000', function () {
  window.setTimeout(function () {
    page.render('./public/test.png');
    phantom.exit();
  }, 3000);
});

What I'm assuming the problem is, is that rails is waiting for the script to finish while phantomjs is trying to access the rails server, but since the server is busy, they both hold each other up.

Any solutions?

EDIT to address @Artjom B.:

1) Yes I'm using PhantomJS 2.0 on ubuntu 14.04 LTS, I'll try an earlier version.
2) Not sure what you're asking here, I can't access the log from phantomjs this way so anything logged to console is unseen by me. If you know a way around this let me know. Can't seem to run -debugger on the phantomjs call from ruby.
3) Yes the script works great from the command line, no issues at all.
4) Not following you here, either. The Phantomjs.run command is being fired from my controller under a test action. This command runs fine if opening a page other than my local server.

zuff
  • 55
  • 5
  • A rails server that doesn't accept multiple connections? I don't think so, but I know nothing about rails. Are you using PhantomJS 2? Have you tried using PhantomJS 1.9.7? Have you checked the first argument (`status`) to the `page.open()` callback? Does the PhantomJS script return if you run it from the command line? Are you running the gem command from the `/` path so that you're essentially create a recursion bomb? There is not enough information in this question to diagnose your problem. Please answer every clarification request and [edit] your question to add missing information. – Artjom B. Oct 08 '15 at 19:31
  • @ArtjomB. I've edited my post to reflect answers to your questions. Thanks for sparing the time to help me out! – zuff Oct 08 '15 at 20:27
  • for 2) I mean `page.open('http://localhost:3000', function (status) { if (status !== "success") {phantom.exit(1);}...`. Generally, there were issues with opening localhost on PhantomJS, but I haven't run into the issue to date. – Artjom B. Oct 08 '15 at 20:44
  • @ArtjomB. Ah I see, I added that line and the server still freezes. It's like the phantomjs hangs on page.open('http://localhost:3000' and doesn't go any farther. – zuff Oct 08 '15 at 20:51
  • I can see the built-in rails server getting hung up on this. Try thin or unicorn. – pguardiario Oct 08 '15 at 23:46
  • 1
    @pguardiario I'm using Puma which should handle concurrent processes. – zuff Oct 09 '15 at 15:20

1 Answers1

2

You have to use a multi-threaded web server like puma.

See here for an overview of different options.

Another pitfall might be your controller logic. If the page, that gets rendered by phantom, triggers the same controller action than the one, that calls phantom, you create an infinite loop.

Then, even a multi-threaded server would get stuck :)


this question is a duplicate of PhantomJS.rb freezing when trying to screen capture page in my rails application Please close this one. I'm lacking reputation to do so or write this as a comment.

Community
  • 1
  • 1
thutt
  • 640
  • 5
  • 18