sheebz / phantom-proxy

a lightweight proxy that lets you to drive phantomjs from node.
MIT License
137 stars 34 forks source link

Multiple proxies are supported? #36

Open estliberitas opened 11 years ago

estliberitas commented 11 years ago

Hi, guys. It seems that right now we can't create 2+ instances of proxies. When I try to do require('phantom-proxy').create(fn) several times, module spawns two processes with different PhantomJS options (as in my case), that works fine. But when I do proxy.page.open() for each proxy, I get various errors. Seems like proxies share smth. and are not fully separated.

Also, I have different options.port, options.clientPort.

If I remember right, in some issue @sheebz said that proxy is singletone, or maybe I did not get his thought properly. So what is the situation?

dmdeklerk commented 11 years ago

I sure hope that's not the case. Why would this not be possible? I was kinda expecting this would be possible.

estliberitas commented 11 years ago

Right now when I create two proxies I begin to issue strange behavior: pages are not loading, or empty page, or evaluation does not work and other bad things. Please, can you confirm that multiple proxies won't do a mess with each other?

estliberitas commented 11 years ago

The latest experience showed that if I do clean of require.cache and then do require('phantom-proxy').create() it acts fine!

dmdeklerk commented 11 years ago

It seems to me that lib/proxy.js should not be creating servers on port 1061 without checking if another server is running there already. When doing a check for existing ports given to runnning phantom-proxy instances, you can allow multiple instances without requiring the user to supply a port.

dmdeklerk commented 11 years ago

Also the way you create your http server in lib/proxy.js looks like it could get you into trouble when running multiple phantom-proxy instances at the same time. I'm no expert on the http module but when running multiple instances one would think you must be able to provide a port yourself and not rely on the http module to choose one as can be seen at: https://github.com/sheebz/phantom-proxy/blob/3400f5b2a028cd7cd4e07b219a3020d953e933b6/lib/proxy.js#L193

kevzettler commented 11 years ago

I don't think multiple proxies, and hence multiple Phantomjs instances is the way to handle this. You can have a single Phantomjs process open up multiple pages and act in parallell. However this library dosen't seem to support that either.

sheebz commented 11 years ago

As @dmdeklerk mentioned, you can specify a port for the server to run - each server is basically an instance. You will also need to specify a different clientport value, clientport is the socket.io callback channel - used mostly to remote events from server object.

This is how I would expect it to work, I haven't actually run significant tests in this way other than verifying that the server was actually listening on the specefied port. so tthere could be some issues. @estliberitas If there is an issue with this, please include some sample code, I'll make it a priority to fix.

estliberitas commented 11 years ago

No problems, guys. When I'm near PC I'll send ya code.

I have tried setting different ports and saw that app creates different HTTP servers w/ socket.io, but after some time they were dying leaving only one HTTP server and PhantomJS instance. Why? That is a question. I think the issue may be connected with some event listeners of socket.io, proxy or some global referenced variable...

After I added clearing of 'phantom-proxy' module cache (cleaning node's require.cache) everything's is working well!

So the issue is not at http server port at all - I thought about that and checked that in this way module acts good. On Jan 19, 2013 8:53 AM, "Rob Schieber" notifications@github.com wrote:

As @dmdeklerk https://github.com/dmdeklerk mentioned, you can specify a port for the server to run - each server is basically an instance. This is how I would expect it to work, I haven't actually run significant tests in this way so tthere could be some issues. @estliberitashttps://github.com/estliberitasIf there is an issue with this, please include some sample code, I'll make it a priority to fix.

— Reply to this email directly or view it on GitHubhttps://github.com/sheebz/phantom-proxy/issues/36#issuecomment-12450527.

sunki commented 11 years ago

@kevzettler: It seems that multiply pages per one instance are not supported, see for example: http://stackoverflow.com/questions/10353833/phantomjs-unexpected-load-behavior-with-multiple-pages and https://github.com/dodysw/phantomjs/commit/f2047de19f80dc48acb241894611f57f6cdf1e73 (I'm also getting strange behaviour in this case).

@estliberitas: Can you describe pls what should I do to run multiply instances of phantomjs server. I tried to clean cache (see below) but also getting stuck (still only one phantomjs process are used by phantom-proxy)

require.cache['phantom-proxy'] = null;
var phantom = require('phantom-proxy');
phantom.create({'debug': false, 'port': serv.port}, function(ph){
estliberitas commented 11 years ago

@sunki: hey, bro, you should clean all the entries having 'phantom-proxy' string in require.cache map thus it keeps paths to source files and not module names. My cleaning code is:

cleanRequireCacheFor = function(sub) {
  var cache = require.cache
    , k;

  for (k in cache) {
    if (cache.hasOwnProperty(k) && k.indexOf(sub) !== -1) {
      delete cache[k];
    }
  }

};

Did you get the idea?

sunki commented 11 years ago

@estliberitas Thanks a lot. Cant say if it usable or not cause Im getting Error: cannot access member `statusCode' of deleted QObject now.

estliberitas commented 11 years ago

Oh, I was getting this kind of error some time ago. Googling helped me a bit. These was already issue with this error here. ;)

On Fri, Apr 26, 2013 at 3:36 PM, sunki notifications@github.com wrote:

@estliberitas https://github.com/estliberitas Thanks a lot. Cant sat if it usable or not cause Im getting Error: cannot access member `statusCode' of deleted QObject now.

— Reply to this email directly or view it on GitHubhttps://github.com/sheebz/phantom-proxy/issues/36#issuecomment-17068322 .

Sincerely, Alexander Makarenko

Curriculum vitae: http://liberitas.info/cv Cell.: +7 988 517 0645 Skype: estliberitas

WoZ commented 11 years ago

@sunki Look at my issue #56.