numtel / phantomjs-persistent-server

PhantomJS Persistent Server Meteor Package
MIT License
8 stars 2 forks source link

Preserving PhantomJS instance across code-reloads in development #1

Open chriswessels opened 9 years ago

chriswessels commented 9 years ago

Hi there,

Thanks for your work on this great smart package.

In my server code I'm running phantomLaunch to start a new instance of PhantomJS and assigning it to a global variable.

Meteor.startup(function () {
  if (typeof PhantomInstance === 'undefined') {
    PhantomInstance = phantomLaunch({ debug: true });
    console.log('Started PhantomJS...');
  }
});

However, upon server reload after a code change, I see this error in the server console:

I20141124-15:17:02.644(0)? Exception in callback of async function: Error: Error: ENOENT, open '/proc/5658/status' [500]
I20141124-15:17:02.647(0)?     at packages/numtel:phantomjs-persistent-server/src/main.js:80
I20141124-15:17:02.647(0)?     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108)

At this time, if I navigate to localhost:13470, I still get output, so PhantomJS is still running. It looks like some vital state information is being lost from reload to reload.

If I just reassign PhantomInstance, the port number increments with each server reload as the old instances of PhantomJS continue to run under Meteor.

Do you know of any acceptable workarounds?

Kind regards, Chris

numtel commented 9 years ago

Thanks for the kind words!

I am not sure I understand how to reproduce this issue. I've copied the posted code into a new meteor 'Hello' app but the orphaned process is detected fine for me. You can see in the following log starting the app and then making a change to the code:

[[[[[ ~/meteor/phantom-issue-1 ]]]]]          

=> Started proxy.                             
=> Started MongoDB.                           
I20141124-19:16:45.300(-8)? PhantomJS server starting on port 13470
I20141124-19:16:45.542(-8)? 13470 Ready.
I20141124-19:16:45.542(-8)? Started PhantomJS...
=> Started your app.

=> App running at: http://localhost:3000/
I20141124-19:18:14.701(-8)? Recovering orphaned port 13470
I20141124-19:18:15.126(-8)? PhantomJS server starting on port 13470
I20141124-19:18:15.142(-8)? 13470 Ready.
I20141124-19:18:15.143(-8)? Started PhantomJS...
=> Meteor server restarted

What OS are you running? If you could post a repo that I could clone to see the error that would be helpful.

As for the auto-port selector, I am considering replacing my linux foo with an NPM package like this: https://www.npmjs.org/package/get-port

From your question though, I wonder if you want the PhantomJS process to continue running after a new code push?

In order to implement this, it would probably be possible to continue issuing requests to an orphaned PhantomJS process but it may be difficult to detect if that process was successfully processing the requests.

Right now, when the code push occurs, your Meteor server (a node.js instance) starts a new process. Because PhantomJS is a child process, it becomes orphaned. This code will kill an orphaned PhantomJS if it occupies the port requested because it assumes that it was originally spawned by the app as well.

-Ben

numtel commented 9 years ago

Ok, I've updated the package with the NPM get-port package. Give 0.0.9 a try!

There are a few changes to the interface:

Please let me know if this fixes your issues.