How do I Choose a Process Manager for NodeJS
Node.js process manager is a tool, which provides an ability to control application lifecycle, monitor the running services and facilitate common system admin tasks to maintain my project operability.
CloudJiffy provides four pre-configured process managers by default, which can be selected in the following ways:
- By selecting the appropriate tag during the environment creation or container redeploy
- By editing the PROCESS_MANAGER Docker environment variable in the already created container(s) with the forever, npm, pm2 or supervisor value (restart is needed to apply the new options)
Below, we’ll consider each of the available managers to help me select one:
- Process Manager (npm)
- PM2
- Supervisor
- Forever
How should I use Process Manager (npm)
- number - a number of processes to spawn, e.g. 4
- timeout - a time (in milliseconds) for each spawn to occur
- strategy - either series or parallel, to spawn one at a time or all together respectively
- readyOn - signal to indicate the script is ready, either listening (by default) or ready
How do I use PM2 :
PM2 provides a huge variety of application management features, including the launched NodeJS processes monitoring. I can get acquainted with the list of commands for pm2, which can be executed directly via SSH.
For example, after Node.js server creation, I can list the running processes with the following command:
As I can see it shows the default draw-game application is running.
Also, PM2 provides users the ability to create the configuration files where all the run options are listed, which is useful for microservice-based applications deployment, as several apps can be described in a single file. The appropriate config file referencecan be found by following the provided link (e.g. the default ecosystem.config.js file is used to launch the server.js application file as the “draw game” application).
How do I use Supervisor :
Supervisor is a great solution to keep my applications running. It automatically monitors any code changes in the launched .jsscripts and restarts the appropriate app to keep it up-to-date. Herewith, it allows to perform a restart of my NodeJS processes, ensuring they are always available and are automatically restarted in the event of a failure.
I can run the following command on my Node.js server with the supervisor process manager to get help:
Here, I can find a short description of the module, its usage syntax, additional options descriptions and a few examples.
Forever
The forever process manager is the simple CLI tool, which allows to make my NodeJS processes run continuously. It permanently keeps a child process (such as my project on the Node.js web server) and automatically restart it upon failure.
Run the next command to get the main information on the forever manager usage, actions, usage, etc:
Also, using forever I can specify the application options in a JSON file. For example, for the default Draw game (available after Node.js server installation), this /home/cloudjiffy/ROOT/forever.json file looks like:
{
"uid": "app1",
"append": true,
"watch": true,
"script": "server.js",
"sourceDir": "/home/cloudjiffy/ROOT"
}
where:- uid - sets unique name for my app
- append - selects if logs should be supplemented (true) or overwritten (false)
- watch - allows to enable or disable automatic restart of a child process upon the appropriate application code changes
- script - defines a name of the executable .js file
- sourceDir - provides an absolute path to the specified script