Run at startup and monitor redis and node.js processes - node.js

I want to deploy node.js app which depends on redis. Both processes will run on the same VPS. There are plenty of examples on how to daemonize and monitor node and I've also found some uncommented configuration for redis. How do I put it together? Can I just combine these two snippets in one monitrc file?

You could use Supervisord to orchestrate the launch of Redis and your NodeJS apps (use the priority parameter to start Redis before your apps). Supervisord will automatically restart your NodeJS app if they crash.
You can then setup monit over it to be alerted when something wrong happens and to restart your NodeJS processes if they use too much memory/cpu or if they are not accessible anymore from a specific port.

Related

What is the best node process manager for Windows Server?

Our team maintains a Node-Express API in a Windows Server environment.
We've struggled to find a reliable process manager.
The Express js site lists some favored pm's, but they all seem Linux-optimized (Forever, PM2, SystemD, Strong-PM).
None of them will create a windows service to revive the process after server reboots without another module like node-windows or pm2-windows-service (based on node-windows). Node-windows works but it requires manual intervention to kill node processes when you stop the service.
Any advice out there on best process managers for maintaining a node process in Windows?
Note: I've got another question out there where I'm trying to debug our implementation of PM2: Why is PM2 not launching my Node process?

Can we use Apache Server for server side rendering of angular web apps or we have to stick with NodeJs

Currently I am running NodeJs server as background process in the host machine to achieve sender side rendering for my angular app.
On Linux for e.g. npm rum server & (ampersand is to put process in background)
But I am looking for solution like Apache Server that manages it start/stop with host reboot.
I think the best way for you to acheive what you're looking for is to use a management solution like PM2 or Forvever. These will pretty easily manage your solution in the background for you.
Rather than apache/nginx managing start stop of your node application, you may create service to run your node application. It will run without any manual intervention.
Start your Node app on some other port than 80, as your main web server might be running on that port.
Create a service file in /etc/init to start your Node app
Configure apache/nginx with reverse proxy to the node applicaiton
Start both of the services: 'service start nodeapp.conf' and 'service start apache2'
This would make your life for handling these services pretty easy.
Yes. You should be able to.
Start by creating a proper deployment directory - https://angular.io/guide/deployment
Then copy/ftp/whatever to the web server.
The tricky part is in your route controllers, etc. and getting all of the paths right if you end up deploying into a different directory than what you developed for.
You need to use Apache/Ngnix with NodeJS using proxy.
Look into this link, if this helps:
https://blog.daudr.me/painless-angular-ssr/

Cluster and Fork mode difference in PM2

I've searched a lot to figure out this question, but I didn't get clear explanation. Is there only one difference thing that clustered app can be scaled out and forked app cannot be?
PM2's public site explains Cluster mode can do these feature but no one says about pros of Fork mode (maybe, it can get NODE_APP_INSTANCE variable).
I feel like Cluster might be part of Fork because Fork seems like to be used in general. So, I guess Fork means just 'forked process' from the point of PM2 and Cluster means 'forked process that is able to be scaled out'. Then, why should I use Fork mode?
The main difference between fork_mode and cluster_mode is that it orders pm2 to use either the child_process.fork api or the cluster api.
What does this means internally?
Fork mode
Take the fork mode as a basic process spawning. This allows to change the exec_interpreter, so that you can run a php or a python server with pm2. Yes, the exec_interpreter is the "command" used to start the child process. By default, pm2 will use node so that pm2 start server.js will do something like:
require('child_process').spawn('node', ['server.js'])
This mode is very useful because it enables a lot of possibilities. For example, you could launch multiple servers on pre-established ports which will then be load-balanced by HAProxy or Nginx.
Cluster mode
The cluster will only work with node as it's exec_interpreter because it will access to the nodejs cluster module (eg: isMaster, fork methods etc.). This is great for zero-configuration process management because the process will automatically be forked in multiple instances.
For example pm2 start -i 4 server.js will launch 4 instances of server.js and let the cluster module handle load balancing.
Node.js is single-thread.
That means only 1 core of your Intel quad-core CPU can execute the node application.
It called: fork_mode.
We use it for local dev.
pm2 start server.js -i 0 helps you running 1 node thread on each core of your CPU.
And auto-load-balance the stateless coming requests.
On the same port.
We call it: cluster_mode.
Which is used for the sake of performance on production.
You may also choose to do this on local dev if you want to stress test your PC :)
Documentation and sources are really misleading here.
Reading up on this in the sources, the only differences seems to be, that they use either node cluster or child_process API. Since cluster uses the latter, you are actually doing the same. There is just a lot more custom stdio passing around happening inn fork_mode. Also cluster can only be communicated with via strings, not objects.
By default you are using fork_mode. If you pass the the -i [number]-option, you're going into cluster_mode, which you generally aim for w/ pm2.
Also fork_mode instance probably can't listen on the same port due to EADDRINUSE. cluster_mode can. This way you also can structure you app to run on the same port being automatically load balanced. You have to build apps without state then though e.g. sessions, dbs.

nodejs for background process, not a http server

Im new to nodejs, i am looking for a way to create a nodejs process that can run in the background. All the example I can find is to create a node http server. I don't need to listen for any web request i just need to startup a process and have it listen to a message queue.
NodeJS has enough API to be used for versatile tasks, not just HTTP Servers. You have two problems to solve:
Run Node process continuously. So, you'd need some equivalent of http.listen() so the process just don't exit. Whatever you intent to do, you'd probably need Node waiting for some external events.
Run node as a daemon or service. There are plenty of modules to help, foreman, pm2. You can make this process auto-start by using upstart on linux machines and node-window for windows.

Is it possible to run sails.js on a node cluster?

I am currently running an express server using the node js vanilla cluster setup as demonstrated over here:
http://rowanmanning.com/posts/node-cluster-and-express/
I'd like to move the server over to sails.js and I am wondering if anyone knows how to configure sails to support the node cluster (no proxies, just simple cluster).
TX,
Sean.
First thing - if You want to use sessions, You need to use session store. Otherwise session will be not shared between instances of Your app.
Then, The easiest way is to use something like PM2, which can be found here: https://github.com/Unitech/pm2
You dont need to do changes in Your app.js files - it should be written as standard non-clustered sails app. PM2 will do the job.
Just start the app with pm2 start app.js -i x where x is number of instances or use pm2 start app.js -i max which will start instances that are equal to numbers of processors, or processor threads.
PM2 is great and very stable, it has many features to run it smoothly in producation, however it has some flaws in development. If You will ever have a problem with "port already in use" after stopping or even deleting app that was using it - You will have to use pm2 kill which will kill ALL Your apps.
Other than that - its just great - with some additional monitoring tools.
You can use PM2 Library a create different instances like cluster.
For do it you have to use app.js file, like:
pm2 start app.js
If you want to run the max number of instances availables:
pm2 start app.js -i max
check the documentation for more: https://github.com/Unitech/pm2

Resources