Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.31k stars 2.61k forks source link

How to get rid of annoying popups in Windows 10? #2182

Open nakamorichi opened 8 years ago

nakamorichi commented 8 years ago

How do I disable the popup windows that pm2 generates in Windows 10 when starting, restarting or reloading a Node.js app? From the perspective of rapid server-side development, this is very annoying...

Unitech commented 8 years ago

Would it be an issue because of the way we spawn processes?

I don't know if I have to customize something on this code segments,

soyuka commented 8 years ago

{detached: true should get rid of this error but it is not available for cluster.

Unitech commented 8 years ago

interesting, maybe @ThisIsMac47 will have a look at this on the next weeks

vmarchaud commented 8 years ago

After testing, the detached option doesnt hide popups on Windows, i think that nodejs spawn a shell to spawn the process. I will investigate more in the next days.

vmarchaud commented 8 years ago

Okay so :

If you get any popup apart of cluster mode, respond to this issue to report it, i will investigate for each one.

soyuka commented 8 years ago

Because we can't get detached: true in cluster?

vmarchaud commented 8 years ago

They use the function fork, we can use detached: true only with spawn.

soyuka commented 8 years ago

Yes ok so if that's the main reason, the only solution is to port cluster into pm2 instead of using the nodejs cluster. Did you try a simple cluster implementation without pm2 to see if it pops up too?

vmarchaud commented 8 years ago

I wasnt able to reproduce with a simple cluster implementation, this might coming from the fact that we are a spawning a child from a forked child or something like this. We manage to delete the popups in cluster mod by rewriting the implementation on the branch of a forked cluster implementation, we will talk about how include this into future release of pm2.

ajithr commented 7 years ago

Is this issue fixed or any workaround available? I have faced the same issue when using cluster mode with watch option. The node window opens multiple time when saving the file.

vmarchaud commented 7 years ago

@ajithr No, like i said, the problem come from nodeJs implementation of the cluster mode. I suggest you to develop in fork mode.

gustavohenke commented 7 years ago

For me what has been working quite well is to use --no-daemon. However, if pm2 was previously started as a daemon process, then this flag has no effect and the popups keep appearing, unless I run pm2 kill.

ghost commented 7 years ago

@gustavohenke I followed your instructions, but it did not work for me. I am using pm2 2.0.19 on Windows 8.1 and I still get a popup window everytime I use child_process.exec()

vmarchaud commented 7 years ago

@centigrade-thomas-becker The fact a popup is spawning when you call child_process.exec isn't coming from pm2, it come from the system of spawning a child process of node itself, you may add detached: true to the spawning config and it will not spawn any popup.

The problem of #2182 is that in cluster mode, we can't control how the process are spawn, so we can't add this options to hide the popup under windows.

soyuka commented 7 years ago

@centigrade-thomas-becker you should try to use child_process.spawn and add a {detached: true} option.

mswaminath commented 7 years ago

I wanted to share what I did to overcome the popup. Initially I will npm start my app that starts pm2 as well, then I will do pm2 stop all immediately. After that when I again do npm start, it just pops up only once and never again.

tayler-king commented 7 years ago

Do we have any updates on this for a proper implementation that will patch this issue?

I use PM2 when locally developing. It's becoming an issue to run pm2 kill after every script run. Consoles interrupting me when working is not welcome.

raghavmac commented 7 years ago

Try this, 1) npm start your project as usual (pop-up's keep on coming up) 2) Stop the project (Ctrl-C) 3) pm2 delete <project-name> 4) Again npm start (this time it stops popping up)

Hope it works!

tayler-king commented 7 years ago

I use the process json file to launch my pm2 instances so I do not believe that will work for me. On Fri, 31 Mar 2017 at 1:07 pm, Raghav Manikandan notifications@github.com wrote:

Try this,

  1. npm start your project as usual (pop-up's keep on coming up)
  2. Stop the project (Ctrl-C)
  3. pm2 delete
  4. Again npm start (this time it stops popping up)

Hope it works!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Unitech/pm2/issues/2182#issuecomment-290694966, or mute the thread https://github.com/notifications/unsubscribe-auth/AQFo-gSTsM1ff6b76yV4Qj20hfUV-uF4ks5rrOyHgaJpZM4Ijsqx .

sant123 commented 7 years ago

Any fix to this? Just asking 😄

ghost commented 7 years ago

I wanted to share what I did to overcome the popup. Initially I will npm start my app that starts pm2 as well, then I will do pm2 stop all immediately. After that when I again do npm start, it just pops up only once and never again.

@mswaminath Can you please post your pm2 config, and how you spawn your child processes?

parthvakharia commented 7 years ago

Any permanent solution for this ??

jgjake2 commented 7 years ago

This issue is especially terrible if you have autorestart enabled, and something causes your script to crash after it loads (I accidentally renamed a file the script frequently reads from). Then all hell breaks loose! Windows are spawning and crashing so fast, I couldn't type or click on anything since the windows spawn in the foreground with focus. It's completely ignoring "max_restarts" and "restart_delay" (i'm guessing these don't apply to clusters). I literally had no choice but to hard reset my computer.

wayofthefuture commented 7 years ago

Wow it feels like I just won a game of solitaire

wayofthefuture commented 7 years ago

So for production applications it is imperative to take the weight off the single event loop by forking a few processes to balance the load. Well, fork() does not take a detached option, and nor should it. Once the primary process fails the child processes will be closed subsequently, allowing pm2 to respawn the parent which will re-fork children, while keeping only 1 instance of each process and maintaining the IPC pipe between them. This is quite simple and the forking nature of the node parent process is beautiful in this regard in my opinion.

At this point our only option is to use pm2 to create 4 actual and separate node apps. This may work, maybe. I have no idea how we will accomplish any kind of IPC messages back and forth from the processes considering there is no parent process to route the messages.

In other words, pm2 for production purposes on windows is currently not working. I will mention that if anyone can find time to resolve this issue, we will be happy to finance your hourly work in the name of getting this problem solved ASAP.

Thank you.

raghavmac commented 7 years ago

I have found a very simple fix that am using now for development,

  1. On your pm2 config file, just remove/comment the instances: 1 line
  2. pm2 delete <project-name> (one time process)
  3. npm start

Then you will see the magic, but I suggest to use this only for development.

wayofthefuture commented 7 years ago

We aren't using a config file. Our master process forks into sub-processes. Will this work for that?

wayofthefuture commented 7 years ago

SOLVED.

Use winsw and run "node myapp.js" as a windows service.

https://github.com/kohsuke/winsw

tayler-king commented 7 years ago

Any updates on this? I run multiple instances so cannot write off instance support.

atrauzzi commented 7 years ago

I've opened this ticket with nodejs: https://github.com/nodejs/node/issues/15217

Would be great if everyone watching this ticket went and upvoted and shared this one. Really it seems like something that wouldn't be high impact for them to fix!

gregorskii commented 6 years ago

I think this is occurring for me on exec processes called by PM2 process management. I am using fork mode. What do you think, does this occur for subprocesses started with exec even with fork?

milichev commented 6 years ago

Any updates? child_process.spawn::windowsHide option is already awailable: https://github.com/nodejs/node/issues/15217#ref-pullrequest-267864790

Unitech commented 6 years ago

https://github.com/Unitech/pm2/pull/3255 https://github.com/nodejs/node/issues/15217#ref-pullrequest-267864790 thanks to @soyuka @milichev @cjihrig

soyuka commented 6 years ago

May you try development branch @milichev ?

FireController1847 commented 6 years ago

Is it possible to get this a little earlier than release?

milichev commented 6 years ago

Installed with no success. The same console fireworks on each watched file change -(

node -v
v8.9.0

yarn global add https://github.com/Unitech/pm2@development
toddwong commented 6 years ago

Any update?

FireController1847 commented 6 years ago

It was fixed for me

toddwong commented 6 years ago

@FireController1847 Not for me I think.

git clone git@github.com:Unitech/pm2.git
cd pm2
git checkout development
npm link
cd \test
pm2 start test.js -i 2

Still got 2 black console windows poped up!

node --version v9.2.0 pm2 --version 2.8.0

I'm on Window 10.

Is there any wrong with my setup?

FireController1847 commented 6 years ago

Ah, actually, you're right. The window does popup. Nevermind.

toddwong commented 6 years ago

Unless i missed something, God.nodeApp(in lib/God/ClusterMode.js) called cluster.fork, which has no windowsHide parameter at all!? Dead end!

toddwong commented 6 years ago

I got a hacky solution

diff --git a/lib/God.js b/lib/God.js
index bf3bedda..61178584 100644
--- a/lib/God.js
+++ b/lib/God.js
@@ -15,7 +15,7 @@
  ******************************/

 var semver        = require('semver');
-var cluster       = require('cluster');
+var cluster       = require('./cluster_hack');
 var numCPUs       = require('os').cpus() ? require('os').cpus().length : 1;
 var path          = require('path');
 var EventEmitter2 = require('eventemitter2').EventEmitter2;
diff --git a/lib/God/ClusterMode.js b/lib/God/ClusterMode.js
index 3a1185a3..af9bf43a 100644
--- a/lib/God/ClusterMode.js
+++ b/lib/God/ClusterMode.js
@@ -10,7 +10,7 @@
  * @author Alexandre Strzelewicz <as@unitech.io>
  * @project PM2
  */
-var cluster       = require('cluster');
+var cluster       = require('../cluster_hack');
 var cst           = require('../../constants.js');
 var Utility       = require('../Utility.js');
 var pkg           = require('../../package.json');
@@ -49,7 +49,7 @@ module.exports = function ClusterMode(God) {
       // { "args": ["foo", "bar"], "env": { "foo1": "bar1" }} will be parsed to
       // { "args": "foo, bar", "env": "[object Object]"}
       // So we passing a stringified JSON here.
-      clu = cluster.fork({pm2_env: JSON.stringify(env_copy), windowsHide: true});
+      clu = cluster.fork({pm2_env: JSON.stringify(env_copy), pm2_windowsHide: true});
     } catch(e) {
       God.logAndGenerateError(e);
       return cb(e);
diff --git a/lib/cluster_hack.js b/lib/cluster_hack.js
new file mode 100644
index 00000000..c1cfd86b
--- /dev/null
+++ b/lib/cluster_hack.js
@@ -0,0 +1,18 @@
+var child_process = require('child_process');
+var oldFork = child_process.fork;
+child_process.fork = function () {
+  var pos = 1;
+  if (pos < arguments.length && Array.isArray(arguments[pos]))
+    pos++;
+  if (pos < arguments.length && arguments[pos] != null
+          && typeof arguments[pos] === 'object'
+          && arguments[pos].env && arguments[pos].env.pm2_windowsHide) {
+    arguments[pos].windowsHide = true;
+    arguments[pos].silent = true;
+  }
+  return oldFork.apply(this, arguments);
+};
+var cluster = require('cluster');
+child_process.fork = oldFork;
+
+module.exports = cluster;

But still get black windows flashing when pm2 stop x pm2 kill pm2 update ...


edit: I make a fork, toddwong/pm2@dc184e9ef76badbb565d9d61f6d39d4dda712db5

soyuka commented 6 years ago

This should be fixed in nodejs then no?

toddwong commented 6 years ago

@soyuka Not sure, but I've filed a feature request nodejs/node#17370 But the black windows during pm2 stop x etc. should be address in pm2 I think.

TheOptimisticFactory commented 6 years ago

When using fork with watch activated, a black window flashes everytime a change is detected. Is this normal behavior?

sant123 commented 6 years ago

It seems is almost done https://github.com/nodejs/node/pull/17412

dwelle commented 6 years ago

Meanwhile, if this helps someone: pm2@2.2.1 is the last version that doesn't show pop-ups for me on win7, fork mode (above that, it doesn't matter if I run fork mode or cluster, it always shows popups).

Unitech commented 6 years ago

Should work now, please try latest pm2 published:

$ npm install pm2 -g
$ pm2 update
sant123 commented 6 years ago

Unfortunately continues spawning these windows 😞

image

Node: v9.4.0 OS: Windows 10 pm2: v2.9.2

Command: pm2 start app.js -i max terminal: tested in git-bash, cmd and powershell

sant123 commented 6 years ago

The server script:

const http = require("http");
const server = http.createServer(handler);
const port = 4875;

server.listen(port, () => {
    console.log("Listening at port %d", port);
});

function handler(req, res) {
    res.end("Holita desde Node.js");
}